SlideShare a Scribd company logo
1 of 49
Inheritance: Extending
Classes
Introduction




Reusability is an important feature of
OOP.
C++ strongly supports the concept
of reusability.
Introduction
continue …







The mechanism of deriving a new
class from an old one is called
inheritance (or derivation).
The old class is referred to as base
class.
The new class is called the derived
class or subclass.
Introduction
continue …





The derived class inherits some or all
of the traits from the base class.
A class can also inherit properties
from more than one class or from
more than one level.
Single Inheritance


A derived class with only one base
class.
A

B
Multiple Inheritance


A derived class with several base
classes.
A

B

C
Hierarchical Inheritance


A traits of one class may be inherited
by more than one class.
A

B

C

D
Multilevel Inheritance


The mechanism of deriving a class
from another derived class.
A
B
C
Hybrid Inheritance


The mechanism of deriving a class by
using a mixture of different methods.
A

B

C

D
Derived Classes


A derived class can be defined by
specifying its relationship with the
base class in addition to its own
details.

class derived-class-name : visibility-mode base-class-name
{
………//
………// members of derived class
………//
};
Derived Classes
continue …

class derived-class-name : visibility-mode base-class-name
The colon indicates that the
derived-class-name is derived
from the base-class-name

{
………//
………//
………//
};

members of derived class

The visibility mode is
optional and , if
present, may be either
private or public.
The default visibility
mode is private.
Visibility mode specifies
whether the features of
the base class are
derived privately or
publicly.
Derived Classes
continue …









When a base class is privately derived by a
derived class, “public members” of the base class
become “private members” of the derived class.
Therefore the members of the derived class can
only access the public members of the base class.
They are inaccessible to the objects of the
derived class.
No member of the base class is accessible to the
objects of the derived class.
Derived Classes
continue …





When a base class is publicly inherited,
”public members” of the base class
become the “public members” of the
derived class.
They are accessible to the objects of the
derived class.
Derived Classes
continue …





The private members of the base class are
not inherited in both the cases
(publicly/privately inherited).
The private members of a base class will
never become the members of its derived
class.
Inheritance








In inheritance, some of the base class data
elements and member functions are inherited into
the derived class.
We can add our own data and member functions
for extending the functionality of the base class.
It is a powerful tool for incremental program
development.
Can increase the capabilities of an existing class
without modifying it.
Single Inheritance

A

B
Making a Private Member Inheritable








By making the visibility limit of the private
members to the public.
The visibility modifier “protected” can be used for
this purpose.
A member declared as “protected” is accessible
by the member functions within its class and any
class immediately derived from it.
It can not be accessed by the functions outside
these two classes.
Making a Private Member Inheritable
continue …

class alpha
{
private : // optional
………
// visible to the member within its class
………
protected :
……… // visible to member functions
……… // of its own and immediate derived class
public :
……… // visible to all functions
……… // in the program
};
Protected Member
continue …







When a protected
member is inherited in
public mode, it
becomes protected in
the derived class.
They are accessible by
the member functions
of the derived class.
And they are ready for
further inheritance.

When a protected
member is inherited in
private mode, it
becomes private in the
derived class.
They are accessible by
the member functions
of the derived class.
But, they are not
available for further
inheritance.
Protected Derivation




It is possible to inherit a base class in
protected mode – protected derivation.
In protected derivation, both the public
and protected members of the base class
become protected members of the derived
class.
Effect of Inheritance on the visibility of Members
Class B

Private

Not inheritable

Not inheritable

Protected
Class D1 : public B

Public

Class D2 : private B

Private

Private

Protected

Protected

Public

Public
Class X : public D1, protected D2

Private
Protected
Public
Visibility
Base class
visibility
Private



Protected 
Public



Derived class visibility
Public
Derivation
Not
Inherited
Protected

Private
Derivation
Not
Inherited
Private

Protected
Derivation
Not
Inherited
Protected

Public

Private

Protected
Access Control to Data Members


Functions that can have access to the
private and protected members of a class:
• A function that is a friend of the class.
• A member function of a class that is a friend of
the class.
• A member function of a derived class.
Access mechanism in classes
class X
friend class Y:

fx1

private

data
protected

fx2

class Y

data

fy1
fy2
friend of X

class Z
Function 1
fz1
fz2
Inherited from X

