8 Replies - 498 Views - Last Post: 07 February 2012 - 03:29 PM Rate Topic: -----

Topic Sponsor:

#1 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 05-February 12

I am having a hard time grasping classes and virtual objects

Posted 05 February 2012 - 05:11 PM

I know that someone else was having trouble with this same example. However that question did not get much attention so now I am doing it and having a hard time figuring out what I am doing.

"Revise the baseDMA-lacksDMA-hasDMA class hierarchy so that all three classes are derived from an ABC. Test the result with a program similar to the one in Listing 13.10. That is, it should feature an array of pointers to the ABC (abstract base class) and allow the user to make runtime decisions as to what types of objects are created. Add virtual View() methods to the class definitions to handle displaying the data."

The comments are there to help me know what goes where however I do not know how to code these things.

//pe13dma.h
#ifndef DMA_H_
#define DMA_H_
#include <iostream>

// Abstract Base Class
class ABC
{
private:
   char * label;
   int rating;
public:
    ABC(const char * l = "null", int r = 0);
    ABC(const ABC & rs);
    virtual ~ABC() = 0;
    virtual ABC & operator*()  { return *this; }
    ABC & operator=(const ABC & rs);
    virtual void View() const;
    friend std::ostream & operator<<( 
        std::ostream & os, const ABC & rs);
};

//  Former Base Class Using DMA
class baseDMA : public ABC
{
private:

public:
    baseDMA(const char * l = "null", int r = 0);
};

// derived class without DMA
// no destructor needed
// uses implicit copy constructor
// uses implicit assignment operator
class lacksDMA :public ABC
{
private:
    char color[40];
public:
    lacksDMA(const char * c = "blank", const char * l = "null",
              int r = 0);
    lacksDMA(const char * c, const ABC & rs);
    void View() const;
};

// derived class with DMA
class hasDMA :public ABC
{
private:
    char * style;
public:
    hasDMA(const char * s = "none", const char * l = "null",
              int r = 0);
    hasDMA(const char * s, const ABC & rs);
    hasDMA(const hasDMA & hs);
    ~hasDMA();
    hasDMA & operator=(const hasDMA & rs);
    void View() const;
};

#endif



//pe13dma.cpp
#include "pe13dma.h"
#include <cstring>

// ABC methods
// Code the methods for the abstract base class here


// baseDMA methods
baseDMA::baseDMA(const char * l, int r) : ABC(l,r)
{
	
}

// lacksDMA methods
lacksDMA::lacksDMA(const char * c, const char * l, int r)
    : ABC(l, r)
{
    std::strncpy(color, c, 39);
    color[39] = '\0';
}

lacksDMA::lacksDMA(const char * c, const ABC & rs)
    : ABC(rs)
{
    std::strncpy(color, c, 39);
    color[39] = '\0';
}

void lacksDMA::View() const
{
    ABC::View();
    std::cout << "Color: " << color << std::endl;
}


// hasDMA methods
hasDMA::hasDMA(const char * s, const char * l, int r)
         : ABC(l, r)
{
    style = new char[std::strlen(s) + 1];
    std::strcpy(style, s);
}

hasDMA::hasDMA(const char * s, const ABC & rs)
         : ABC(rs)
{
    style = new char[std::strlen(s) + 1];
    std::strcpy(style, s);
}

hasDMA::hasDMA(const hasDMA & hs)
         : ABC(hs)  // invoke base class copy constructor
{
    style = new char[std::strlen(hs.style) + 1];
    std::strcpy(style, hs.style);
}

hasDMA::~hasDMA()
{
    delete [] style;
}

hasDMA & hasDMA::operator=(const hasDMA & hs)
{
    if (this == &hs)
        return *this;
    ABC::operator=(hs);  // copy base portion
    style = new char[std::strlen(hs.style) + 1];
    std::strcpy(style, hs.style);
    return *this;
}

void hasDMA::View() const
{
    ABC::View();
    std::cout << "Style: " << style << std::endl;
}



//main.cpp
// pe13-3.cpp -- inheritance, friends, and DMA
// compile with pe13dma.cpp
#include <iostream>
#include "pe13dma.h"

