C++ Question.

Discussion in 'Computers & Website Forum' started by Jodo Kast, Sep 10, 2004.

  1. Jodo Kast

    Jodo Kast
    Expand Collapse
    New Member

    Joined:
    Sep 4, 2004
    Messages:
    383
    Likes Received:
    0
    Anybody know of a good diagram, a very simple one that illustrates Classes and Inheritance. More particularly derived classes and how they interact with the public / private members of the base class. My new proffessor is from China, and I can barley understand what he says in class.
     
  2. Jodo Kast

    Jodo Kast
    Expand Collapse
    New Member

    Joined:
    Sep 4, 2004
    Messages:
    383
    Likes Received:
    0
    I see, fair enough. I will learn class inheritance myself. J/K.
     
  3. NaasPreacher (C4K)

    NaasPreacher (C4K)
    Expand Collapse
    Administrator
    Administrator

    Joined:
    Oct 21, 2003
    Messages:
    26,806
    Likes Received:
    78
    My problem is that I am so stupid I don't even know what "Classes and Inheritance" is (are?).
     
  4. natters

    natters
    Expand Collapse
    New Member

    Joined:
    Jul 23, 2004
    Messages:
    2,496
    Likes Received:
    0
    The relationship of derived classes to their base class is quite simple once you memorize these rules:

    - public members in the base class are inherited and visible to derived classes, and remain public (visible to all)

    - protected members in the base class are inherited and visible to derived classes, and remain protected (not visible outside the classes)

    - private members in the base class are not visible to derived classes

    Let me know if you need further detail or examples, or if you have any other questions.
     
  5. Jodo Kast

    Jodo Kast
    Expand Collapse
    New Member

    Joined:
    Sep 4, 2004
    Messages:
    383
    Likes Received:
    0
    That makes sense, I appreciate it much. I think I can handle it with a little practice, and applying the idea. once again I appreciate your help.
     
  6. JamesJ

    JamesJ
    Expand Collapse
    New Member

    Joined:
    May 9, 2002
    Messages:
    533
    Likes Received:
    0
    I think that this is right... Hope it helps.


    class CBeing
    {
    //Attributes
    BOOL Alive;
    long Type;
    short Age;

    private:
    int InitBaseClassAttributes(void);
    };

    class CCanine : public CBeing
    {
    short Breed;
    short Height;
    short Weight;
    };

    class CHuman : public CBeing
    {
    char Name[256];
    short Height;
    short Weight;
    char EyeColor;
    short MaritalStatus;
    };

    class CHumanMale : public CHuman
    {
    short SportsType[100];
    short TimesAnniversaryMissed;
    };

    class CHumanFemale : public CHuman
    {
    short NumberOfShoesInCloset;
    short TimesComplainedAboutSeatBeingUp;
    };

    CBeing Beings;
    CHumanMale Males[100];
    CHumanFemale Females[100];

    Beings.InitBaseClassAttributes(); // OK
    Males[0].InitBaseClassAttributes(); // fail: cannot call private member function from derived class

    strcpy(Males[0].Name, "Fred");
    Males[0].Height = 602; // 6' 2"
    Males[0].Age = 31;
    Males[0].Alive = TRUE;
    Males[0].SportsType[0] = 1; // Baseball
    Males[0].SportsType[1] = 3; // Hockey
    Males[0].SportsType[2] = 5; // NASCAR
    Males[0].TimesAnniversaryMissed = 0;

    strcpy(Females[0].Name, "Sally");
    Females[0].Height = 503; // 5' 3"
    Females[0].Age = 34;
    Females[0].Alive = TRUE;
    Females[0].NumberOfShoesInCloset = 8;
    Females[0].TimesComplainedAboutSeatBeingUp = 0;


    The instance "array of Males" inherits attributes from all of the other classes in the chain back to the root class CBeing. None of the attributes of CCanine are available (inherited by) CHuman, but CCanine still inherits attributes from CBeing. There seems to be a problem with the data in both of the instances (Males[0] and Females[0]). I can't believe that the last attribute entries could ever be zero!!
     
  7. natters

    natters
    Expand Collapse
    New Member

    Joined:
    Jul 23, 2004
    Messages:
    2,496
    Likes Received:
    0
    JamesJ,

    That's the general idea, except for one small but important mistake: classes default their members to private, so all your attributes are invisible to their derived classes. In your example above, before all your member variables you should have the "public:" or "protected:" keywords.

    Also (but this is unrelated to the initial issue of the relationship of derived classes with private/protected/public members of their parent classes), one of the reasons behind classes in the first place is to provide some encapsulation, keeping the internal implementation (i.e. the data representing the class) hidden away from direct manipulation, accesing them through member functions. This allows you to protect the integrety of the class's data, and will also allow you to change the types of member variables used without breaking any code that has already been written using that class. For example:

    </font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">class CBeing
    {
    protected: // main() cannot see these
    BOOL Alive;
    short Age;

    private:
    void InitBaseClassAttributes();

    public:
    CBeing();
    CBeing(BOOL init_alive, int init_age);
    virtual ~CBeing();

    void SetAlive(BOOL set_alive = TRUE);
    void SetAge(int set_age);

    BOOL GetAlive();
    int GetAge();
    };

    // constructor for default initialization
    CBeing::CBeing()
    {
    InitBaseClassAttributes();
    }

    CBeing::CBeing(BOOL init_alive, int init_age)
    {
    // start in a known state
    InitBaseClassAttributes();
    // then change to reflect desired init state
    SetAlive(init_alive);
    SetAge(init_age);
    }

    // private member function, call only be called from other member functions of this class
    void CBeing::InitBaseClassAttributes()
    {
    Alive = TRUE;
    Age = 0;
    }

    CBeing::~CBeing()
    {
    // nothing to do here yet
    }

    void SetAlive(BOOL set_alive /*= TRUE*/)
    {
    Alive = set_alive;
    }

    void SetAge(int set_age)
    {
    // disallow negative ages, protect the data
    if(set_age) &gt;= 0)
    Age = (int)set_age;
    }

    BOOL GetAlive()
    {
    return Alive;
    }

    int GetAge()
    {
    return (int)Age;
    }

    int main()
    {
    CBeing person; // defaults to alive and age of 0
    CBeing tree(FALSE, 89); // create a dead 89-year old tree

    person.SetAge(18);
    if(tree.GetAlive() == TRUE)
    {
    printf(&quot;The tree is alive!\n&quot;);
    }
    else
    {
    printf(&quot;The tree is dead!\n&quot;);
    }


    return 0;
    }</pre>[/QUOTE]In this example, if later on I decide to change the internal implementation of "Age" from a short to a float, I only have to change its declaration and a couple of typecasts in the member functions: no code (e.g. in main() or elsewhere) that is already written needs to change. Also note that it is now impossible to have a negative Age in a CBeing (we have protected our data from invalid conditions), because the variable is not accessible outside the class and because our member function prevents changing the Age if the new age is &lt; 0.

    Also, you should use constructors, destructors, as I have above. It's only beneficial. [​IMG]
     
  8. JamesJ

    JamesJ
    Expand Collapse
    New Member

    Joined:
    May 9, 2002
    Messages:
    533
    Likes Received:
    0
    Thanks natters. I was going for "very simple" as stated in the original request (just to show inheritance), but your example fleshes mine out nicely!!
     
  9. Jodo Kast

    Jodo Kast
    Expand Collapse
    New Member

    Joined:
    Sep 4, 2004
    Messages:
    383
    Likes Received:
    0
    Well I thank you both. I took the test and I feel like I did well on it. Thank you both.
     

Share This Page

Loading...