Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inheritance & constructors ??? 1

Status
Not open for further replies.

JANET1979

Vendor
Dec 3, 2002
13
GB
hi, here is my problem

Iam aware that a derived class is capable of not only using a constructor to initialise its own attributes but it is also able to send values to the base class constructor it has inherited from.

However what happens when a class (eg. animal) inherits from a base class (eg. organism) and then another class (eg.dolphin) inherits from the animal class?

When creating a dolphin its constructor needs to give values to its own attributes (dolphin), the animals attributes and also to the base class organism.

ORGANISM
ANIMAL
DOLPHIN

I know how to send attributes to the class immeadiately above it in the class hierachy, but i dont know how to use the constructor to send values to the class above that.

//example of constructors
oraganism::eek:rganism (int a, b)

animal::animal(int a, int b, int c):eek:rganism(a,b) //this i understand

dolphin::dolphin(int d,int e,int f,int g):animal(d,e) //and this

but how to send values from dolphin to both animal and organism???

could i put this on the end of the dolphin constructor the following? " : animal (d, e) : organism ( g ) "

hope its not to long winded... plz help or ask questions... cheers!

maybe its my design???
 
There's no way to do that, as far as I know.


But yes, if you have this problem, it is a problem with your design. You should never have a need to do that.

If Dolphin inherits from Animal, it should only need to call the Animal constructor.


That means that the designer of Animal (presumably you in this case) should expect it to be inherited from, and therefore provide a constructor that lets subclasses set anything they might need to set.

In other words, the class needs to be made usable for its intended purpose.


If you want, post an example of what you're trying to do and someone can show you a better way of designing your heirarchy.
 
could this be a suitable answer?

organism::eek:rganism(int a,int b)
animal::animal(int a, int b, int c):eek:rganism(a,b)
dolphin::dolphin(int a,int b,int c,int d):animal(a,b,c)

the 3 integers a,b,c are passed from the dolphin constructor to the animal constructor,which in turn passes a and b to the organism constructor...

 
firstly thank you chipperMDW for the help!

an additional question i need to clear up is :

i've introduced another class (CARNIVORE) into the inheritance heirachy
eg.
ORGANISM
ANIMAL
---> CARNIVORE
DOLPHIN

when writing my constructor code am i able to
skip passing the values through CARNIVORE, could
i simply send them straight through to ANIMAL ?

//code example
dolphin::dolphin(int a,int b,int c,int d):animal(a,b,c)
//as apposed to
dolphin::dolphin(int a,int b,int c,int d):carnivore(a,b,c)
carnivore::carnivore(int a,int b,int c):animal(a,b,c)

it seems logiacal, but i just want to be sure ???

THANKS!!!
 
//code example
dolphin::dolphin(int a,int b,int c,int d):animal(a,b,c)

It is not legal code, since animal is not a direct base class of dolphin(but carnivore is).
Imagine you change the body of carnivore's constructor to perform further initializations, then dolphin instances won't get properly intialized.
You should only use constructors of direct ancestors so that to at least ensure correct initialization of all inherited attributes.

--
Globos
 
That's the same question as before using different names. Just because there are four classes instead of three doesn't change the fact that you can't call constructors of non-direct base classes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top