SlideShare a Scribd company logo
Unit5 Inheritance
Presented By : Tekendra Nath Yogi
Tekendranath@gmail.com
College Of Applied Business And Technology
Content
– Introduction to inheritance
– Derived Class and Base Class,
– Access Specifiers (private, protected, and public),
– Public and Private Inheritance
– Types of inheritance(simple, multiple, hierarchical, multi-level, hybrid)
– Ambiguity in inheritance(ambiguity in multiple inheritance and ambiguity in
multipath inheritance)
– Abstract base class
– Constructor and Destructor in derived classes,
– Overriding member functions
– Aggregation (containership)
2By: Tekendra Nath Yogi
Introduction
• Inheritance is the process of creating new classes, called derived
classes, from existing or base classes.
• The derived class inherits all the capabilities of the base class but
can add embellishments and refinements of its own.
– (Like the child inheriting the features of its parents)
• The base class is unchanged by this process.
3By: Tekendra Nath Yogi
Contd..
• The inheritance relationship is shown in Figure below:
4By: Tekendra Nath Yogi
Contd..
• Inheritance permits code reusability.
– programmer can use a class created by another programmer
and, without modifying it, derive other classes from it that are
suited to particular situations.
– Reusing existing code:
• saves time and money and increases a program’s reliability.
• makes ease of distributing classes.
5By: Tekendra Nath Yogi
Why and when to use inheritance?
• Classes without inheritance
• This increases the chances of error and data redundancy. To
avoid this type of situation, inheritance is used.
6By: Tekendra Nath Yogi
Contd..
• Classes using inheritance
• avoid the duplication of data and increase re-usability.
7By: Tekendra Nath Yogi
Derived class and base class
• Inheritance is a technique of building new classes from the existing classes.
• The existing classes that are used to derive new classes are called base classes
and new classes derived from existing classes are called derived classes.
• When a class is derived from a base class, the derived class inherits all the
characteristics of base class and can add new features.
• Base class is also called ancestor, parent, or super class and derived class is
called as descendent, child or subclass.
• Because of inheritance, the base class is not changed.
• The inheritance does not work in reverse order:
– The features in derived class can not be accessed by the base class object. However the
features from base class are accessible in derived class.
8By: Tekendra Nath Yogi
Contd..
• A derived class is specified by defining its relationship with the base class in
addition to its own details.
• The general syntax of defining a derived class is as follows:
• The colon(:) indicates that derived_class_name class is derived from the
base_class_name class.
• The access specifiers or the visibility mode is optional and, if present can be
public, private or protected. By default it is private. Visibility mode, describes
the accessibility status of derived features.
9By: Tekendra Nath Yogi
Contd..
• Following are some possible derived class declarations:
10By: Tekendra Nath Yogi
Contd..
• In any of the above inheritance, private data members of the base class can
not be accessed by any of the derived classes.
11By: Tekendra Nath Yogi
Contd..
• A derived class inherits all base class methods with the following
exceptions −
– Constructors, and destructors of the base class.
– Overloaded operators of the base class.
– The friend functions of the base class.
12By: Tekendra Nath Yogi
Access specifiers
• Access specifiers are used to impose access restrictions on members of class
• so, It is used for the purpose of data hiding.
• Any member of a class is accessible within that class i.e. inside any member
function of same class.
• Types of Access Specifier
– Private
– Public
– protected
13By: Tekendra Nath Yogi
Contd..
• Syntax
class Base
{
public:
// public members go here
protected:
// protected members go here
private:
// private members go here
};
14By: Tekendra Nath Yogi
Contd..
15By: Tekendra Nath Yogi
Contd..
16By: Tekendra Nath Yogi
Visibility Modes of Inheritance
• Like the access restrictions imposed on members of class by
using access specifiers we can restrict the access of inheritated
members in derived class by using access specifier while
performing derivation.
• Three types of visibility modes of inheritance:
– Public mode
– Protected mode
– Private mode
17By: Tekendra Nath Yogi
Contd..
• Private mode:
– If we derive a sub class from a base class by using a private
qualifier.
– i.e.,
– Then both public member and protected members of the base class
will become Private in derived class.
18By: Tekendra Nath Yogi
Private: x
Protected: y
Public: z
X is not accessible
Private: y
Private: z
Base class members
Derivation mode is
private
Derived class member
Contd..
19By: Tekendra Nath Yogi
Contd..
20By: Tekendra Nath Yogi
Contd..
21By: Tekendra Nath Yogi
Contd..
• Protected mode:
– If we derive a sub class from a base class by using a protected
qualifier.
– i.e.,
– Then both public member and protected members of the base class
will become protected in derived class.
22By: Tekendra Nath Yogi
Private: x
Protected: y
Public: z
X is not accessible
Protected: y
protected: z
Base class members Derivation
mode is
protected
Derived class member
Contd..
23By: Tekendra Nath Yogi
Contd..
24By: Tekendra Nath Yogi
Contd..
25By: Tekendra Nath Yogi
Contd..
• Public mode:
– If we derive a sub class from a base class by using public qualifier.
• i.e.,
– Then the public member of the base class will become public in the
derived class and protected members of the base class will become
protected in derived class.
26By: Tekendra Nath Yogi
Private: x
Protected: y
Public: z
X is not accessible
Protected: y
Public: z
Base class members
Derivation
mode is public
Derived class member
Contd..
27By: Tekendra Nath Yogi
Contd..
28By: Tekendra Nath Yogi
Contd..
• The below table summarizes the above three modes and shows the access
specifier of the members of base class in the sub class when derived in public,
protected and private modes:
29By: Tekendra Nath Yogi
Type(forms) of inheritance
• A class can inherit properties from one or more classes and from one or more
levels.
• Based on the number of base classes and number of levels involved in the
inheritance can be categorized into the following forms:
– Single inheritance
– Multiple inheritance
– Hierarchical inheritance
– Multilevel inheritance
– Hybrid inheritance
30By: Tekendra Nath Yogi
Contd..
• Single inheritance: In this types of inheritance a derived class has only one
base class.
• Implementation skeleton:
31By: Tekendra Nath Yogi
Contd..
32By: Tekendra Nath Yogi
Contd..
33By: Tekendra Nath Yogi
Contd..
• Output:
34By: Tekendra Nath Yogi
Contd..
• Multiple inheritance: In this type of inheritance a single derived class may
inherit from two or more than two base classes.
• Implementation skeleton:
35By: Tekendra Nath Yogi
Contd..
36By: Tekendra Nath Yogi
Contd..
37By: Tekendra Nath Yogi
Contd..
38By: Tekendra Nath Yogi
Contd..
39By: Tekendra Nath Yogi
Contd..
• Hierarchical inheritance: In this type, two or more classes inherit the
properties of one base class.
• Implementation skeleton:
40By: Tekendra Nath Yogi
Contd..
41By: Tekendra Nath Yogi
Contd..
42By: Tekendra Nath Yogi
Contd..
43By: Tekendra Nath Yogi
Contd..
44By: Tekendra Nath Yogi
Contd..
• Multilevel Inheritance: The mechanism of deriving a class from another
derived class is known as multilevel inheritance. The process can be extended
to an arbitrary number of levels.
• Implementation skeleton:
45By: Tekendra Nath Yogi
Contd..
46By: Tekendra Nath Yogi
Contd..
47By: Tekendra Nath Yogi
Contd..
48By: Tekendra Nath Yogi
Contd..
49By: Tekendra Nath Yogi
Contd..
• Hybrid Inheritance: This type of inheritance includes more than one
type of inheritance.
• Implementation skeleton:
50By: Tekendra Nath Yogi
Contd..
51By: Tekendra Nath Yogi
Contd..
52By: Tekendra Nath Yogi
Contd..
53By: Tekendra Nath Yogi
Contd..
54By: Tekendra Nath Yogi
Contd..
55By: Tekendra Nath Yogi
Contd..
• Output:
56By: Tekendra Nath Yogi
Overriding member functions
• We can define the function in derived class having same name and signature
as that of base class which is called function overriding.
• It is called overriding because the new name overrides(hides or displaces) the
old name inherited from base class.
• In such situations derived class have two versions of same function one
derived from base class and another defined in the derived class itself.
• And if we call the overridden function by using the object of derived class
version of the method defined in the derived class is invoked.
• We can call the version of method derived from base class as follows:
57By: Tekendra Nath Yogi
Contd..
58By: Tekendra Nath Yogi
Constructors in derived classes
• Constructors play an important role in initializing objects.
• As long as no base class constructor takes any arguments, the derived class need
not have a constructor function.
• However, if any base class contains a constructor with one or more arguments ,
then it is mandatory for the derived class to pass arguments to the base class
constructor.
• Initial values are supplied to all classes when a derived class object is declared.
• 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.
• So, When object of a derived class is created, the constructor of the base class is
executed first and then the constructor of the derived class is executed next.
59By: Tekendra Nath Yogi
Contd..
60By: Tekendra Nath Yogi
Contd..
• If the base class contains no constructor, we can write the derived class
constructor as follows:
61By: Tekendra Nath Yogi
Contd..
• In case of multiple inheritance,
constructor in the base classes
are placed in the initializer list in
the derived class constructor
separated by commas.
• The order of the constructor
invocation is not as the order in
the initializer list but as the order
of the inherited base class
mentioned in derived class
declaration.
62By: Tekendra Nath Yogi
Contd..
• Order of execution of constructors:
– The base class constructor is executed first and then constructor in the derived
class is executed.
– In case of multiple inheritance, the base class constructors are executed in
which they appear in the definition of the derived class.
– similarly, in a multilevel inheritance, the constructors will be executed in the
order of inheritance. i.e., the constructor of the root base class is called at first
and the constructor of the lastly derived class is called sequentially at last.
– Furthermore, the constructors for virtual base classes are invoked before any
non-virtual base classes.
– If there are multiple virtual base classes, they are invoked in the order in
which they are declared in the derived class.
63By: Tekendra Nath Yogi
Contd..
64By: Tekendra Nath Yogi
Destructors in derived classes
• Destructor is a member function which destructs or deletes an object.
• Order of destructor invocation is just reverse order of constructor invocation.
65By: Tekendra Nath Yogi
Contd..
• Order of execution of destructors:
– The derived class destructor is executed first and then the destructor in the
base class is executed.
– In case of multiple inheritance, the destructor of derived class is called
first and the destructor of the base class which is mentioned at last in the
derived class declaration is called next and destructor of other classes from
last towards first is called sequentially and the destructor of the class
which is mentioned at first is called last..
– similarly, in a multilevel inheritance, the destructors will be executed in
the reverse order of inheritance.
66By: Tekendra Nath Yogi
Example:
67By: Tekendra Nath Yogi
Aggregation(Containership)
• Two types of class relationships:
– Aggregation and
– Inheritance
68By: Tekendra Nath Yogi
Contd..
• Inheritance:
– Inheritance is often called a “kind-of” or “is-a” relationship. In
inheritance If a class B is derived by inheritance from a class A, we can
say that “B is a kind of A.” This is because B has all the characteristics of
A, and in addition some of its own.
– E.g., we can say that bulldog is a kind of dog: A bulldog has the
characteristics shared by all dogs but has some distinctive characteristics
of its own.
69By: Tekendra Nath Yogi
Contd..
• Aggregation:
– This is another type of relationship, called a “has a” relationship, or
containership.
• We say a library has a book .
– Aggregation is also called a “part-whole” relationship: the book is part of
the library.
• In object-oriented programming, aggregation(has a relationship) may occur
when one object is an attribute of another. i.e., when one object is contained in
another.
70By: Tekendra Nath Yogi
Contd… Example
71By: Tekendra Nath Yogi
Contd..
72By: Tekendra Nath Yogi
Contd..
• OUTPUT:
73By: Tekendra Nath Yogi
Ambiguities in inheritance
• An ambiguity in inheritance arises in two situations:
– In case of multiple inheritance and
– In case multipath hybrid inheritance
74By: Tekendra Nath Yogi
Contd…
• Ambiguity in multiple inheritance:
– Suppose two base classes have an exactly similar member.
– Also, suppose a class derived from both of these classes has not this
member.
– Then, if we try to access this member from the objects of the derived class,
it will be ambiguous.
– We can remove this ambiguity by using the syntax:
Obj.class_name::methodname.
75By: Tekendra Nath Yogi
Contd..
76By: Tekendra Nath Yogi
Contd..
77By: Tekendra Nath Yogi
Output
Error will be:
Contd..
• We can also remove this ambiguity by adding a function in class C as follows:
78By: Tekendra Nath Yogi
Ambiguity in multi-path inheritance
• Another kind of ambiguity arises if you derive a class from two classes that
each derived from the same class as shown in figure below, which is called
multipath hybrid inheritance.
• In this case, public or protected member of grandparent is derived in the child
class twice which creates confusion to the compiler.
79By: Tekendra Nath Yogi
Contd..
• Consider the hybrid multipath inheritance as shown in figure below:
• All the public and protected members from class A inheritated into class D
twice:
– Once through the path A->B->D and
– Again through the path A->B->C
• This causes ambiguity and should be avoided.
80By: Tekendra Nath Yogi
Contd..
• We can remove this kind of ambiguity by declaring the base class(A) as virtual
while creating derived classes(B and C) from this class.
• After adding the keyword virtual while creating derived classes(B and C) it
ensures that only one copy of the properties of base class(A) is inherited in the
child class(D) which is derived from derived classes(B and C)
81By: Tekendra Nath Yogi
Contd..
• Note: The keywords virtual and public may be used in either order.
82By: Tekendra Nath Yogi
Contd.. example
83By: Tekendra Nath Yogi
Contd..
84By: Tekendra Nath Yogi
Contd..
85By: Tekendra Nath Yogi
Contd..
86By: Tekendra Nath Yogi
Contd..
87By: Tekendra Nath Yogi
Homework
• Differentiate between base class and derived class with suitable examples.
• Differentiate between private, public and protected variables with suitable example.
• Explain the role of inheritance in OOP. What is public, private and protected dentations?
Explain.
• Discuss a situation in which the private derivation will be more appropriate as compared to
public derivation.
• What is inheritance? Explain the types of inheritance with suitable examples.
• What is multilevel inheritance? How it differ from multiple inheritance?
• Define a shape class (with necessary constructors and member functions) in Object Oriented
Programming (abstract necessary attributes and their types). Write a complete code in C++
programming language.
– Derive triangle and rectangle classes from shape class adding necessary attributes.
– Use these classes in main function and display the area of triangle and rectangle.
88By: Tekendra Nath Yogi
Contd..
• 1
• 2
• 3
89By: Tekendra Nath Yogi
Contd..
• What is container class? Differentiate container class from inheritance.
• Differentiate between overloading and overriding.
• Differentiate abstract base class and concrete classes with suitable example.
• What are ambiguities in inheritance and how do you resolve those
ambiguities.
• What is the purpose of virtual base classes? Explain with suitable example.
90By: Tekendra Nath Yogi
Thank You !
91By: Tekendra Nath Yogi

More Related Content

What's hot

Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
Ravi Kant Sahu
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
Java Introduction
Java IntroductionJava Introduction
Java Introduction
sunmitraeducation
 
Penguat transistor
Penguat  transistorPenguat  transistor
Penguat transistor
Ahmad_Bagus
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
Muraleedhar Sundararajan
 
IGBT
IGBTIGBT
OOP Assignment 03.pdf
OOP Assignment 03.pdfOOP Assignment 03.pdf
OOP Assignment 03.pdf
ARSLANMEHMOOD47
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Harsh Patel
 
Modul praktikum rl2
Modul praktikum rl2Modul praktikum rl2
Modul praktikum rl2
heri santosa
 
Logic Families ( Digital Electronics )
Logic Families ( Digital Electronics )Logic Families ( Digital Electronics )
Logic Families ( Digital Electronics )
Yaksh Jethva
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
Laporan1 sr&d flip-flop_kurniawan suganda_1_nk1_14
Laporan1 sr&d flip-flop_kurniawan suganda_1_nk1_14Laporan1 sr&d flip-flop_kurniawan suganda_1_nk1_14
Laporan1 sr&d flip-flop_kurniawan suganda_1_nk1_14
Kurniawan Suganda
 
My review on low density parity check codes
My review on low density parity check codesMy review on low density parity check codes
My review on low density parity check codes
pulugurtha venkatesh
 
Computer science
Computer scienceComputer science
Computer science
DIGDARSHAN KUNWAR
 
Modular programming
Modular programmingModular programming
OOPS with C++ | Concepts of OOPS | Introduction
OOPS with C++ | Concepts of OOPS | IntroductionOOPS with C++ | Concepts of OOPS | Introduction
OOPS with C++ | Concepts of OOPS | Introduction
ADITYATANDONKECCSE
 
Introduction to VHDL
Introduction to VHDLIntroduction to VHDL
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit orderMalikireddy Bramhananda Reddy
 

What's hot (20)

Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Java Introduction
Java IntroductionJava Introduction
Java Introduction
 
Penguat transistor
Penguat  transistorPenguat  transistor
Penguat transistor
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
IGBT
IGBTIGBT
IGBT
 
OOP Assignment 03.pdf
OOP Assignment 03.pdfOOP Assignment 03.pdf
OOP Assignment 03.pdf
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Modul praktikum rl2
Modul praktikum rl2Modul praktikum rl2
Modul praktikum rl2
 
Logic Families ( Digital Electronics )
Logic Families ( Digital Electronics )Logic Families ( Digital Electronics )
Logic Families ( Digital Electronics )
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Laporan1 sr&d flip-flop_kurniawan suganda_1_nk1_14
Laporan1 sr&d flip-flop_kurniawan suganda_1_nk1_14Laporan1 sr&d flip-flop_kurniawan suganda_1_nk1_14
Laporan1 sr&d flip-flop_kurniawan suganda_1_nk1_14
 