friend of X
Multilevel Inheritance






The class A serves as a
base class for the derived
class B, which in turn
serves as a base class for
the derived class C.
Class B provides a link for
the inheritance between A
and C.
The chain ABC is known as
inheritance path.

Base Class

A

Intermediate
Base Class

B

Derived Class

C
Multilevel Inheritance
continue …

class A { ………} ;

// Base Class

class B : public A { ……… } ; // B derived from A
class C : public B { ……… } ; // C derived from B
Multiple Inheritance

B-2

Multiple inheritance
allows us to combine
the features of several
existing classes as a
starting point for
defining new classes.

D



A class can inherit the
attributes of two or
more classes.

B-1



It is like a child inheriting
the physical features of
one parent and the
intelligence of another.
Multiple Inheritance
continue …

class D: visibility B-1, visibility B-2, ……
{
………
……… (Body of D)
………
};



Where, visibility may be either public or private.
The base classes are separated by comma.
Ambiguity Resolution in Inheritance
class M
{
public:
void display (void)
{ cout << “Class M n“;}
};
class N
{
public:
void display (void)
{ cout << “Class N n“;}
};

class P : public M, public N
{
public:
void display (void)
{ M :: display();}
};
void main( )
{
P p;
p.display( );
}

In Multiple Inheritance
Ambiguity Resolution in Inheritance
continue …

class A
{
public:
void display (void)
{ cout << “Class A n“;}
};
class B : public A
{
public:
void display (void)
{ cout << “Class B n“;}
};

void main( )
{
B b;
b.display( ); // in B
b.A::display( ); // in A
b.B::display( ); // in B
}
Ambiguity can be resolved by
specifying the function with
class name and scope
resolution operator to invoke.

In Single Inheritance
Hierarchical Inheritance






Inheritance support
hierarchical design of a
program.
Additional members are
added through inheritance to
extend the capabilities of a
class.
Programming problems can
be cast into a hierarchy where
certain features of one level
are shared by many others
below that level

Account

SB

CA
FD

STD

LTD
MTD
Hybrid Inheritance


Applying Two or more
types of inheritance
together.

student

test

result

sports
Virtual Base Classes




Here the result class
has two direct base
classes test and sports
which themselves
have a common base
class student.
The result inherits the
traits of student via
two separate paths.

student

test

sports

result
Virtual Base Classes
continue …





It can also inherit
directly as shown by
the broken line.
The student class is
referred to as indirect
base class.

student

test

sports

result
Virtual Base Classes
continue …





All the public and
protected members of
student are inherited
into result twice, first
via test and again via
sports.
This means result
class have duplicate
set of members
inherited from
student.

student

test

sports

result
Virtual Base Classes
continue …

class student
{
………
};
class test : virtual public student
{
………
};
class sports : public virtual student
{
………
};
class result : public test, public sports
{
………
};

student

test

sports

result
Abstract Classes






An abstract class is one that is not used to create
objects.
An abstract class is designed only to act as a base
class.
It is a design concept in program development
and provides a base upon which other classes
may be built.
Constructors in Derived Classes




If no base class constructor takes any arguments,
the derived class need not have a constructor
function.
If any base class contains a constructor with one
or more arguments, then it is mandatory for the
derived class to have a constructor and pass the
arguments to the base class constructors.
Constructors in Derived Classes
continue …





When both the derived and base class contain
constructors, the base constructor is executed
first and then the constructor in the derived class
is executed.
In case of multiple inheritance, the base class
constructors are executed in the order in which
they appear in the declaration of the derived
class.
Constructors in Derived Classes
continue …





In a multilevel inheritance, the constructors will
be executed in the order of inheritance.
Since the derived class takes the responsibility of
supplying initial values to its base classes, we
supply the initial values that are required by all
the classes together, when a derived class object
is declared.
Constructors in Derived Classes
continue …





The constructor of the derived class receives the
entire list of values as its arguments and passes
them on to the base constructors in the order in
which they are declared in the derived class.
The base constructors are called and executed
before executing the statements in the body of
the derived constructor.
Constructors in Derived Classes
continue …



The header line of derived-constructor
function contains two parts separated by
a colon (:).
• The first part provides the declaration of the
arguments that are passed to the derived
constructor.
• The second part lists the function calls to the
base constructors.
Defining Derived Constructors
continue …

