Professional Documents
Culture Documents
INHERITANCE
SESSION : 14
INHERITANCE
Inheritance means deriving new
classes from the old ones.
The old class is called the base class
or parent class or super class and the
class which is derived from this base
class is called as derived class or
child class or sub class.
Deriving a new class from an existing
one , allows redefining a member
function of the base class and also
Relationships
Example:
M AM M ALS
DO G S C ATS H UM ANS
L IO N S T IG E R S LEO PARD S
Inheritance Relationship
Superclass
Is a class from which another class inherits a set of attributes
Subclass
Is a class that inherits a set of attributes from another class
types of inheritance
Single inheritance
Is displayed when a class inherits attributes from a single class
Multiple inheritance
Is displayed when a class inherits attributes from two or more
classes
TABLE OF DERIVATION AND ACCESS SPECIFIERS
Need for Generalization
Example:
Example:
Relationship between a driver and a
car
Instantiation Relationship
Example:
Relationship between a book and
Gone with the Wind
Uses of Inheritance
The derived class inherits all the properties of all its base classes.
public
protected
private
VIRTUAL BASE CLASSES
If the class derived contains only one
copy of the class base. This can be
done by making the base class a
virtual class.
This keyword makes the two classes
share a single copy of their base
class .
It can be done as follows :
class base
class Aclass : virtual public base
{
:
};
class Bclass : virtual public base
{:
};
class derived : public Aclass, public
Bclass
{