My review on low density parity check codes
My review on low density parity check codesMy review on low density parity check codes
My review on low density parity check codes
 
Computer science
Computer scienceComputer science
Computer science
 
Modular programming
Modular programmingModular programming
Modular programming
 
OOPS with C++ | Concepts of OOPS | Introduction
OOPS with C++ | Concepts of OOPS | IntroductionOOPS with C++ | Concepts of OOPS | Introduction
OOPS with C++ | Concepts of OOPS | Introduction
 
Introduction to VHDL
Introduction to VHDLIntroduction to VHDL
Introduction to VHDL
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
 

Similar to B.sc CSIT 2nd semester C++ Unit5

Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Anil Kumar
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
Inheritance
Inheritance Inheritance
Inheritance
sourav verma
 
Object oriented programming new syllabus presentation
Object oriented programming new syllabus presentationObject oriented programming new syllabus presentation
Object oriented programming new syllabus presentation
iqraamjad1405
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Shweta Shah
 
B.sc CSIT 2nd semester C++ Unit3
B.sc CSIT  2nd semester C++ Unit3B.sc CSIT  2nd semester C++ Unit3
B.sc CSIT 2nd semester C++ Unit3
Tekendra Nath Yogi
 
Inheritance
InheritanceInheritance
Inheritance
Munsif Ullah
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
ChiradipBhattacharya
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
SystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfSystemVerilog_Classes.pdf
SystemVerilog_Classes.pdf
ssusere9cd04
 
INHERITANCES.pptx
INHERITANCES.pptxINHERITANCES.pptx
INHERITANCES.pptx
VISHNU PRIYA NR
 
Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1
Blue Elephant Consulting
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdf
SHASHIKANT346021
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
Vladislav Hadzhiyski
 
B.sc CSIT 2nd semester C++ Unit6
B.sc CSIT  2nd semester C++ Unit6B.sc CSIT  2nd semester C++ Unit6
B.sc CSIT 2nd semester C++ Unit6
Tekendra Nath Yogi
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
NAVANEETCHATURVEDI2
 
Basics of inheritance .22
Basics of inheritance .22Basics of inheritance .22
Basics of inheritance .22myrajendra
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
AteeqaKokab1
 

Similar to B.sc CSIT 2nd semester C++ Unit5 (20)

Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance
Inheritance Inheritance
Inheritance
 
