| | |
inheritance and composition
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 32
Reputation:
Solved Threads: 0
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
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?
Let's say I have a class definition like this
C++ Syntax (Toggle Plain Text)
class Month { Week *week[4]; }; // class Month class Week { }; //class Week class Day : public Week { } // 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.
•
•
Join Date: Jun 2009
Posts: 53
Reputation:
Solved Threads: 4
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.
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 that confuses you, let me give you an example.
C++ Syntax (Toggle Plain Text)
class Car { //a car has four wheels. // a car has an engine } class Truck: public Car { //a truck ALSO has four wheels //a truck ALSO has an engine //a truck has a bed }
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.
•
•
Join Date: Jan 2009
Posts: 32
Reputation:
Solved Threads: 0
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
then the compiler will know that I will be calling the print() function for the Truck class.
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)
Car[0] = new Truck(); 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.
0
#5 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.
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.C++ Syntax (Toggle Plain Text)
class Car { //a car has four wheels. // a car has an engine } class Truck: public Car { //a truck ALSO has four wheels //a truck ALSO has an engine //a truck has a bed }
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.
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?•
•
Join Date: Jun 2009
Posts: 53
Reputation:
Solved Threads: 4
0
#6 29 Days Ago
•
•
•
•
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.
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.
0
#7 29 Days Ago
•
•
•
•
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.
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?![]() |
Similar Threads
- Inheritance and composition (C++)
- Combinations of Inheritance and Composition (C++)
- composition (C++)
- Composition (Java)
Other Threads in the C++ Forum
- Previous Thread: i do not understand this Q
- Next Thread: Binary Files! Test went poorly.
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






