Title: Inheritance: constructors
1Inheritance constructors
- Constructors, copy constructors and destructors
are NOT inherited. - Each subclass has its own constructors and
destructor to handle the creation and destruction
of its own data members. - The superclass constructor initializes the data
members defined in the superclass - The subclass constructor calls the superclass
constructor (which initializes the inherited
members) and then goes on to initialize its own
data members. - The destructor works in a similar way, but in
reverse order.
2Inheritance constructors
class Base public Base() cout ltlt
BaseBase()\n class Derived public
Base public Derived() cout ltlt
DerivedDerived()\n int main ()
Derived obj return 0
Output BaseBase() DerivedDerived()
3Inheritance constructors
class Derived public Base public
Derived() cout ltlt DerivedDerived()\n
Derived(int x) Base(x)
cout ltlt DerivedDerived(x)\n
class Base public Base() cout
ltlt BaseBase()\n Base(int x)
cout ltlt BaseBase(x)\n int main
() Derived obj(10) return 0
Output BaseBase(x) DerivedDerived(x)
4Inheritance destructors
class Base public Base() Base()
cout ltlt BaseBase()\n class Derived
public Base public Derived()
Derived() cout ltlt DerivedDerived()\
n int main () Derived obj return
0
Output DerivedDerived() BaseBase()
5Inheritance hiding methods
- A derived class may use the base classs member
functions. - A derived class may also redefine the base
classs member functions. - In that case, the derived classs definition
hides the one in the base class.
6Inheritance hiding methods
class Base private public Base()
void print() cout ltlt "Baseprint()\n"
class Derived public Base
private public Derived() int main ()
Derived obj obj.print()
return 0
Output Baseprint()
7Inheritance hiding methods
class Base private public Base()
void print() cout ltlt "Baseprint()\n"
class Derived public Base
private public Derived() void print()
cout ltlt Derivedprint()\n" int main ()
Derived obj obj.print()
return 0
Output Derivedprint()
8Inheritance hiding methods
class Base private public Base()
void print() cout ltlt "Baseprint()\n"
void print(int x) cout ltlt Baseprint(x)\n
class Derived public Base
private public Derived() void print()
cout ltlt Derivedprint()\n" int main ()
Derived obj obj.print(10)
return 0
print() is overloaded
this definition hides BOTH Baseprints.
COMPILER ERROR! Baseprint(x) is hidden by
Derivedprint()
9Inheritance hiding methods
class Base private public Base()
void print() cout ltlt "Baseprint()\n"
void print(int x) cout ltlt Baseprint(x)\n
class Derived public Base
private public Derived() void print()
cout ltlt Derivedprint()\n" void print(int
x) cout ltlt Derivedprint(x)\n" int main
() Derived obj obj.print(10)
return 0
Output Derivedprint(x)
10Virtual functions
- Recall the class hierarchy
- We wish to create an array of pointers to various
subtypes of Vehicles. In addition, each member of
the array should behave according to each
subclass. - This can be achieved if every Vehicle member
function that is overriden in the subclasses is
declared as virtual.
Vehicle
Truck
Train
Bicycle
11Virtual functions
int main () Vehicle fleet2 fleet0
new Train fleet1 new Truck
fleet0-gtprint() fleet1-gtprint()
delete fleet0 delete fleet1 return
0
class Vehicle public Vehicle()
virtual void print() cout ltlt Vehicle\n cl
ass Train public Vehicle public Train()
virtual void print() cout ltlt
Train\n class Truck public Vehicle
public Truck() virtual void
print() cout ltlt Truck\n
Output Train Truck
12Multiple inheritance
- A class may inherit from more than one base
class. - BoatCar inherits the members of Car and those of
Boat. - BoatCar's constructor calls the constructors of
Car and Boat in the order specified in BoatCar's
definition (i.e. as they appear after the colon)
class Car ... class Boat ... class
BoatCar public Car, public Boat ...