Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Inheritance

by

P.VASUKI
Aug 2017
Introduction
• Purpose: Code Reusability
• Defining new classes based on
existing class(es).
• Derived class access the properties
and functions of base class with
different access specifiers
• Inheritance maintain ‘is a’
relationship.
Objective
• Introduction
• Usage
• Different Kinds of Inheritance
Why Inheritance ?
Inheritance is a mechanism for
• building class types from existing class
types-Derive a new class (subclass) from an
existing class (base class or superclass).

• Inheritance creates a hierarchy of related classes


(types) which share code and interface.
• defining new class types to be a
– specialization
– augmentation
of existing types
Inheritance Example
LivingBeing

IS A IS A

Tree Bird

• Syntax:
class DerivedClassName : access-level
BaseClassName
class Tree : Public LivingBeing
Inheritance
Example
class Account{

protected: base class/ superclass/


int Balance
public:
parent class
int AccountNumber;

members goes to
derive from
int set (int a, int b);
};

class FD{
protected:
int interest;
int period; derived class/ subclass/
public:
child class
int set (int a, int b);
};
Inheritance Concept
x
class Point{
Point
y protected:
int x, y;
public:
Circle 3D-Point
int set (int a, int b);
x x };
y y
r z

class Circle : public Point{ class 3D-Point: public


private: Point{
double r; private:
}; int z;
};
7
Simple Inheritance

Person

is a

Employee
Multi Level Inheritance
• A Class may be derived from another
derived class
Person
Base

is a is a

Employee
Derived 1

is a is a

Derived 2 Manager
What to inherit?
• In principle, every member of a base
class is inherited by a derived class
– just with different access permission

• However, there are exceptions for


– constructor and destructor
– operator=() member
– friends
Since all these functions are class-
specific
Constructor/Destructor
• Derived Object calls Base class
default constructor and derived
constructor.
• While the object is destroyed the
derived class destructor is called,
then base class destructor invoked.
Example
class A { class B : public A
public: {
A() public:
{cout<< “A:default”<<endl;} B (int a)
A (int a) {cout<<“B”<<endl;}
};
{cout<<“A:parameter”<<endl;}
};
output: A:default
B test(1); B
Multiple Inheritance - Example
• A class derived from more than one
classes
Student Dancer

is a is a

StudentDancer

• class studentDancer : Public student, protected


Dancer
Virtual Inheritance

Person
As the Student Dancer get
Virtual Virtual the properties of Person Via
Student as well from
Student Dancer Dancer, there is an
ambiguity of two copies of
person data members to
avoid it
Both Student and Dancer
StudentD are declared as virtual
ancer classes
Summary
• Inheritance is a mechanism for
defining new class types to be a
specialization or an augmentation of
existing types.

• In principle, every member of a base


class is inherited by a derived class
with different access permissions,
except for the constructors

You might also like