Archived

This topic is now archived and is closed to further replies.

Agape

C++ inheritance

Recommended Posts

Agape    122
Let's say I have a class Human and a class Boy that inherits Human. Human has variables age and name, and Boy has an extra variable, "height".
Human::Human(int a, string n)
{
    age = a;
    name = n;
}
  
How do I do the Boy's constructor so that it does everything that the Human constructor does--AND sets the "height" veriable? [edited by - agape on October 13, 2003 2:10:31 AM]

Share this post


Link to post
Share on other sites
RanBlade    731
I think something happened to your code snippet....

RanBlade
"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry." - Dave Pottinger, Ensemble Studios

[edited by - ranblade on October 13, 2003 2:10:30 AM]

Share this post


Link to post
Share on other sites
ArmitageIII87    228
Boy( int age, int name, int height):
Human(age, name),
m_Height( height )
{
Do other non-variable setting constructor stuff here.
}

I think this is what your trying to do. Essentially you want to call the base classes constructor if I understood correctly.

Share this post


Link to post
Share on other sites
RanBlade    731




RanBlade
"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios

[GameDev][C++ Page][Game Tutorials]

Share this post


Link to post
Share on other sites
Peon    276
Couldn''t you just code a NEW constructor for the boy object? I would just copy the duplicated code into the Boy constructor, and add the height there.

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by Peon
Couldn''t you just code a NEW constructor for the boy object? I would just copy the duplicated code into the Boy constructor, and add the height there.


Such a terribly bad idea would cost your C++ license.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Chacha    138
ArmitageIII87 is correct, but here is the full code example in case you don''t understand:


class Human
{
public:
Human(int a, string n): age(a), name(n) {};

private:
int age;
string name;
};

/////////////////////////////////////////////////////////////////


class Boy : public Human
{
public:
Boy(int a, string n, int h): Human(a, n), height(h) {};

private:
int height;
};

Share this post


Link to post
Share on other sites
SabreMan    504
quote:
Original post by Agape
Let''s say I have a class Human and a class Boy that inherits Human. Human has variables age and name, and Boy has an extra variable, "height".

That''s an interesting class design. What happens when a Boy reaches an age that he becomes a Man? C++ doesn''t let you change the type of an object!

Share this post


Link to post
Share on other sites
Lektrix    106
quote:
Original post by SabreMan
That's an interesting class design. What happens when a Boy reaches an age that he becomes a Man? C++ doesn't let you change the type of an object!


It could be worse; it could be:

class Girl : public Human;            

You'd get a "not humane" error.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on October 13, 2003 11:09:12 AM]

Share this post


Link to post
Share on other sites
Agape    122
quote:
Original post by chacha
ArmitageIII87 is correct, but here is the full code example in case you don''t understand:


class Human
{
public:
Human(int a, string n): age(a), name(n) {};

private:
int age;
string name;
};

/////////////////////////////////////////////////////////////////


class Boy : public Human
{
public:
Boy(int a, string n, int h): Human(a, n), height(h) {};

private:
int height;
};




That''s interesting...I''ve never seen that kind of syntax...then again, I''ve always defined my constructors in the cpp file and not in the header...Learn something new every day...

Now, just so I can end my confusion...isn''t it possible to use "super" to call the base constructor? Or am I mixing up with Java here?

Share this post


Link to post
Share on other sites
Peon    276
quote:
Original post by Fruny
quote:
Original post by Peon
Couldn''t you just code a NEW constructor for the boy object? I would just copy the duplicated code into the Boy constructor, and add the height there.


Such a terribly bad idea would cost your C++ license.

I''m confused; please elaborate.

Share this post


Link to post
Share on other sites
ThomasSauder    122
Because in big projects that can cost you space.
Because in big projects that can cause many errors.
Because in big projects that will cost you performance.

I''m taking a wild guess here

Plus, that''s why inheritance was created, to fix all those problems.

Cheers

Share this post


Link to post
Share on other sites
Robbo    122

Its because the base class constructor will initialise those variables anyway, so if you copy code in there, they get initialised twice. Another reason not to do it is you just plain got more code and more code = more bugs.

Anyway, personally, I think height should be in the Human class, not in the boy class.

Rememver, a boy is a human with the constraint that his age is under 52

Share this post


Link to post
Share on other sites
Fruny    1658
Couldn''t you just code a NEW constructor for the boy object? I would just copy the duplicated code into the Boy constructor, and add the height there.

I''m confused; please elaborate.

If you don''t explicitely call a base class constructor, the compiler will automatically insert a call to the base class default constructor. If this constructor doesn''t exist (like in the original poster''s example - he has defined a constructor with arguments, inhibiting the compiler-generated default constructor), your program will not compile.

Now, assuming you do have a default constructor, the base class members are likely to have been declared private. If so, the constructor in the derived class cannot access them - your program will not compile.

Assuming further that those members are accessible, you are doing twice as much work as is needed, first initialising the class in the base class default constructor and then *again* in the Boy constructor - which you said duplicates the base class constructor code. People often claim C++ classes are slow... but that fault lies on those who misuse the language, not the language itself.

Finally ... you have duplicated the base class code in the child class. Fine. But what if you then modify the base class ? You will have to go and modify *all* the class that derive from it to update their constructors. And that''s what object-oriented programming is intended to avoid. Imagine working in a large project, you would have to be informed of each and every change in the base class : "Oh, I changed the implementation class Foo again, if you are inheriting from it, please replace the corresponding code fragments in your class constructors with the following...".

That''s just insanity. Inheritance does mean you reuse your parent''s code directly - not that you manually duplicate it. That way, so long as the public interface of the class remains the same, you''re fine. Furthermore, the code for the base class might just not be available to you.


Anyway, here''s "the solution".

class Human
{
int age;
std::string name;
public:
Human( int a, const std::string& n );
};

Human::Human( int a, const std::string& n )
: age(a), name(n) // Initializer list - use them, love them.

{}

class Boy
{
int height;
public:
Boy( int a, const std::string& n, int h );
};

Boy::Boy( int a, const std::string& n, int h )
: Human(a,b), height(h)
{}


Thomas Sauder was basically right on every point.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
ThomasSauder    122
Yay. Fruny said I was right! It took me a while but I''m catching onto this C++ thing . Just have to dive into the STL.. But I think I''m going to create a text game before that.

But yea, Inheritance is a great tool that saves you time and boost your performance! Because say you have a gigantic Enemy class, holding everything (making it all virtual and protected) Then all you would have to do for the derived classes is access them and create only a couple functions. This would increase speed dramatically... I''m thinking.

Anyways, I''m still trying to learn so don''t take anything I say seriously, unless backed up by someone like Fruny

Cheers and Happy Thanksgiving! (to us canadians!)

Share this post


Link to post
Share on other sites
Peon    276
Awesome, thanks for the detailed clarification. I had been doing it my way before (copying the code) and was running into exactly the problems you described: having to change a bunch of derived classes if the base changed. My HD decided to destroy itself ridding myself of days of work, but perhaps it was a message telling me my design was poor I''ll have to keep this in mind when I rewrite it.

Share this post


Link to post
Share on other sites
Mr Teatime    122
So this may seem like an obvious question, but say you have a player class that inherits from a generic Character class. If you wanted to instantiate a Player object(or any other object that derives from the Character class) you always instantiate the base Character class object?

Share this post


Link to post
Share on other sites