Advantages to Inheritance?

Started by
3 comments, last by UraniumRod 24 years, 1 month ago
Does any one know of an advantage to using inheritance over just including the objects necessary for what you want in a new class. For example say I want a class that encryps data and then dumps it into a file I could do it this way: class EncryptFile : public Encryption, public FileIO { // blah }; or class EncryptFile { Encryption crypt; FileIO io; //blah }; Are there any advantages to doing it either way?
------------------------------"My sword is like a menacing cloud, but instead of rain, blood will pour in its path." - Sehabeddin, Turkish Military Commander 1438.
Advertisement
I''m sure a lot of long answers are coming, so I''ll give a short one:

Inheritance is great wherever you would otherwise use a function pointer.

Example: I''m writing a board game, and I want human and computer players. I write a base-class Player, with derived class HumanPlayer and ComputerPlayer. The basic data structure is in Player, but I overload methods in the derived classes. Player::rollDice () is pure-virtual (=0), so it doesn''t exist in the base class. ComputerPlayer overrides it to just pick two random numbers between 1 and 6, and HumanPlayer waits for the user to click on the two juggling dice. In my game loop, all I have is an array of Player*s--the game doesn''t have to know what kind of Player it''s dealing with, it just tells it to rollDice () and the right thing happens.

Another time you want to inherit is when you have an existing class that almost does everything you want, but you want to add some features or change some behaviors. This way, you can use it in place of the base class (i.e. in any functions that take your base class as a parameter) automatically, but now it does the functions the way you want.

This is not to say that including objects (composition, I believe it''s called) should be avoided. Quite the opposite. But there is a time & place for both methods.

Wow, that wasn''t so short, was it?
The main reason I use inheritance is for polymorphism. It''s great when you have very similiar classes with only a few minor differences.

--TheGoop
the semantic difference between inheritance and the other way can be described like this:

class B : public A

this means that class B ''IS A'' class A.

class B{    A my_a;}

this means that class B ''HAS A'' class A.


There is a fundamental difference in designing between the two.

Inheritance tends to imply that the class is the base, has the same exact interface, and should add features, or modify existing features.

Including a class says that you need to use the features of another class, but has some other, different, and often higher level function.

===============================================
I saw a man pursuing the horizon;
Round and round they sped. I was disturbed at this; I accosted the man.
"It is futile," I said, You can never -- "

"You lie," he cried, And ran on.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
Thanks guys!
------------------------------"My sword is like a menacing cloud, but instead of rain, blood will pour in its path." - Sehabeddin, Turkish Military Commander 1438.

This topic is closed to new replies.

Advertisement