inheritance and composition

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 32
Reputation: red999 is an unknown quantity at this point 
Solved Threads: 0
red999 red999 is offline Offline
Light Poster

inheritance and composition

 
0
  #1
29 Days Ago
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
  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; 29 Days Ago at 9:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 53
Reputation: chaines51 is an unknown quantity at this point 
Solved Threads: 4
chaines51 chaines51 is offline Offline
Junior Poster in Training
 
0
  #2
29 Days Ago
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 32
Reputation: red999 is an unknown quantity at this point 
Solved Threads: 0
red999 red999 is offline Offline
Light Poster
 
0
  #3
29 Days Ago
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
  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; 29 Days Ago at 12:37 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 53
Reputation: chaines51 is an unknown quantity at this point 
Solved Threads: 4
chaines51 chaines51 is offline Offline
Junior Poster in Training
 
0
  #4
29 Days Ago
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).
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,362
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 173
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #5
29 Days Ago
Originally Posted by chaines51 View Post
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.

  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.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 53
Reputation: chaines51 is an unknown quantity at this point 
Solved Threads: 4
chaines51 chaines51 is offline Offline
Junior Poster in Training
 
0
  #6
29 Days Ago
Originally Posted by firstPerson View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,362
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 173
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #7
29 Days Ago
Originally Posted by chaines51 View Post
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.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC