Inheritance
A mechanism for specialization
A mechanism for reuse
Fundamental to supporting polymorphism
68
BankAccount
class Account
{
protected:
double fBalance;
public:
Account( double aBalance );
};
void deposit( double aAmount );
void withdraw( double aAmount );
double getBalance();
class BankAccount: public Account
{
private:
double fInterestRate;
public:
BackAccount( double aRate );
};
void addInterest();
void chargeFee( double aAmount );
69
Constructors and Inheritance
Whenever an object of a derived class is instantiated,
multiple constructors are called so that each class in
the inheritance chain can initialize itself.
The constructor for each class in the inheritance
chain is called beginning with the base class at the
top of the inheritance chain and ending with the most
recent derived class.
70
Base Class Initializer
class BankAccount : public Accout
{
public:
BackAccount( double aRate ) :
Account( 0.0 ),
fInterestRate( aRate )
{}
};
71
Facts About Base Class Initializer
If a base class does not have a default constructor, the
derived class must provide a base class initializer for it.
Base class initializers frequently appear alongside member
initializers, which use similar syntax.
If more than one argument is required by a base class
constructor, the arguments are separated by comma.
Reference members need to be initialized using a member
initializer.
72
Destructors and Inheritance
Whenever an object of a derived class is destroyed,
the destructor for each class in the inheritance
chain, if defined, is called.
The destructor for each class in the inheritance chain
is called beginning with the most recent derived class
and ending with the base class at the top of the
inheritance chain.
73
Virtual Destructors
When deleting an object using a base class pointer
of reference, it is essential that the destructors for
each of the classes in the inheritance chain get a
chance to run:
BackAccount *BAptr;
Account *Aptr;
BAptr = new BackAccount(2.25);
Aptr = BAptr;
delete Aptr;
74
Virtual Account Destructor
Not declaring a destructor
virtual is a common source
for memory leaks!
class Account
{
public:
virtual ~Account() { }
};
75
Virtual Member Functions
To give a member function from a base class new
behavior in a derived class, one overrides it.
To allow a member function in a base class to be
overridden, one must declare the member function
virtual.
Note, in Java all member functions are virtual.
76
Virtual withdraw Method
class Account
{
public:
virtual void withdraw( double aAmount )
{
fBalance -= aAmount;
}
};
77
Overriding the withdraw Method
class BackAccount : public Account
{
public:
virtual void withdraw( double aAmount )
{
if ( !(fBalance - aAmount < 0.0) )
Account::withdraw( aAmount );
}
};
never use ==
base call
78
Calling a Virtual Method
BankAccount lBankAccount(2.25);
Account* Aptr = &lBackAccount;
Aptr->withdraw( 50.0 );
invokes BankAccount::withdraw
79
Facts About Virtual Members
Constructors cannot be virtual.
Declaring a member function virtual does not require that
this function must be overridden in derived classes, except
the member function is declared pure virtual.
Once a member function has been declared virtual, it remains
virtual.
Parameter and result types must match to properly override
a virtual member function.
If one declares a non-virtual member function virtual in a
derived class, the new member function hides the inherited
member function.
You can declare a private member function virtual to enable
polymorphism within the scope of the declaring class.
80
Abstract Base Classes
A class is abstract if it contains one or more pure
virtual member functions.
An abstract class cannot be instantiated.
Derived classes must provide definitions for the pure virtual
member functions, or declare them as pure virtual itself.
A pure virtual member function can be defined in the class
that declares it pure virtual.
If one declares a pure virtual destructor, a definition for it
must be given in the class that declares the destructor
pure virtual.
Abstract classes required a virtual destructor.
81
Pure Virtual Member Function
Now Account is abstract!
class Account
{
public:
virtual double estimateReturn() = 0;
};
82
Abstract Classes as Interfaces
An interface is a set of member functions.
Interfaces never define data types.
Interfaces never provide a default implementation
of methods.
A class implements an interface by defining all
member functions declared by the interface.
An abstract class can be viewed as an interface if
it only contains public pure virtual member
functions.
83
Interface TicTacToeView
class TicTacToeView
{
public:
virtual ~TicTacToeView() {}; // empty required destructor
virtual
virtual
virtual
virtual
void
void
void
void
registerGame( TicTacToe* aGame ) = 0;
printBoard() = 0;
draw() = 0;
declareWinner( int aPlayer ) = 0;
};
84
Access Level for Inheritance
public:
protected:
Public members in the base class remain public.
Protected members in the base class remain protected.
Yields a is a relationship.
Public and protected members in the base class are
protected in the derived class.
Yields a implemented in terms of relationship.
private:
Public and protected members in the base class become
private in the derived class.
Yields a stricter implemented in terms of relationship.
85
Multiple Inheritance
C++ allows a class to inherit from
several base classes at once.
Multiple inheritance is a mechanism to
compose orthogonal behavior.
86
CheckingAccount
class Account
{ };
class BackAccount : public Account
{ };
class WireAccount : public Account
{ };
class CheckingAccount : public BackAccount,
public WireAccount
{ };
87
Diamond Problem
class Account
fBalance
fBalance
class BankAccount
class WireAccount
BankAccount::fBalance
WireAccount::fBalance
class CheckingAccount
CheckingAccount inherits
Account twice!
88
Virtual Base Classes
A virtual base class allows multiple
instances of a base to be included in
object of a derived class.
Members of virtual base classes are
disambiguated by merging them into
one representative.
89
Virtual Account Class
class Account
{ };
class BackAccount : virtual public Account
{ };
class WireAccount : virtual public Account
{ };
class CheckingAccount : public BackAccount,
public WireAccount
{ };
90
How Virtual Base Classes Work
class Account
fBalance
fBalance
class BankAccount
class WireAccount
(Account::)fBalance
(Account::)fBalance
class CheckingAccount
fBalance
91