Derived-constructor(Arglist1, Arglist2, … ArglistN,
ArglistD) :
base1(arglist1),
base2(arglist2),
…
baseN(arglistN)
{
}
Member Classes : Nesting of Classes




Inheritance is the mechanism of deriving
certain properties of one class into
another.
C++ supports a new way of inheriting
classes:
• An object can be collection of many other
objects.
• A class can contain objects of other classes
as its members.
Member Classes : Nesting of Classes
continue …

class alpha { ……… };
class beta { ……… };
class gamma
{
alpha a;

// an object of class alpha

beta b; // an object of class beta
………
};
Member Classes : Nesting of Classes
continue …

class alpha { ……… };
class beta { ……… };
class gamma
{
alpha a;
beta b;
………
};

All objects of gamma
class will contain
the objects a and
b.
This is called
containership or
nesting.
Member Classes : Nesting of Classes
continue …





An independent object is created by its
constructor when it is declared with arguments.
A nested object is created in two stages:
• The member objects are created using their
respective constructors.
• Then ordinary members are created.



Constructors of all the member objects should
be called before its own constructor body is
executed.
Member Classes : Nesting of Classes
continue …

class gamma
{
………
alpha a;
beta b;
public:
gamma(arglist): alpha(arglist1), beta(arglist2)

{
};

body of the constructor

}
Thank You

Learners Support Publications
www.lsp4you.com

More Related Content

What's hot

Inheritance
InheritanceInheritance
InheritanceTech_MX
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1Mirza Hussain
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Gajendra Singh Thakur
 
Introduction to Inheritance
Introduction to InheritanceIntroduction to Inheritance
Introduction to InheritanceKeshav Vaswani
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Paumil Patel
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...cprogrammings
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++Nikunj Patel
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritanceadil raja
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 

What's hot (20)

Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
 
Inheritance
InheritanceInheritance
Inheritance
 
OOP Inheritance
OOP InheritanceOOP Inheritance
OOP Inheritance
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Introduction to Inheritance
Introduction to InheritanceIntroduction to Inheritance
Introduction to Inheritance
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Single inheritance
Single inheritanceSingle inheritance
Single inheritance
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 

Viewers also liked

abitha-pds inheritance presentation
abitha-pds inheritance presentationabitha-pds inheritance presentation
abitha-pds inheritance presentationabitha ben
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1PRN USM
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritanceNurhanna Aziz
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Jenish Patel
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Learn By Watch
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
169 Ch 29_lecture_presentation
 169 Ch 29_lecture_presentation 169 Ch 29_lecture_presentation
169 Ch 29_lecture_presentationgwrandall
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 

Viewers also liked (20)

Lecture4
Lecture4Lecture4
Lecture4
 
abitha-pds inheritance presentation
abitha-pds inheritance presentationabitha-pds inheritance presentation
abitha-pds inheritance presentation
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
Opps
OppsOpps
Opps
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
Inheritance
InheritanceInheritance
Inheritance
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
169 Ch 29_lecture_presentation
 169 Ch 29_lecture_presentation 169 Ch 29_lecture_presentation
169 Ch 29_lecture_presentation
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Ntroduction to computer architecture and organization
Ntroduction to computer architecture and organizationNtroduction to computer architecture and organization
Ntroduction to computer architecture and organization
 
Inheritance
InheritanceInheritance
Inheritance
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 

Similar to Inheritance (20)

Inheritance
InheritanceInheritance
Inheritance
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritence
inheritenceinheritence
inheritence
 
Inheritance
Inheritance Inheritance
Inheritance
 
E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Programming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptxProgramming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptx
 
inheritance
inheritanceinheritance
inheritance
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 

Recently uploaded

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 

Recently uploaded (20)

LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 

