You are on page 1of 23

SESSION : 14

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

Are specified among classes based


on the behavior of each class
Exist between classes because of
two reasons:
To indicate some sort of sharing
To indicate some kind of a connection

Are of four types:


Inheritance
Composition
Utilization
Instantiation
Inheritance
Relationship
Between classes or objects means an object or a class inherits a
set of attributes from another class

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

To create programs that are more


extensible

To provide source reusability


Generalization

Is done by clubbing the structure and


behavior that is common to all the
classes

Is represented by the super class

Is implemented by using the Abstract


class, which
Is not an existent entity
Is a base from which other classes inherit attributes
Composition Relationship

Occurs when one class is made up of


another

Example:

Relationship between a car and its


parts like engine, doors, steering
wheel, gear box, seats, and so on.
Utilization Relationship

Exists between two or more classes,


which make use of other classes

Example:
Relationship between a driver and a
car
Instantiation Relationship

Is a relationship between a class and


an instance of that class

Example:
Relationship between a book and
Gone with the Wind
Uses of Inheritance

Reduces redundancy in code

Enables easy maintenance of code

Extends the functionality of an


existing class by adding functions to
the subclass
Sequence of Invoking Constructors and
Destructors

Constructors are called in the order


of Base - to -Derived

Destructors are called in the order of


Derived - to -Base
Base Class Initialization

Is required before the member object


initialization of the derived class is
done
class Line : public Figure
{
Point p2;
public:
Line(int x1 = 0,int y1 = 0,int x2 =
0,int y2 = 0): p2(x2, y2), Figure(x1,
y1) {} //Member Initialization List
CONSTRUCTOR AND DESTRUCTOR FUNCTION WITH DERIVED CLASSES

If there are constructors involved in


the base class and the derived class,
the compiler automatically calls both
of them. This happens because as
soon as the derived class constructor
gets control and establishes formal
arguments, the base class
constructor will be called
immediately, i.e., before the derived
class initialization list is honored.
Syntax in the derived class
MULTILEVEL INHERITANCE

In multilevel inheritance there is a parent class , from whom


we derive another class .

from this derived class we can derive another class and so


on.
MULTIPLE INHERITANCE
Multiple inheritance , as the name suggests , is deriving a class from
more than one class .

The derived class inherits all the properties of all its base classes.

Consider the following example :


MULTIPLE INHERITANCE WITH A COMMON BASE (HYBRID
INHERITANCE)

There are many combinations in which inheritance can be put to


use. For instance, inheriting a class from two different classes, which
in turn have been derived from the same base class .
Access Specifiers of Derivation

Falls under the following category:

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
{

You might also like