Design question for using derived class/ Multiple Base classes
-
I have a basic OOP question I have a Base class lets I'll call it B I am going to add functionality to this in addition I will need to use multiple copies of the Base class My question is What is the best programming approach CLass A : B { OR CLass A { B mybclass[10]; Thankx in advance
-
I have a basic OOP question I have a Base class lets I'll call it B I am going to add functionality to this in addition I will need to use multiple copies of the Base class My question is What is the best programming approach CLass A : B { OR CLass A { B mybclass[10]; Thankx in advance
ForNow wrote:
What is the best programming approach
No one can say that once and for all. The classical oO-analysis question would be: IS every A also a B in every respect? Or does it HAVE a B, that is, does it use a B to do its work.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening words -
I have a basic OOP question I have a Base class lets I'll call it B I am going to add functionality to this in addition I will need to use multiple copies of the Base class My question is What is the best programming approach CLass A : B { OR CLass A { B mybclass[10]; Thankx in advance
-
ForNow wrote:
My question is What is the best programming approach
The difference requires context that you have not supplied. Your question as posted is not possible to answer.
i don't understand what the question is?? if you have an Animal base class and you want to derive a Dog from it surely you would put
class Dog : public Animal { };
or is that not the question you're asking? i am only a learner myself but if you're asking is it right or wrong, no there is no answer whichever you're most comfortable with hth paulif ignorance is bliss then knock the smile off my face!!!
-
i don't understand what the question is?? if you have an Animal base class and you want to derive a Dog from it surely you would put
class Dog : public Animal { };
or is that not the question you're asking? i am only a learner myself but if you're asking is it right or wrong, no there is no answer whichever you're most comfortable with hth paulif ignorance is bliss then knock the smile off my face!!!
1slipperyfish wrote:
but if you're asking
I didn't ask anything so I have no idea what your post is supposed to mean for me.
1slipperyfish wrote:
no there is no answer whichever you're most comfortable with
If that is directed at the original question, "To inherit or to aggregate", it is not a valid answer.
-
1slipperyfish wrote:
but if you're asking
I didn't ask anything so I have no idea what your post is supposed to mean for me.
1slipperyfish wrote:
no there is no answer whichever you're most comfortable with
If that is directed at the original question, "To inherit or to aggregate", it is not a valid answer.
sorry led mike i thought you just posted and it joined the rest i wasn't posting to you :confused: why wouldn't you inherit? paul
if ignorance is bliss then knock the smile off my face!!!
-
1slipperyfish wrote:
but if you're asking
I didn't ask anything so I have no idea what your post is supposed to mean for me.
1slipperyfish wrote:
no there is no answer whichever you're most comfortable with
If that is directed at the original question, "To inherit or to aggregate", it is not a valid answer.
led mike wrote:
I didn't ask anything so I have no idea what your post is supposed to mean for me.
I chuckled at the irony (or is it coincidence) in the user name :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
led mike wrote:
I didn't ask anything so I have no idea what your post is supposed to mean for me.
I chuckled at the irony (or is it coincidence) in the user name :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
sorry led mike i thought you just posted and it joined the rest i wasn't posting to you :confused: why wouldn't you inherit? paul
if ignorance is bliss then knock the smile off my face!!!
1slipperyfish wrote:
why wouldn't you inherit?
You are just not following the thread. Your post used classes "Animal" and "Dog" which provide "context" and obviously a Dog "is a" Animal thereby indicating inheritance. However, the original poster used the classes "A" and "B" which provide no context and therefore we cannot distinguish between "is a" or "has a". By the way Mark wants to have lunch with you.
-
ForNow wrote:
What is the best programming approach
No one can say that once and for all. The classical oO-analysis question would be: IS every A also a B in every respect? Or does it HAVE a B, that is, does it use a B to do its work.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening words -
ForNow wrote:
My question is What is the best programming approach
The difference requires context that you have not supplied. Your question as posted is not possible to answer.
-
In that case, your second example (which is neither derived or multiple base classes). 'A' has a fleet (array) of cars but it is not a car. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
In that case, your second example (which is neither derived or multiple base classes). 'A' has a fleet (array) of cars but it is not a car. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
If it helps any: "Is ..." Use inheritance "Has ..." Add a member Example: given a base class fruit class apple - apple "is" a fruit so derive apple from fruit class fruitbasket - a fruit basket "has" fruit so add fruit (or an array of fruit) as a member of the fruitbasket class Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
If it helps any: "Is ..." Use inheritance "Has ..." Add a member Example: given a base class fruit class apple - apple "is" a fruit so derive apple from fruit class fruitbasket - a fruit basket "has" fruit so add fruit (or an array of fruit) as a member of the fruitbasket class Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I think I was looking at the other way ....a base has lets say 3 data members/methods.... If I want to use those but I need another method/dats member I will dervie the base and in the inherted class and add to it I think what you are saying it works the other way around the base has 10 data members/methods The drevid class only uses 7 data members/methods
-
I think I was looking at the other way ....a base has lets say 3 data members/methods.... If I want to use those but I need another method/dats member I will dervie the base and in the inherted class and add to it I think what you are saying it works the other way around the base has 10 data members/methods The drevid class only uses 7 data members/methods
Data members/methods and the amount of data members/methods isn't relevant to the class hierarchy. Those are only relevant to the class they're members of.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Data members/methods and the amount of data members/methods isn't relevant to the class hierarchy. Those are only relevant to the class they're members of.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I'll put another way I thought of the concept derived class(s) as a dervived class uses the base class .. but adds functionality Or Is it just the opposite way of thinking a base has all the functionality but derived class just picks the one it needs
ForNow wrote:
a dervived class uses the base class .. but adds functionality
A derived class IS the base class. The derived class can alter and/or add to the functionality of its base class. Again, the class hierarchy shouldn't be designed around the members. The members are whatever is necessary to implement the functionality of a class. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
ForNow wrote:
a dervived class uses the base class .. but adds functionality
A derived class IS the base class. The derived class can alter and/or add to the functionality of its base class. Again, the class hierarchy shouldn't be designed around the members. The members are whatever is necessary to implement the functionality of a class. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I understand I am going say something to see if I undertand what you mean a typical way of altering functionality would be to declare a method as Virtual that way the derived class can implement Its version of the method Or Since dervied members can have thier version of the Base('s) constructer that would be a way of altering the functionality
-
I understand I am going say something to see if I undertand what you mean a typical way of altering functionality would be to declare a method as Virtual that way the derived class can implement Its version of the method Or Since dervied members can have thier version of the Base('s) constructer that would be a way of altering the functionality
ForNow wrote:
a typical way of altering functionality would be to declare a method as Virtual that way the derived class can implement Its version of the method
Yes. That's polymorphism (through inheritance)
ForNow wrote:
Since dervied members can have thier version of the Base('s) constructer that would be a way of altering the functionality
Yes. Derived class constructors can also both alter and extend the base class constructor implementation, but it's not quite the same as overriding a virtual function. There's nothing virtual about constructors.
Mark Salsbery Microsoft MVP - Visual C++ :java: