Design question for using derived class/ Multiple Base classes
-
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:
-
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:
-
Thankx I think I am ready to code.... After being a Dinasour Assembler programmer for 30 years writting procedurel code its hard to change my way thinking
Have fun! I coded a few years in assembler and C before C++, but not 30 :) Thinking in terms of classes will be second-nature in no time. Cheers, MArk
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Have fun! I coded a few years in assembler and C before C++, but not 30 :) Thinking in terms of classes will be second-nature in no time. Cheers, MArk
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
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.
i thought that was what was happening and wondered why use A and B?? i am only a learner myself:-O i am on leave all week so any day suits me:-D paul
if ignorance is bliss then knock the smile off my face!!!
-
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
Puncuation of some sort would extremely help in getting your ideas across.
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 -
Puncuation of some sort would extremely help in getting your ideas across.
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 -
can i add a quetion on the end? i have a base class animalClass.hpp i have a derived Dog class and main in dogObj.cpp and a derived Cat class in catObj.cpp what i wanted to know is should i have animalClass.hpp dogClass.hpp, catClass.hpp, declaring the classes and have a seperate animalObj.cpp for main() to constrol the functions etc? at the moment my animalClass is about fifty lines as are the other animal classes, with my main () about 80 lines thanks in advance paul
if ignorance is bliss then knock the smile off my face!!!
-
can i add a quetion on the end? i have a base class animalClass.hpp i have a derived Dog class and main in dogObj.cpp and a derived Cat class in catObj.cpp what i wanted to know is should i have animalClass.hpp dogClass.hpp, catClass.hpp, declaring the classes and have a seperate animalObj.cpp for main() to constrol the functions etc? at the moment my animalClass is about fifty lines as are the other animal classes, with my main () about 80 lines thanks in advance paul
if ignorance is bliss then knock the smile off my face!!!
1slipperyfish wrote:
should i have animalClass.hpp dogClass.hpp, catClass.hpp, declaring the classes and have a seperate animalObj.cpp
you need header files (.h, .hpp) if you want to use the classes in other .cpp files so they can be #include < ...> If the class is defined in a cpp file the visibility of it is limited to (not sure) that file maybe.
-
1slipperyfish wrote:
should i have animalClass.hpp dogClass.hpp, catClass.hpp, declaring the classes and have a seperate animalObj.cpp
you need header files (.h, .hpp) if you want to use the classes in other .cpp files so they can be #include < ...> If the class is defined in a cpp file the visibility of it is limited to (not sure) that file maybe.
thanks for the reply led mike :D i have rewritten my classes into baseClass.hpp, CatClass.hpp, and DogClass.hpp with farms.cpp initialising the functions and having main() i had a lot of trouble compiling as it kept saying i had already declared my Mammal class in baseClass however if you add [code]#ifndef BASE_CLASS_HPP #define BASE_CLASS_HPP[/code] at the top of baseClass.hpp and [code]#endif[/code] at the bottom it works. you all probably already new that but i've just wasted 2 1/2 hours of my life learning that the hard way:D thanks again paul
if ignorance is bliss then knock the smile off my face!!!
-
can i add a quetion on the end? i have a base class animalClass.hpp i have a derived Dog class and main in dogObj.cpp and a derived Cat class in catObj.cpp what i wanted to know is should i have animalClass.hpp dogClass.hpp, catClass.hpp, declaring the classes and have a seperate animalObj.cpp for main() to constrol the functions etc? at the moment my animalClass is about fifty lines as are the other animal classes, with my main () about 80 lines thanks in advance paul
if ignorance is bliss then knock the smile off my face!!!
MAybe I shouldn't answer this after all I work as a MainFrame Assembler Dino programmer I think think if you declare it in the code/obj/cpp you just have to qualify the method with Class name e.g. DogClass :: dogmethod saying that this dogmethod is releated to the dogalass Class Trying to get into the future.....
-
MAybe I shouldn't answer this after all I work as a MainFrame Assembler Dino programmer I think think if you declare it in the code/obj/cpp you just have to qualify the method with Class name e.g. DogClass :: dogmethod saying that this dogmethod is releated to the dogalass Class Trying to get into the future.....
thanks paul
if ignorance is bliss then knock the smile off my face!!!