C++ inheritance

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

Peon
Advertisement
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

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
quote:
isn''t it possible to use "super" to call the base constructor?

I believe that''s java
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 ]
"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
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!)
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.
Peon
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?

This topic is closed to new replies.

Advertisement