int main()
{
	using std::cout;
	using std::endl;

	// create children classes and copy constructors


	// create a parent array and load the children


	// use polymorphism to display


	// pause and return the environment variable
	cout << endl;
	system("pause");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: I am having a hard time grasping classes and virtual objects

#2 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: I am having a hard time grasping classes and virtual objects

Posted 06 February 2012 - 11:49 AM

Before getting off on the wrong foot based on a misunderstanding, let me tell you how the reads to us... You tell me if I have it wrong.

Homework instructions said:

"Revise the baseDMA-lacksDMA-hasDMA class hierarchy so that all three classes are derived from an ABC. Test the result with a program similar to the one in Listing 13.10. That is, it should feature an array of pointers to the ABC (abstract base class) and allow the user to make runtime decisions as to what types of objects are created. Add virtual View() methods to the class definitions to handle displaying the data."


View Postkillermunk1, on 05 February 2012 - 06:11 PM, said:

I do not know how to code these things, can someone do my homework for me?


Which might explain this part

View Postkillermunk1, on 05 February 2012 - 06:11 PM, said:

I know that someone else was having trouble with this same example. However that question did not get much attention

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: I am having a hard time grasping classes and virtual objects

Posted 06 February 2012 - 12:11 PM

If what you really need is an explaination of the concepts of classes as the title suggests, maybe this will help:

Think of objects in coding just as you would objects in the real world.

A Dodge Ram is an object.
It is made up of smaller objects: Engine, doors, tires
Each of those is made up of smaller objects: Bolts, pistons, etc.

Objects in coding can inherit from each other, usually from the general to the specific.

  • Class vehicle
    • Class truck : vehicle
      • Class Ram : Truck
        • class 2500FWD : Ram


A class is the blueprint for instanciating (making an instance of) the object.

DodgeRam is a class describing how to make an instance, but itself is not an actual thing you can interact with.

myDodgeRam is an instance of a the class DodgeRam

I can do things with the object instance myDodgeRam

myDodgeRam.SerialNumber = 123456789;
myDodgeRam.FillUpTank();
float fuelLevel = myDodgeRam.FuelTankPercentageFull;
if (myDodgeRam.IsReady) myDodgeRam.StartEngine();


Anything defined in the base class is available to a child.

class truck : vehicle
{

    public float FuelTankPercentageFull
    {
       get; set;
    }
}

class DodgeRam : truck
{
    // I don't have to define a FuelTankPercentageFull here because I inherit it from my parent
}


Methods defined as virtual in the base class can be overridden by the child class (at least in .NET languages like C#). This is often to account for more specific needs.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Do something to start the engine
       return true; // No checks or requirements
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return true;
        }
        return false;
    }
}


You can even have a child class call the base classes methods which is often the smart way to go.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Notice there are no safety checks before trying to start up.
       try
       {
          // Do something to start the engine
          return true; // because we succeeded
       }
       catch(exception error)
       {
           return false; // because there was an error
       }
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return base.StartEngine();
            // Now all the electrical work is in the base class
            // and not repeated in every child.
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 Kajun160  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-August 09

Re: I am having a hard time grasping classes and virtual objects

Posted 06 February 2012 - 01:42 PM

The assignment is giving you a pretty straight forward request. Take a look at the example given to you about the Dodge Ram. He actually broke your answer to your assignment to you in perfect "easiness" as I call it. You are not going to get your homework completed for you on here, doesn't teach you a thing in the long run. Not trying to be mean, but if you can't handle the basics of programming (or do not want to learn), then you will get crushed in the real world.

Now if you actually TRY to do it yourself and need help, then you will get good resulting replies. But you are not even trying, and thus like the other person who posted this same example, your request will fall quite short.

If your textbook is falling short on helping you, then you need to do what I did when I was in college. Ask for help, but show some workmanship (or aka try), and then ask for help in a particular area and explain what you tried to do and why it failed. That will work more in your favor. Of course we are smart enough to see if you just change a word or letter. Try looking through the tutorials on this site, or get on Amazon.com and buy some really good textbooks to help you learn. I personally recommend "C++ Without Fear" (Second Edition is out now) by Brian Overland. It is a few years old, but will help you out learning as it is written in very easy to learn english. I am a Software Engineer and this book sits on my desk as we speak and I even pull it out every now and then to look up something.
Was This Post Helpful? 1
  • +
  • -

