Professional Documents
Culture Documents
Inheritance PDF
Inheritance PDF
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).
IS A IS A
Tree Bird
• Syntax:
class DerivedClassName : access-level
BaseClassName
class Tree : Public LivingBeing
Inheritance
Example
class Account{
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
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
is a is a
StudentDancer
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.