944,093 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 670
  • C++ RSS
Nov 11th, 2009
0

inheritance and composition

Expand Post »
I am write a program that both uses inheritance and composition. I am a bit new to inheritance so I am a little confused on some aspects of it.

Let's say I have a class definition like this
C++ Syntax (Toggle Plain Text)
  1. class Month
  2. {
  3. Week *week[4];
  4. }; // class Month
  5.  
  6. class Week
  7. {
  8. }; //class Week
  9.  
  10. class Day : public Week
  11. {
  12. } // class Day

I understand what it means if I create a Week object in week[0], but what if I create a Day object? Will week[0] be a Day object with Week characteristics? The classes in my program is composed of an array similar to this. Can I just do something like week[0] = new Day instead of week[0] = new Week to make it a Day object instead of what it was intended to be in my class definition?
Last edited by red999; Nov 11th, 2009 at 9:44 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
red999 is offline Offline
55 posts
since Jan 2009
Nov 11th, 2009
0
Re: inheritance and composition
To start off with, based on your example, I'm not sure you've got the general idea of inheritance. In general, you want to inherit from another class if the class you're building has all the attributes of the class you're inheriting from, plus some unique to this child class.
If that confuses you, let me give you an example.

C++ Syntax (Toggle Plain Text)
  1. class Car
  2. {
  3. //a car has four wheels.
  4. // a car has an engine
  5. }
  6.  
  7. class Truck: public Car
  8. {
  9. //a truck ALSO has four wheels
  10. //a truck ALSO has an engine
  11. //a truck has a bed
  12. }
so, you wouldn't want to inherit a Week object from a Day object, because you can't, in essence, say that a Day IS A Week.

On to your later question. If you were to say weeks[0] = new Day(); then yes, it would create a day object, but by accessing it through the weeks array, you could only access the attributes of the "week" portion of it that was inherited.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
chaines51 is offline Offline
54 posts
since Jun 2009
Nov 12th, 2009
0
Re: inheritance and composition
I kind of noticed my example was bad as I was typing it, but I went with it anyway =P. I'll just use your example since it is more clear. I'll like to add that I have an array *Car[8]
If I do Car[0] = new Truck(), how can I access the "Truck" portion if I do Car[0]?

I have two print() functions in both Car and Truck and both of which are virtual. I thought that if I do something like
C++ Syntax (Toggle Plain Text)
  1. Car[0] = new Truck();
  2. Car[0]->print();

then the compiler will know that I will be calling the print() function for the Truck class.
Last edited by red999; Nov 12th, 2009 at 12:37 am.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
red999 is offline Offline
55 posts
since Jan 2009
Nov 12th, 2009
0
Re: inheritance and composition
That is correct, if the function in the parent class is declared as virtual, it will be overridden by the child class's method of the same prototype, no matter how you access the child class (including your array example above).
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
chaines51 is offline Offline
54 posts
since Jun 2009
Nov 12th, 2009
0
Re: inheritance and composition
Click to Expand / Collapse  Quote originally posted by chaines51 ...
To start off with, based on your example, I'm not sure you've got the general idea of inheritance. In general, you want to inherit from another class if the class you're building has all the attributes of the class you're inheriting from, plus some unique to this child class.
If that confuses you, let me give you an example.

C++ Syntax (Toggle Plain Text)
  1. class Car
  2. {
  3. //a car has four wheels.
  4. // a car has an engine
  5. }
  6.  
  7. class Truck: public Car
  8. {
  9. //a truck ALSO has four wheels
  10. //a truck ALSO has an engine
  11. //a truck has a bed
  12. }
so, you wouldn't want to inherit a Week object from a Day object, because you can't, in essence, say that a Day IS A Week.

On to your later question. If you were to say weeks[0] = new Day(); then yes, it would create a day object, but by accessing it through the weeks array, you could only access the attributes of the "week" portion of it that was inherited.
If you are going to critique his work, then give a better example.
Car does not have a "is a" relationship to truck. Instead you should
have a abstract base class and have both of them inherit from it.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 12th, 2009
0
Re: inheritance and composition
If you are going to critique his work, then give a better example.
Car does not have a "is a" relationship to truck. Instead you should
have a abstract base class and have both of them inherit from it.
I disagree, I was using car AS a base class, seeing as the question "what kind of car do you drive?" would apply, regardless of whether or not you drove a truck, or an SUV, or a minivan.

However, that's truly irrelevant to the point. I wasn't trying to 'critique' his work per say, I was trying to ensure he truly understood the idea of inheritance.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
chaines51 is offline Offline
54 posts
since Jun 2009
Nov 12th, 2009
0
Re: inheritance and composition
Click to Expand / Collapse  Quote originally posted by chaines51 ...
I disagree, I was using car AS a base class, seeing as the question "what kind of car do you drive?" would apply, regardless of whether or not you drove a truck, or an SUV, or a minivan.

However, that's truly irrelevant to the point. I wasn't trying to 'critique' his work per say, I was trying to ensure he truly understood the idea of inheritance.
Just because a car is composed of similar things that a truck is
composed of does not mean a truck is a car( or the other way around).
It however suggests that they are composed of similar things, thus
each of their class should inherit from a base class that encapsulate
those similarities.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: i do not understand this Q
Next Thread in C++ Forum Timeline: Binary Files! Test went poorly.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC