[   ^ to index...   |   <-- previous   |   next -->   ]

Details of inheritance

Memory layout

class Mammal { double milkProduction; };
double
milkProduction
class Feline : public Mammal { boolean purrs; };
double
milkProduction
boolean
purrs
class Canine : public Mammal { double barkVolume; };
double
milkProduction
double
barkVolume
class PitBull : public Canine { int peopleKilled; };
double
milkProduction
double
barkVolume
int
peopleKilled

What happens when you cast one type to another?

PitBull mauler; Canine pooch; pooch = mauler;

Answer: excess fields get chopped off. In the code above, the program forgets all about how many people mauler has killed.

Note that the reverse conversion (Canine to PitBull) doesn't make any sense (What value would we place in the peopleKilled field?) and is illegal. However, you can define such a conversion yourself using operator= if you really need it.


Last modified: Mon Jul 24 22:32:07 PDT 2000