Object oriented programming new syllabus presentation
Object oriented programming new syllabus presentationObject oriented programming new syllabus presentation
Object oriented programming new syllabus presentation
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
B.sc CSIT 2nd semester C++ Unit3
B.sc CSIT  2nd semester C++ Unit3B.sc CSIT  2nd semester C++ Unit3
B.sc CSIT 2nd semester C++ Unit3
 
Inheritance
InheritanceInheritance
Inheritance
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
SystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfSystemVerilog_Classes.pdf
SystemVerilog_Classes.pdf
 
INHERITANCES.pptx
INHERITANCES.pptxINHERITANCES.pptx
INHERITANCES.pptx
 
Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdf
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
 
B.sc CSIT 2nd semester C++ Unit6
B.sc CSIT  2nd semester C++ Unit6B.sc CSIT  2nd semester C++ Unit6
B.sc CSIT 2nd semester C++ Unit6
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Basics of inheritance .22
Basics of inheritance .22Basics of inheritance .22
Basics of inheritance .22
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 

More from Tekendra Nath Yogi

Unit9:Expert System
Unit9:Expert SystemUnit9:Expert System
Unit9:Expert System
Tekendra Nath Yogi
 
Unit7: Production System
Unit7: Production SystemUnit7: Production System
Unit7: Production System
Tekendra Nath Yogi
 
Unit8: Uncertainty in AI
Unit8: Uncertainty in AIUnit8: Uncertainty in AI
Unit8: Uncertainty in AI
Tekendra Nath Yogi
 
Unit5: Learning
Unit5: LearningUnit5: Learning
Unit5: Learning
Tekendra Nath Yogi
 
Unit4: Knowledge Representation
Unit4: Knowledge RepresentationUnit4: Knowledge Representation
Unit4: Knowledge Representation
Tekendra Nath Yogi
 
Unit3:Informed and Uninformed search
Unit3:Informed and Uninformed searchUnit3:Informed and Uninformed search
Unit3:Informed and Uninformed search
Tekendra Nath Yogi
 
Unit2: Agents and Environment
Unit2: Agents and EnvironmentUnit2: Agents and Environment
Unit2: Agents and Environment
Tekendra Nath Yogi
 
Unit1: Introduction to AI
Unit1: Introduction to AIUnit1: Introduction to AI
Unit1: Introduction to AI
Tekendra Nath Yogi
 
Unit 6: Application of AI
Unit 6: Application of AIUnit 6: Application of AI
Unit 6: Application of AI
Tekendra Nath Yogi
 
Unit10
Unit10Unit10
Unit9
Unit9Unit9
Unit8
Unit8Unit8
Unit7
Unit7Unit7
BIM Data Mining Unit5 by Tekendra Nath Yogi
 BIM Data Mining Unit5 by Tekendra Nath Yogi BIM Data Mining Unit5 by Tekendra Nath Yogi
BIM Data Mining Unit5 by Tekendra Nath Yogi
Tekendra Nath Yogi
 
BIM Data Mining Unit4 by Tekendra Nath Yogi
 BIM Data Mining Unit4 by Tekendra Nath Yogi BIM Data Mining Unit4 by Tekendra Nath Yogi
BIM Data Mining Unit4 by Tekendra Nath Yogi
Tekendra Nath Yogi
 
BIM Data Mining Unit3 by Tekendra Nath Yogi
 BIM Data Mining Unit3 by Tekendra Nath Yogi BIM Data Mining Unit3 by Tekendra Nath Yogi
BIM Data Mining Unit3 by Tekendra Nath Yogi
Tekendra Nath Yogi
 
BIM Data Mining Unit2 by Tekendra Nath Yogi
 BIM Data Mining Unit2 by Tekendra Nath Yogi BIM Data Mining Unit2 by Tekendra Nath Yogi
BIM Data Mining Unit2 by Tekendra Nath Yogi
Tekendra Nath Yogi
 
BIM Data Mining Unit1 by Tekendra Nath Yogi
 BIM Data Mining Unit1 by Tekendra Nath Yogi BIM Data Mining Unit1 by Tekendra Nath Yogi
BIM Data Mining Unit1 by Tekendra Nath Yogi
Tekendra Nath Yogi
 
Unit6
Unit6Unit6
B. SC CSIT Computer Graphics Unit 5 By Tekendra Nath Yogi
B. SC CSIT Computer Graphics Unit 5 By Tekendra Nath YogiB. SC CSIT Computer Graphics Unit 5 By Tekendra Nath Yogi
B. SC CSIT Computer Graphics Unit 5 By Tekendra Nath Yogi
Tekendra Nath Yogi
 

More from Tekendra Nath Yogi (20)

Unit9:Expert System
Unit9:Expert SystemUnit9:Expert System
Unit9:Expert System
 
Unit7: Production System
Unit7: Production SystemUnit7: Production System
Unit7: Production System
 
Unit8: Uncertainty in AI
Unit8: Uncertainty in AIUnit8: Uncertainty in AI
Unit8: Uncertainty in AI
 
Unit5: Learning
Unit5: LearningUnit5: Learning
Unit5: Learning
 
Unit4: Knowledge Representation
Unit4: Knowledge RepresentationUnit4: Knowledge Representation
Unit4: Knowledge Representation
 
