C++ inheritance

Started by
16 comments, last by Agape 20 years, 6 months ago
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]
Advertisement
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]

Eric Ranaldi a.k.a RanBlade


[size=1]"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...


[size=1]Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry."


[size=2]- Dave Pottinger, Ensemble Studios



[size=1][GameDev][C++ Page][Unity Game Engine][Panda3D Game Engine][NeHe Productions][Drunken Hyena][MSDN][Beej's Guide to Network Programming]


[size=1][FreedBSD][My Site][Gamasutra][Khan Acadamey]

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.



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]

Eric Ranaldi a.k.a RanBlade


[size=1]"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...


[size=1]Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry."


[size=2]- Dave Pottinger, Ensemble Studios



[size=1][GameDev][C++ Page][Unity Game Engine][Panda3D Game Engine][NeHe Productions][Drunken Hyena][MSDN][Beej's Guide to Network Programming]


[size=1][FreedBSD][My Site][Gamasutra][Khan Acadamey]

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.
Peon
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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;};
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!
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]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
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?

This topic is closed to new replies.

Advertisement