Jump to content
  • Advertisement

Archived

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

Agape

C++ inheritance

This topic is 5420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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]

Share this post


Link to post
Share on other sites
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
"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
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
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
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
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
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
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!