#5 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 122
  • Joined: 24-January 12

Re: I am having a hard time grasping classes and virtual objects

Posted 07 February 2012 - 01:13 PM

Quote

I personally recommend "C++ Without Fear" (Second Edition is out now) by Brian Overland. It is a few years old, but will help you out learning as it is written in very easy to learn english. I am a Software Engineer and this book sits on my desk as we speak and I even pull it out every now and then to look up something.


Good call on that book. I decided recently to teach myself C++ and Googled best c++ book or something like that and the title you mentioned was the top rank in the reviews I read.
It's been at my elbow for the past 2 or 3 weeks as I dive into learning this stuff.
Was This Post Helpful? 0
  • +
  • -

#6 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 05-February 12

Re: I am having a hard time grasping classes and virtual objects

Posted 07 February 2012 - 01:39 PM

From what I can tell, you guys obviously misunderstood what I wanted (well, half of you). The example was there to tell you specifically what I am working with. From me saying "I do not know how to code these things". I was not saying that you should do my homework. I meant what the title says. I was not directly looking for the code for the example. I do not work all that well from reading my book because often times the book spreads what i need to know over about 40 pages. However reading it in a condensed form is what I needed to understand how classes and objects worked together and how to code them.
Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 301
  • View blog
  • Posts: 1,039
  • Joined: 31-December 10

Re: I am having a hard time grasping classes and virtual objects

Posted 07 February 2012 - 03:22 PM

Did you even read tlhIn`toq'd second post? (S)he did a pretty good job explaining the basics of classes and objects.

Think of it this way, the types in C++ like "int", "char", "double", or "float" are called POD types (Plain Old Data types). You could think of them each as a separate class. Then when you declare variables of those types, those variables are "objects" of those classes. So all objects are, are variables. They can also be called "instantiations" of a class.

When you write a class, you are defining a "type", similar to the POD types, just more complex. When you define a class, you define how that class can be used. Once you've defined a class, you can use it like the other types, you just need to make sure that the class is in scope when you use it, you do this by including the class' header file. There are other ways to do it, but this is the most common.

You've been using classes and objects all along, you probably just didn't know it. If you're first C++ program was the canonical "Hello, World!" program, then you definitely have. Whenever you need to do input/output on the console, you include the header file: "iostream". This header includes other headers and together they actually make up a class hierarchy. The header also "instantiates" four objects, "cout", "cin", "cerr", and "clog", all in the "std" namespace. For the "hello world" program, you had something like:
#include <iostream>

int main()
{
     std::cout << "Hello, World!\n";
}


The line of code inside main, looks something like this to the compiler:
operator<<(cout, "Hello, World!\n");


That function call is defined inside the header iostream or inside a header that's included by iostream. The function takes two arguments. The first is the object on which the action(output) is performed. The second argument is the data to be output. If you just look at the standard C++ library, you have a perfect example of classes and objects.

Here is a good reference site: cplusplus.com/reference/
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: I am having a hard time grasping classes and virtual objects

Posted 07 February 2012 - 03:25 PM

View Postkillermunk1, on 07 February 2012 - 02:39 PM, said:

From what I can tell, you guys obviously misunderstood what I wanted (well, half of you). The example was there to tell you specifically what I am working with. From me saying "I do not know how to code these things". I was not saying that you should do my homework. I meant what the title says. I was not directly looking for the code for the example. I do not work all that well from reading my book because often times the book spreads what i need to know over about 40 pages. However reading it in a condensed form is what I needed to understand how classes and objects worked together and how to code them.


So give us the condensed version since that is what works for you... Did you get the answer you were looking for? I've read that post 5 times and still can't figure out if you did, or did not.
Was This Post Helpful? 0
  • +
  • -

#9 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 05-February 12

Re: I am having a hard time grasping classes and virtual objects

Posted 07 February 2012 - 03:29 PM

Yes, i did read it. It did help me understand how they work and how to code them my reply was about his first post, and Kajun160's post.

Also to tlhIn`toq. Your second post as I had said above did help me finishing the work. I ended up doing part B&C before part A ( the part i posted). They were set up differently but easier after completing them I finished part A.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1