[   ^ 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, and is illegal (though you can define it yourself using operator=).


Last modified: Mon Jul 24 22:18:44 PDT 2000