Unit3:Informed and Uninformed search
Unit3:Informed and Uninformed searchUnit3:Informed and Uninformed search
Unit3:Informed and Uninformed search
 
Unit2: Agents and Environment
Unit2: Agents and EnvironmentUnit2: Agents and Environment
Unit2: Agents and Environment
 
Unit1: Introduction to AI
Unit1: Introduction to AIUnit1: Introduction to AI
Unit1: Introduction to AI
 
Unit 6: Application of AI
Unit 6: Application of AIUnit 6: Application of AI
Unit 6: Application of AI
 
Unit10
Unit10Unit10
Unit10
 
Unit9
Unit9Unit9
Unit9
 
Unit8
Unit8Unit8
Unit8
 
Unit7
Unit7Unit7
Unit7
 
BIM Data Mining Unit5 by Tekendra Nath Yogi
 BIM Data Mining Unit5 by Tekendra Nath Yogi BIM Data Mining Unit5 by Tekendra Nath Yogi
BIM Data Mining Unit5 by Tekendra Nath Yogi
 
BIM Data Mining Unit4 by Tekendra Nath Yogi
 BIM Data Mining Unit4 by Tekendra Nath Yogi BIM Data Mining Unit4 by Tekendra Nath Yogi
BIM Data Mining Unit4 by Tekendra Nath Yogi
 
BIM Data Mining Unit3 by Tekendra Nath Yogi
 BIM Data Mining Unit3 by Tekendra Nath Yogi BIM Data Mining Unit3 by Tekendra Nath Yogi
BIM Data Mining Unit3 by Tekendra Nath Yogi
 
BIM Data Mining Unit2 by Tekendra Nath Yogi
 BIM Data Mining Unit2 by Tekendra Nath Yogi BIM Data Mining Unit2 by Tekendra Nath Yogi
BIM Data Mining Unit2 by Tekendra Nath Yogi
 
BIM Data Mining Unit1 by Tekendra Nath Yogi
 BIM Data Mining Unit1 by Tekendra Nath Yogi BIM Data Mining Unit1 by Tekendra Nath Yogi
BIM Data Mining Unit1 by Tekendra Nath Yogi
 
Unit6
Unit6Unit6
Unit6
 
B. SC CSIT Computer Graphics Unit 5 By Tekendra Nath Yogi
B. SC CSIT Computer Graphics Unit 5 By Tekendra Nath YogiB. SC CSIT Computer Graphics Unit 5 By Tekendra Nath Yogi
B. SC CSIT Computer Graphics Unit 5 By Tekendra Nath Yogi
 

Recently uploaded

The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 

Recently uploaded (20)

The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 

