Inheritance: constructors - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Inheritance: constructors

Description:

fleet[0] = new Train; fleet[1] = new Truck; fleet[0]- print(); fleet[1]- print ... Truck. 12. Multiple inheritance. A class may inherit from more than one base class. ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 13
Provided by: vana
Category:

less

Transcript and Presenter's Notes

Title: Inheritance: constructors


1
Inheritance 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.

2
Inheritance constructors
  • Example 1

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()
3
Inheritance constructors
  • Example 2

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)
4
Inheritance destructors
  • Example 3

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()
5
Inheritance 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.

6
Inheritance hiding methods
  • Example 1

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()
7
Inheritance hiding methods
  • Example 2

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()
8
Inheritance hiding methods
  • Example 3

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()
9
Inheritance hiding methods
  • Example 4

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)
10
Virtual 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
11
Virtual functions
  • Example

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
12
Multiple 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 ...
Write a Comment
User Comments (0)
About PowerShow.com