Inheritance

  • 2. Introduction   Reusability is an important feature of OOP. C++ strongly supports the concept of reusability.
  • 3. Introduction continue …    The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is referred to as base class. The new class is called the derived class or subclass.
  • 4. Introduction continue …   The derived class inherits some or all of the traits from the base class. A class can also inherit properties from more than one class or from more than one level.
  • 5. Single Inheritance  A derived class with only one base class. A B
  • 6. Multiple Inheritance  A derived class with several base classes. A B C
  • 7. Hierarchical Inheritance  A traits of one class may be inherited by more than one class. A B C D
  • 8. Multilevel Inheritance  The mechanism of deriving a class from another derived class. A B C
  • 9. Hybrid Inheritance  The mechanism of deriving a class by using a mixture of different methods. A B C D
  • 10. Derived Classes  A derived class can be defined by specifying its relationship with the base class in addition to its own details. class derived-class-name : visibility-mode base-class-name { ………// ………// members of derived class ………// };
  • 11. Derived Classes continue … class derived-class-name : visibility-mode base-class-name The colon indicates that the derived-class-name is derived from the base-class-name { ………// ………// ………// }; members of derived class The visibility mode is optional and , if present, may be either private or public. The default visibility mode is private. Visibility mode specifies whether the features of the base class are derived privately or publicly.
  • 12. Derived Classes continue …     When a base class is privately derived by a derived class, “public members” of the base class become “private members” of the derived class. Therefore the members of the derived class can only access the public members of the base class. They are inaccessible to the objects of the derived class. No member of the base class is accessible to the objects of the derived class.
  • 13. Derived Classes continue …   When a base class is publicly inherited, ”public members” of the base class become the “public members” of the derived class. They are accessible to the objects of the derived class.
  • 14. Derived Classes continue …   The private members of the base class are not inherited in both the cases (publicly/privately inherited). The private members of a base class will never become the members of its derived class.
  • 15. Inheritance     In inheritance, some of the base class data elements and member functions are inherited into the derived class. We can add our own data and member functions for extending the functionality of the base class. It is a powerful tool for incremental program development. Can increase the capabilities of an existing class without modifying it.
  • 17. Making a Private Member Inheritable     By making the visibility limit of the private members to the public. The visibility modifier “protected” can be used for this purpose. A member declared as “protected” is accessible by the member functions within its class and any class immediately derived from it. It can not be accessed by the functions outside these two classes.
  • 18. Making a Private Member Inheritable continue … class alpha { private : // optional ……… // visible to the member within its class ……… protected : ……… // visible to member functions ……… // of its own and immediate derived class public : ……… // visible to all functions ……… // in the program };
  • 19. Protected Member continue …    When a protected member is inherited in public mode, it becomes protected in the derived class. They are accessible by the member functions of the derived class. And they are ready for further inheritance. When a protected member is inherited in private mode, it becomes private in the derived class. They are accessible by the member functions of the derived class. But, they are not available for further inheritance.
  • 20. Protected Derivation   It is possible to inherit a base class in protected mode – protected derivation. In protected derivation, both the public and protected members of the base class become protected members of the derived class.
  • 21. Effect of Inheritance on the visibility of Members Class B Private Not inheritable Not inheritable Protected Class D1 : public B Public Class D2 : private B Private Private Protected Protected Public Public Class X : public D1, protected D2 Private Protected Public
  • 22. Visibility Base class visibility Private  Protected  Public  Derived class visibility Public Derivation Not Inherited Protected Private Derivation Not Inherited Private Protected Derivation Not Inherited Protected Public Private Protected
  • 23. Access Control to Data Members  Functions that can have access to the private and protected members of a class: • A function that is a friend of the class. • A member function of a class that is a friend of the class. • A member function of a derived class.
  • 24. Access mechanism in classes class X friend class Y: fx1 private data protected fx2 class Y data fy1 fy2 friend of X class Z Function 1 fz1 fz2 Inherited from X friend of X
  • 25. Multilevel Inheritance    The class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. Class B provides a link for the inheritance between A and C. The chain ABC is known as inheritance path. Base Class A Intermediate Base Class B Derived Class C
  • 26. Multilevel Inheritance continue … class A { ………} ; // Base Class class B : public A { ……… } ; // B derived from A class C : public B { ……… } ; // C derived from B
  • 27. Multiple Inheritance B-2 Multiple inheritance allows us to combine the features of several existing classes as a starting point for defining new classes. D  A class can inherit the attributes of two or more classes. B-1  It is like a child inheriting the physical features of one parent and the intelligence of another.
  • 28. Multiple Inheritance continue … class D: visibility B-1, visibility B-2, …… { ……… ……… (Body of D) ……… };   Where, visibility may be either public or private. The base classes are separated by comma.
  • 29. Ambiguity Resolution in Inheritance class M { public: void display (void) { cout << “Class M n“;} }; class N { public: void display (void) { cout << “Class N n“;} }; class P : public M, public N { public: void display (void) { M :: display();} }; void main( ) { P p; p.display( ); } In Multiple Inheritance
  • 30. Ambiguity Resolution in Inheritance continue … class A { public: void display (void) { cout << “Class A n“;} }; class B : public A { public: void display (void) { cout << “Class B n“;} }; void main( ) { B b; b.display( ); // in B b.A::display( ); // in A b.B::display( ); // in B } Ambiguity can be resolved by specifying the function with class name and scope resolution operator to invoke. In Single Inheritance
  • 31. Hierarchical Inheritance    Inheritance support hierarchical design of a program. Additional members are added through inheritance to extend the capabilities of a class. Programming problems can be cast into a hierarchy where certain features of one level are shared by many others below that level Account SB CA FD STD LTD MTD
  • 32. Hybrid Inheritance  Applying Two or more types of inheritance together. student test result sports
  • 33. Virtual Base Classes   Here the result class has two direct base classes test and sports which themselves have a common base class student. The result inherits the traits of student via two separate paths. student test sports result
  • 34. Virtual Base Classes continue …   It can also inherit directly as shown by the broken line. The student class is referred to as indirect base class. student test sports result
  • 35. Virtual Base Classes continue …   All the public and protected members of student are inherited into result twice, first via test and again via sports. This means result class have duplicate set of members inherited from student. student test sports result
  • 36. Virtual Base Classes continue … class student { ……… }; class test : virtual public student { ……… }; class sports : public virtual student { ……… }; class result : public test, public sports { ……… }; student test sports result
  • 37. Abstract Classes    An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class. It is a design concept in program development and provides a base upon which other classes may be built.
  • 38. Constructors in Derived Classes   If no base class constructor takes any arguments, the derived class need not have a constructor function. If any base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructors.
  • 39. Constructors in Derived Classes continue …   When both the derived and base class contain constructors, the base constructor is executed first and then the constructor in the derived class is executed. In case of multiple inheritance, the base class constructors are executed in the order in which they appear in the declaration of the derived class.
  • 40. Constructors in Derived Classes continue …   In a multilevel inheritance, the constructors will be executed in the order of inheritance. Since the derived class takes the responsibility of supplying initial values to its base classes, we supply the initial values that are required by all the classes together, when a derived class object is declared.
  • 41. Constructors in Derived Classes continue …   The constructor of the derived class receives the entire list of values as its arguments and passes them on to the base constructors in the order in which they are declared in the derived class. The base constructors are called and executed before executing the statements in the body of the derived constructor.
  • 42. Constructors in Derived Classes continue …  The header line of derived-constructor function contains two parts separated by a colon (:). • The first part provides the declaration of the arguments that are passed to the derived constructor. • The second part lists the function calls to the base constructors.
  • 43. Defining Derived Constructors continue … Derived-constructor(Arglist1, Arglist2, … ArglistN, ArglistD) : base1(arglist1), base2(arglist2), … baseN(arglistN) { }
  • 44. Member Classes : Nesting of Classes   Inheritance is the mechanism of deriving certain properties of one class into another. C++ supports a new way of inheriting classes: • An object can be collection of many other objects. • A class can contain objects of other classes as its members.
  • 45. Member Classes : Nesting of Classes continue … class alpha { ……… }; class beta { ……… }; class gamma { alpha a; // an object of class alpha beta b; // an object of class beta ……… };
  • 46. Member Classes : Nesting of Classes continue … class alpha { ……… }; class beta { ……… }; class gamma { alpha a; beta b; ……… }; All objects of gamma class will contain the objects a and b. This is called containership or nesting.
  • 47. Member Classes : Nesting of Classes continue …   An independent object is created by its constructor when it is declared with arguments. A nested object is created in two stages: • The member objects are created using their respective constructors. • Then ordinary members are created.  Constructors of all the member objects should be called before its own constructor body is executed.
  • 48. Member Classes : Nesting of Classes continue … class gamma { ……… alpha a; beta b; public: gamma(arglist): alpha(arglist1), beta(arglist2) { }; body of the constructor }
  • 49. Thank You Learners Support Publications www.lsp4you.com