B.sc CSIT 2nd semester C++ Unit5

  • 1. Unit5 Inheritance Presented By : Tekendra Nath Yogi Tekendranath@gmail.com College Of Applied Business And Technology
  • 2. Content – Introduction to inheritance – Derived Class and Base Class, – Access Specifiers (private, protected, and public), – Public and Private Inheritance – Types of inheritance(simple, multiple, hierarchical, multi-level, hybrid) – Ambiguity in inheritance(ambiguity in multiple inheritance and ambiguity in multipath inheritance) – Abstract base class – Constructor and Destructor in derived classes, – Overriding member functions – Aggregation (containership) 2By: Tekendra Nath Yogi
  • 3. Introduction • Inheritance is the process of creating new classes, called derived classes, from existing or base classes. • The derived class inherits all the capabilities of the base class but can add embellishments and refinements of its own. – (Like the child inheriting the features of its parents) • The base class is unchanged by this process. 3By: Tekendra Nath Yogi
  • 4. Contd.. • The inheritance relationship is shown in Figure below: 4By: Tekendra Nath Yogi
  • 5. Contd.. • Inheritance permits code reusability. – programmer can use a class created by another programmer and, without modifying it, derive other classes from it that are suited to particular situations. – Reusing existing code: • saves time and money and increases a program’s reliability. • makes ease of distributing classes. 5By: Tekendra Nath Yogi
  • 6. Why and when to use inheritance? • Classes without inheritance • This increases the chances of error and data redundancy. To avoid this type of situation, inheritance is used. 6By: Tekendra Nath Yogi
  • 7. Contd.. • Classes using inheritance • avoid the duplication of data and increase re-usability. 7By: Tekendra Nath Yogi
  • 8. Derived class and base class • Inheritance is a technique of building new classes from the existing classes. • The existing classes that are used to derive new classes are called base classes and new classes derived from existing classes are called derived classes. • When a class is derived from a base class, the derived class inherits all the characteristics of base class and can add new features. • Base class is also called ancestor, parent, or super class and derived class is called as descendent, child or subclass. • Because of inheritance, the base class is not changed. • The inheritance does not work in reverse order: – The features in derived class can not be accessed by the base class object. However the features from base class are accessible in derived class. 8By: Tekendra Nath Yogi
  • 9. Contd.. • A derived class is specified by defining its relationship with the base class in addition to its own details. • The general syntax of defining a derived class is as follows: • The colon(:) indicates that derived_class_name class is derived from the base_class_name class. • The access specifiers or the visibility mode is optional and, if present can be public, private or protected. By default it is private. Visibility mode, describes the accessibility status of derived features. 9By: Tekendra Nath Yogi
  • 10. Contd.. • Following are some possible derived class declarations: 10By: Tekendra Nath Yogi
  • 11. Contd.. • In any of the above inheritance, private data members of the base class can not be accessed by any of the derived classes. 11By: Tekendra Nath Yogi
  • 12. Contd.. • A derived class inherits all base class methods with the following exceptions − – Constructors, and destructors of the base class. – Overloaded operators of the base class. – The friend functions of the base class. 12By: Tekendra Nath Yogi
  • 13. Access specifiers • Access specifiers are used to impose access restrictions on members of class • so, It is used for the purpose of data hiding. • Any member of a class is accessible within that class i.e. inside any member function of same class. • Types of Access Specifier – Private – Public – protected 13By: Tekendra Nath Yogi
  • 14. Contd.. • Syntax class Base { public: // public members go here protected: // protected members go here private: // private members go here }; 14By: Tekendra Nath Yogi
  • 17. Visibility Modes of Inheritance • Like the access restrictions imposed on members of class by using access specifiers we can restrict the access of inheritated members in derived class by using access specifier while performing derivation. • Three types of visibility modes of inheritance: – Public mode – Protected mode – Private mode 17By: Tekendra Nath Yogi
  • 18. Contd.. • Private mode: – If we derive a sub class from a base class by using a private qualifier. – i.e., – Then both public member and protected members of the base class will become Private in derived class. 18By: Tekendra Nath Yogi Private: x Protected: y Public: z X is not accessible Private: y Private: z Base class members Derivation mode is private Derived class member
  • 22. Contd.. • Protected mode: – If we derive a sub class from a base class by using a protected qualifier. – i.e., – Then both public member and protected members of the base class will become protected in derived class. 22By: Tekendra Nath Yogi Private: x Protected: y Public: z X is not accessible Protected: y protected: z Base class members Derivation mode is protected Derived class member
  • 26. Contd.. • Public mode: – If we derive a sub class from a base class by using public qualifier. • i.e., – Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class. 26By: Tekendra Nath Yogi Private: x Protected: y Public: z X is not accessible Protected: y Public: z Base class members Derivation mode is public Derived class member
  • 29. Contd.. • The below table summarizes the above three modes and shows the access specifier of the members of base class in the sub class when derived in public, protected and private modes: 29By: Tekendra Nath Yogi
  • 30. Type(forms) of inheritance • A class can inherit properties from one or more classes and from one or more levels. • Based on the number of base classes and number of levels involved in the inheritance can be categorized into the following forms: – Single inheritance – Multiple inheritance – Hierarchical inheritance – Multilevel inheritance – Hybrid inheritance 30By: Tekendra Nath Yogi
  • 31. Contd.. • Single inheritance: In this types of inheritance a derived class has only one base class. • Implementation skeleton: 31By: Tekendra Nath Yogi
  • 35. Contd.. • Multiple inheritance: In this type of inheritance a single derived class may inherit from two or more than two base classes. • Implementation skeleton: 35By: Tekendra Nath Yogi
  • 40. Contd.. • Hierarchical inheritance: In this type, two or more classes inherit the properties of one base class. • Implementation skeleton: 40By: Tekendra Nath Yogi
  • 45. Contd.. • Multilevel Inheritance: The mechanism of deriving a class from another derived class is known as multilevel inheritance. The process can be extended to an arbitrary number of levels. • Implementation skeleton: 45By: Tekendra Nath Yogi
  • 50. Contd.. • Hybrid Inheritance: This type of inheritance includes more than one type of inheritance. • Implementation skeleton: 50By: Tekendra Nath Yogi
  • 57. Overriding member functions • We can define the function in derived class having same name and signature as that of base class which is called function overriding. • It is called overriding because the new name overrides(hides or displaces) the old name inherited from base class. • In such situations derived class have two versions of same function one derived from base class and another defined in the derived class itself. • And if we call the overridden function by using the object of derived class version of the method defined in the derived class is invoked. • We can call the version of method derived from base class as follows: 57By: Tekendra Nath Yogi
  • 59. Constructors in derived classes • Constructors play an important role in initializing objects. • As long as no base class constructor takes any arguments, the derived class need not have a constructor function. • However, if any base class contains a constructor with one or more arguments , then it is mandatory for the derived class to pass arguments to the base class constructor. • Initial values are supplied to all classes when a derived class object is declared. • 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. • So, When object of a derived class is created, the constructor of the base class is executed first and then the constructor of the derived class is executed next. 59By: Tekendra Nath Yogi
  • 61. Contd.. • If the base class contains no constructor, we can write the derived class constructor as follows: 61By: Tekendra Nath Yogi
  • 62. Contd.. • In case of multiple inheritance, constructor in the base classes are placed in the initializer list in the derived class constructor separated by commas. • The order of the constructor invocation is not as the order in the initializer list but as the order of the inherited base class mentioned in derived class declaration. 62By: Tekendra Nath Yogi
  • 63. Contd.. • Order of execution of constructors: – The base class constructor is executed first and then constructor in the derived class is executed. – In case of multiple inheritance, the base class constructors are executed in which they appear in the definition of the derived class. – similarly, in a multilevel inheritance, the constructors will be executed in the order of inheritance. i.e., the constructor of the root base class is called at first and the constructor of the lastly derived class is called sequentially at last. – Furthermore, the constructors for virtual base classes are invoked before any non-virtual base classes. – If there are multiple virtual base classes, they are invoked in the order in which they are declared in the derived class. 63By: Tekendra Nath Yogi
  • 65. Destructors in derived classes • Destructor is a member function which destructs or deletes an object. • Order of destructor invocation is just reverse order of constructor invocation. 65By: Tekendra Nath Yogi
  • 66. Contd.. • Order of execution of destructors: – The derived class destructor is executed first and then the destructor in the base class is executed. – In case of multiple inheritance, the destructor of derived class is called first and the destructor of the base class which is mentioned at last in the derived class declaration is called next and destructor of other classes from last towards first is called sequentially and the destructor of the class which is mentioned at first is called last.. – similarly, in a multilevel inheritance, the destructors will be executed in the reverse order of inheritance. 66By: Tekendra Nath Yogi
  • 68. Aggregation(Containership) • Two types of class relationships: – Aggregation and – Inheritance 68By: Tekendra Nath Yogi
  • 69. Contd.. • Inheritance: – Inheritance is often called a “kind-of” or “is-a” relationship. In inheritance If a class B is derived by inheritance from a class A, we can say that “B is a kind of A.” This is because B has all the characteristics of A, and in addition some of its own. – E.g., we can say that bulldog is a kind of dog: A bulldog has the characteristics shared by all dogs but has some distinctive characteristics of its own. 69By: Tekendra Nath Yogi
  • 70. Contd.. • Aggregation: – This is another type of relationship, called a “has a” relationship, or containership. • We say a library has a book . – Aggregation is also called a “part-whole” relationship: the book is part of the library. • In object-oriented programming, aggregation(has a relationship) may occur when one object is an attribute of another. i.e., when one object is contained in another. 70By: Tekendra Nath Yogi
  • 74. Ambiguities in inheritance • An ambiguity in inheritance arises in two situations: – In case of multiple inheritance and – In case multipath hybrid inheritance 74By: Tekendra Nath Yogi
  • 75. Contd… • Ambiguity in multiple inheritance: – Suppose two base classes have an exactly similar member. – Also, suppose a class derived from both of these classes has not this member. – Then, if we try to access this member from the objects of the derived class, it will be ambiguous. – We can remove this ambiguity by using the syntax: Obj.class_name::methodname. 75By: Tekendra Nath Yogi
  • 77. Contd.. 77By: Tekendra Nath Yogi Output Error will be:
  • 78. Contd.. • We can also remove this ambiguity by adding a function in class C as follows: 78By: Tekendra Nath Yogi
  • 79. Ambiguity in multi-path inheritance • Another kind of ambiguity arises if you derive a class from two classes that each derived from the same class as shown in figure below, which is called multipath hybrid inheritance. • In this case, public or protected member of grandparent is derived in the child class twice which creates confusion to the compiler. 79By: Tekendra Nath Yogi
  • 80. Contd.. • Consider the hybrid multipath inheritance as shown in figure below: • All the public and protected members from class A inheritated into class D twice: – Once through the path A->B->D and – Again through the path A->B->C • This causes ambiguity and should be avoided. 80By: Tekendra Nath Yogi
  • 81. Contd.. • We can remove this kind of ambiguity by declaring the base class(A) as virtual while creating derived classes(B and C) from this class. • After adding the keyword virtual while creating derived classes(B and C) it ensures that only one copy of the properties of base class(A) is inherited in the child class(D) which is derived from derived classes(B and C) 81By: Tekendra Nath Yogi
  • 82. Contd.. • Note: The keywords virtual and public may be used in either order. 82By: Tekendra Nath Yogi
  • 88. Homework • Differentiate between base class and derived class with suitable examples. • Differentiate between private, public and protected variables with suitable example. • Explain the role of inheritance in OOP. What is public, private and protected dentations? Explain. • Discuss a situation in which the private derivation will be more appropriate as compared to public derivation. • What is inheritance? Explain the types of inheritance with suitable examples. • What is multilevel inheritance? How it differ from multiple inheritance? • Define a shape class (with necessary constructors and member functions) in Object Oriented Programming (abstract necessary attributes and their types). Write a complete code in C++ programming language. – Derive triangle and rectangle classes from shape class adding necessary attributes. – Use these classes in main function and display the area of triangle and rectangle. 88By: Tekendra Nath Yogi
  • 89. Contd.. • 1 • 2 • 3 89By: Tekendra Nath Yogi
  • 90. Contd.. • What is container class? Differentiate container class from inheritance. • Differentiate between overloading and overriding. • Differentiate abstract base class and concrete classes with suitable example. • What are ambiguities in inheritance and how do you resolve those ambiguities. • What is the purpose of virtual base classes? Explain with suitable example. 90By: Tekendra Nath Yogi
  • 91. Thank You ! 91By: Tekendra Nath Yogi