SlideShare ist ein Scribd-Unternehmen logo
1 von 60
Downloaden Sie, um offline zu lesen
INHERITANCE
Mrs. Pranali P. Chaudhari
Contents
 Types of inheritance with examples
 Constructors and Destructors in derived class
 Virtual Base Classes, Virtual function and Pure
Virtual function
 Abstract Base Classes
Inheritance
 The mechanism of deriving a new class from an old one
is called inheritance.
 C++ strongly supports the concept of reusability.
 Once the class has been written and tested, it can be
adapted by other programmers to suit their
requirements.
 The old class is referred to as the base class and the
new one is called the derived class or subclass.
Types of Inheritance
 Single Inheritance
 Multiple Inheritance
 Hierarchical Inheritance
 Multilevel Inheritance
 Hybrid Inheritance
Single Inheritance
 A derived class with only one base class is called
single inheritance.
Multiple Inheritance
 A derived class with more than one base classes is
called multiple inheritance.
Hierarchical Inheritance
 In hierarchical inheritance one base class is inherited
by more than one derived class.
Multilevel Inheritance
 The mechanism of deriving a class from another
derived class is known as multilevel inheritance.
Hybrid Inheritance
 A combination of any two above types of
inheritance is known as hybrid inheritance.
Defining Derived Classes
 A derived class can be defined by specifying its
relationship with the base class in addition to its own
details.
 General Syntax:
class derived_class_name : visibility-mode base_class_name
{
........... // members of derived class
...........
};
Defining Derived Classes
 The visibility mode is optional and may be private and
public.
 The default visibility mode is private.
 Visibility mode specifies whether the features of the
base class are privately derived or publicly derived.
 Example:
Class ABC : private | public XYZ
{
members of ABC
};
Visibility mode - Private
 When a base class is privately inherited by a
derived class, public members of the base class
become private members of the derived class.
 Members of base class is accessed by the member
functions of the derived class.
Visibility mode - Public
 When the base class is publicly inherited, public
members of the base class becomes public members of
the derived class.
 The members are accessible to the objects of derived
class.
 In both the cases, the private members are not
inherited.
Single Inheritance Example (Public)
 Single Inheritance ( public mode)
 The class D is a public derivation of the base class B. Thus inherits
all the public members of B and retains their visibility.
 The public member of class B is also a public member of the class
D.
 The private members are not inherited by D.
 The class D after inheritance will have more members than what it
contains at the time of declaration.
 Class D (publically inherited)
Single Inheritance Example (Private)
 Single inheritance ( private mode)
 In private derivation the public members of the base
class become private members of the derived class.
 Therefore the objects of D can not have direct access to
the public member functions of B.
 Class D (privately inherited)
Making private Member Inheritable
 C++ provides a third visibility modifier, protected,
which serve a limited purpose in inheritance.
 A member declared as protected is accessible by the
member functions within its class and any class
immediately derived from it.
 It cannot be accessed by the functions outside these two
classes.
Making private Member Inheritable
class alpha
{
private: // optional visible to
........... // member functions within its class
...........
protected: // visible to member functions of its own
............ // and derived class
............
public:
............ // visible to all functions in the program
............
};
Effect of Inheritance on the visibility of
members
Base Class (Protected Derivation)
 It is also possible to inherit a base class in protected
mode.
 Here, both the public and protected members of the
base class becomes protected members of the
derived class.
Practice Statement 1
 Write a program to prepare marksheet of the college
examination with the following items read from the
keyboard: Student name, roll number, subject name,
subject code, internal marks, external marks. Design the
base class consisting of data members such as student
name, roll no, subject name. The derived class consists of
members subject code, internal and external marks.
Multilevel Inheritance
 The method of deriving a class from another derived class is
known as 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.
 The class B is known as intermediate base class.
 Example: Suppose the test results of students are stored in
three different classes. Class student stores roll number, class
test stores the marks obtained in two subjects and class result
contains the total marks obtained in the test. The class result
can inherit the details of marks obtained and the roll number
of students through multilevel inheritance.
Multiple Inheritance
 A class can inherit the attributes of two or more classes.
 It allows us to combine the features of several existing
classes as a starting point for defining new classes.
 Syntax:
class D : visibility B1, visibility B2 .....
{
...........
...........
};
Multiple Inheritance
 Example
Ambiguity Resolution in Inheritance
 Ambiguity occurs when a function with the same name appears in more
than one base class or appears in both base and derived class.
class M
{
public:
void display() { cout << “n class M” ; }
};
class N
{
public:
void display() { cout << “n class N” ; }
};
class P : public M, public N
Ambiguity Resolution in Inheritance
 The problem can be solved by defining a named
instance within the derived class using the class
resolution operator with the function name.
Class P : public M, public N
{
public:
void display()
{ // overrides display()
M :: display() // of M and N
}
};
Hierarchical Inheritance
 Inheritance support to the hierarchical design of a
program where certain features of one level are
shared by many others below that level.
 Example
Hybrid Inheritance
 There could be some situations where we need to
apply two or more types of inheritance to design a
program.
 Example
Virtual Base Classes
 Consider a situation where three kinds of
inheritance namely multilevel, multiple and
hierarchical inheritance are involved.
Virtual Base Classes
 The child has two direct base classes parent1 and
parent2 which themselves have a common base class
grandparent.
 The child inherits the public and protected members of
grandparent twice via parent1 and parent2.
 This duplication can be avoided by making the common
base class virtual base class.
Virtual Base Classes
class A // grandparent
{
..............
};
class B1 : virtual public A // parent 1
{
...............
};
class B2 : public virtual A //parent 2
{
...............
}
class C : public B1, public B2 //child
{
............... // only one copy of A will be inherited
};
Virtual Base Classes
 When a class is made virtual base class, C++ takes
necessary care to see that only one copy of that
class is inherited.
 Example
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.
 Eg: Student is the abstract class since it was not used
to create any objects.
Constructors in Derived Classes
 If 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
have a constructor.
 When both derived and base classes contain constructors,
the base constructor is executed first and then the
constructor in the derived class is executed.
Constructors in Derived Classes
 The constructor of the derived class receives the
entire list if values as its arguments and passes them
on to the base constructors in the order in which they
are declared in the derived class.
Constructors in Derived Classes
 The header line of the 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.
 Second part list the function calls to the base
constructors.
 Example.
Constructors in Derived Classes
Quiz 1
 What is polymorphism?
 An ability to have multiple forms is known as
polymorphism.
 What is static/early binding?
 Static Binding is the determination of which method to call
at compile time.
 What is Dynamic/late binding?
 Dynamic Binding is the determination of which method to
call at run time.
Achieving Polymorphism
Run time Polymorphism
 Selection of an appropriate member function at run
time is known as run time polymorphism.
 C++ supports a mechanism known as virtual function to
achieve run time polymorphism.
 At runtime when it is known what class objects are under
consideration, the appropriate version of the function is
invoked.
 This requires the use of pointers to objects.
Pointers to objects
 A pointer can point to an object created by a class.
 Syntax:
 item * it_ptr ;
 Object pointers are useful in creating objects at run time.
 We can refer to the member functions of item by using the
arrow operator and the object pointer.
 Example:
item x;
item *ptr = &x;
ptr -> getdata(100,75.5);
ptr -> show();
Pointers to Derived Classes
 Pointers can be used to point both base and derived class
objects.
 Pointers to objects of Base class are type compatible with
pointers to objects of a derived class.
 Thus, a single pointer can be used to points the objects
belonging to different classes.
 If B is a base class and D is a derived class then, a pointer
declared as a pointer to B can also be a pointer to D.
Pointers to Derived Classes
 Consider the example:
B *ptr;
B b;
D d;
ptr = &b;
ptr = &d;
 The pointer ptr can point to both b and d.
 Using ptr we can access only the members that are
inherited from B and not the members that belongs to D.
 If D has the same name as that of members of B then
any reference to that member by ptr will always access
the base class member.
Virtual Functions
 When we use the same function name in both the base
and derived classes, the function in base class is
declared as virtual.
 When a function is made virtual, C++ determines which
function to use at run time based on the type of object
pointed to by the base pointer, rather than the type of
the pointer.
 Example
Rules for Virtual Functions
 The virtual functions must be members of some class.
 They cannot be static members.
 They are accessed by using object pointers.
 A virtual function can be a friend of another class.
 A virtual function in a base class must be defined, even
though it may not be used.
 The prototypes of the base class version of a virtual
functions and all the derived class versions must be
identical.
 We cannot have virtual constructors, but we can have
virtual destructors.
Rules for Virtual Functions
 While a base pointer can point to any type of the
derived object, the reverse is not true.
 When a base pointer points to a derived class,
incrementing or decrementing it will not make it to
point to the next object of the derived class.
 If a virtual function is defined in the base class, it
need not be necessarily redefined in the derived
class.
Pure Virtual Functions
 Sometimes implementation of all function cannot be
provided in a base class because we don’t know the
implementation.
 A pure virtual function (or abstract function) in C++ is
a virtual function for which we don’t have implementation,
we only declare it.
 A pure virtual function is declared by assigning 0 in
declaration.
virtual void show() = 0;
Summary
 The mechanism of deriving a new class from an old class is
called ___________.
 A _____ member of class cannot be inherited either in public
mode or in private mode.
 A class can be derived from another derived class which is
known as ______ inheritance.
 A protected member inherited in the public mode becomes
_______, whereas inherited in private mode becomes private
in the derived class.
 A public member inherited in public mode becomes _____,
whereas inherited in private mode becomes _____ in the
derived class.
 There are two types of polymorphism namely ________ and
____________.
 A virtual function equated to zero is called ______.
Short Answer Questions
 What are the different forms of inheritance? Give
an example for each.
 Different form of inheritance are:
 Single Inheritance
 Multiple Inheritance
 Multilevel Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance
 Examples are described in the slides 5 – 9.
Short Answer Questions
 Describe the syntax of multiple inheritance. When
do we use such inheritance?
 When we need to combine the features of several
existing classes for defining new class multiple
inheritance is used.
 Syntax:
class D : visibility B1, visibility B2 .....
{
...........
...........
};
Short Answer Questions
 We know that a private member of a base class is not
inheritable. Is it anyway possible for the objects of a
derived class to access the private members of the
base class? If yes, how?
 Private members are not inheritable.
 Yes, it is possible to access the private members of base
class through derived class object this is achieved through
the member functions of base class.
Short Answer Questions
 In what order are the class constructors called when
a derived class object is created?
Short Answer Questions
 When do we use the protected visibility specifier to
a class member?
 In a situation where we want the members to be
accessed by the class in which they are declared and
the class immediately derived from it. We use
protected visibility specifier. The members are not
accessed by the functions outside these two classes.
Short Answer Questions
 Class D is derived from class B. The class D does not
contain any data members of its own. Does the class
D require constructors? If yes, Why?
 The class D will have a constructor with the arguments
for the base class members . The derived class
constructor is used to construct the base class object.
Short Answer Questions
 What is a Virtual Base Class?
 Virtual base classes is a way of preventing multiple
instances of a given class appearing in an inheritance
hierarchy when using multiple inheritance.
 What is an Abstract Class?
 An abstract class is one that is not used to create
objects. An abstract class is designed only to act as a
base class.
Short Answer Questions
 Why do we need virtual function?
 Virtual Functions are used to support " Run time
Polymorphism".
 What is run time polymorphism?
 When the virtual function is called by using a Base Class
Pointer, the Compiler decides at Runtime which version of the
function i.e. Base Class version or the overridden Derived
Class version is to be called. This is called Run time
Polymorphism.
Short Answer Questions
 When do we make a virtual function “pure”? What are
the implications of making a function a pure virtual
function.
 When we assign virtual function as zero at the time of its
declaration we create a pure virtual function. It is a function
declared in base class that has no definition relative to the
base class. A pure virtual function is implemented by classes
which are derived from the abstract class.
BACK
BACK
References
 Object Oriented Programming with C++ by E.
Balagurusamy.
End of unit

Weitere ähnliche Inhalte

Was ist angesagt?

Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsHarsh Patel
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++Arpita Patel
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Kamlesh Makvana
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsAnil Kumar
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationViji B
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
virtual function
virtual functionvirtual function
virtual functionVENNILAV6
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsMarlom46
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++imran khan
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Kamal Acharya
 

Was ist angesagt? (20)

Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Method overriding
Method overridingMethod overriding
Method overriding
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
virtual function
virtual functionvirtual function
virtual function
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 

Ähnlich wie Inheritance

Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdfWaqarRaj1
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Shweta Shah
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)Redwan Islam
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismJawad Khan
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfAnant240318
 

Ähnlich wie Inheritance (20)

Inheritance
InheritanceInheritance
Inheritance
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
Inheritance Inheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 

Mehr von Pranali Chaudhari (9)

Exception handling
Exception handlingException handling
Exception handling
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Templates
TemplatesTemplates
Templates
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 

Kürzlich hochgeladen

Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086anil_gaur
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 

Kürzlich hochgeladen (20)

Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 

Inheritance

  • 2. Contents  Types of inheritance with examples  Constructors and Destructors in derived class  Virtual Base Classes, Virtual function and Pure Virtual function  Abstract Base Classes
  • 3. Inheritance  The mechanism of deriving a new class from an old one is called inheritance.  C++ strongly supports the concept of reusability.  Once the class has been written and tested, it can be adapted by other programmers to suit their requirements.  The old class is referred to as the base class and the new one is called the derived class or subclass.
  • 4. Types of Inheritance  Single Inheritance  Multiple Inheritance  Hierarchical Inheritance  Multilevel Inheritance  Hybrid Inheritance
  • 5. Single Inheritance  A derived class with only one base class is called single inheritance.
  • 6. Multiple Inheritance  A derived class with more than one base classes is called multiple inheritance.
  • 7. Hierarchical Inheritance  In hierarchical inheritance one base class is inherited by more than one derived class.
  • 8. Multilevel Inheritance  The mechanism of deriving a class from another derived class is known as multilevel inheritance.
  • 9. Hybrid Inheritance  A combination of any two above types of inheritance is known as hybrid inheritance.
  • 10. Defining Derived Classes  A derived class can be defined by specifying its relationship with the base class in addition to its own details.  General Syntax: class derived_class_name : visibility-mode base_class_name { ........... // members of derived class ........... };
  • 11. Defining Derived Classes  The visibility mode is optional and may be private and public.  The default visibility mode is private.  Visibility mode specifies whether the features of the base class are privately derived or publicly derived.  Example: Class ABC : private | public XYZ { members of ABC };
  • 12. Visibility mode - Private  When a base class is privately inherited by a derived class, public members of the base class become private members of the derived class.  Members of base class is accessed by the member functions of the derived class.
  • 13. Visibility mode - Public  When the base class is publicly inherited, public members of the base class becomes public members of the derived class.  The members are accessible to the objects of derived class.  In both the cases, the private members are not inherited.
  • 14. Single Inheritance Example (Public)  Single Inheritance ( public mode)  The class D is a public derivation of the base class B. Thus inherits all the public members of B and retains their visibility.  The public member of class B is also a public member of the class D.  The private members are not inherited by D.  The class D after inheritance will have more members than what it contains at the time of declaration.  Class D (publically inherited)
  • 15. Single Inheritance Example (Private)  Single inheritance ( private mode)  In private derivation the public members of the base class become private members of the derived class.  Therefore the objects of D can not have direct access to the public member functions of B.  Class D (privately inherited)
  • 16. Making private Member Inheritable  C++ provides a third visibility modifier, protected, which serve a limited purpose in inheritance.  A member declared as protected is accessible by the member functions within its class and any class immediately derived from it.  It cannot be accessed by the functions outside these two classes.
  • 17. Making private Member Inheritable class alpha { private: // optional visible to ........... // member functions within its class ........... protected: // visible to member functions of its own ............ // and derived class ............ public: ............ // visible to all functions in the program ............ };
  • 18. Effect of Inheritance on the visibility of members
  • 19. Base Class (Protected Derivation)  It is also possible to inherit a base class in protected mode.  Here, both the public and protected members of the base class becomes protected members of the derived class.
  • 20. Practice Statement 1  Write a program to prepare marksheet of the college examination with the following items read from the keyboard: Student name, roll number, subject name, subject code, internal marks, external marks. Design the base class consisting of data members such as student name, roll no, subject name. The derived class consists of members subject code, internal and external marks.
  • 21. Multilevel Inheritance  The method of deriving a class from another derived class is known as 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.  The class B is known as intermediate base class.  Example: Suppose the test results of students are stored in three different classes. Class student stores roll number, class test stores the marks obtained in two subjects and class result contains the total marks obtained in the test. The class result can inherit the details of marks obtained and the roll number of students through multilevel inheritance.
  • 22. Multiple Inheritance  A class can inherit the attributes of two or more classes.  It allows us to combine the features of several existing classes as a starting point for defining new classes.  Syntax: class D : visibility B1, visibility B2 ..... { ........... ........... };
  • 24. Ambiguity Resolution in Inheritance  Ambiguity occurs when a function with the same name appears in more than one base class or appears in both base and derived class. class M { public: void display() { cout << “n class M” ; } }; class N { public: void display() { cout << “n class N” ; } }; class P : public M, public N
  • 25. Ambiguity Resolution in Inheritance  The problem can be solved by defining a named instance within the derived class using the class resolution operator with the function name. Class P : public M, public N { public: void display() { // overrides display() M :: display() // of M and N } };
  • 26. Hierarchical Inheritance  Inheritance support to the hierarchical design of a program where certain features of one level are shared by many others below that level.  Example
  • 27. Hybrid Inheritance  There could be some situations where we need to apply two or more types of inheritance to design a program.  Example
  • 28. Virtual Base Classes  Consider a situation where three kinds of inheritance namely multilevel, multiple and hierarchical inheritance are involved.
  • 29. Virtual Base Classes  The child has two direct base classes parent1 and parent2 which themselves have a common base class grandparent.  The child inherits the public and protected members of grandparent twice via parent1 and parent2.  This duplication can be avoided by making the common base class virtual base class.
  • 30. Virtual Base Classes class A // grandparent { .............. }; class B1 : virtual public A // parent 1 { ............... }; class B2 : public virtual A //parent 2 { ............... } class C : public B1, public B2 //child { ............... // only one copy of A will be inherited };
  • 31. Virtual Base Classes  When a class is made virtual base class, C++ takes necessary care to see that only one copy of that class is inherited.  Example
  • 32. 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.  Eg: Student is the abstract class since it was not used to create any objects.
  • 33. Constructors in Derived Classes  If 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 have a constructor.  When both derived and base classes contain constructors, the base constructor is executed first and then the constructor in the derived class is executed.
  • 34. Constructors in Derived Classes  The constructor of the derived class receives the entire list if values as its arguments and passes them on to the base constructors in the order in which they are declared in the derived class.
  • 35. Constructors in Derived Classes  The header line of the 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.  Second part list the function calls to the base constructors.  Example.
  • 37. Quiz 1  What is polymorphism?  An ability to have multiple forms is known as polymorphism.  What is static/early binding?  Static Binding is the determination of which method to call at compile time.  What is Dynamic/late binding?  Dynamic Binding is the determination of which method to call at run time.
  • 39. Run time Polymorphism  Selection of an appropriate member function at run time is known as run time polymorphism.  C++ supports a mechanism known as virtual function to achieve run time polymorphism.  At runtime when it is known what class objects are under consideration, the appropriate version of the function is invoked.  This requires the use of pointers to objects.
  • 40. Pointers to objects  A pointer can point to an object created by a class.  Syntax:  item * it_ptr ;  Object pointers are useful in creating objects at run time.  We can refer to the member functions of item by using the arrow operator and the object pointer.  Example: item x; item *ptr = &x; ptr -> getdata(100,75.5); ptr -> show();
  • 41. Pointers to Derived Classes  Pointers can be used to point both base and derived class objects.  Pointers to objects of Base class are type compatible with pointers to objects of a derived class.  Thus, a single pointer can be used to points the objects belonging to different classes.  If B is a base class and D is a derived class then, a pointer declared as a pointer to B can also be a pointer to D.
  • 42. Pointers to Derived Classes  Consider the example: B *ptr; B b; D d; ptr = &b; ptr = &d;  The pointer ptr can point to both b and d.  Using ptr we can access only the members that are inherited from B and not the members that belongs to D.  If D has the same name as that of members of B then any reference to that member by ptr will always access the base class member.
  • 43. Virtual Functions  When we use the same function name in both the base and derived classes, the function in base class is declared as virtual.  When a function is made virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer, rather than the type of the pointer.  Example
  • 44. Rules for Virtual Functions  The virtual functions must be members of some class.  They cannot be static members.  They are accessed by using object pointers.  A virtual function can be a friend of another class.  A virtual function in a base class must be defined, even though it may not be used.  The prototypes of the base class version of a virtual functions and all the derived class versions must be identical.  We cannot have virtual constructors, but we can have virtual destructors.
  • 45. Rules for Virtual Functions  While a base pointer can point to any type of the derived object, the reverse is not true.  When a base pointer points to a derived class, incrementing or decrementing it will not make it to point to the next object of the derived class.  If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class.
  • 46. Pure Virtual Functions  Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation.  A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it.  A pure virtual function is declared by assigning 0 in declaration. virtual void show() = 0;
  • 47. Summary  The mechanism of deriving a new class from an old class is called ___________.  A _____ member of class cannot be inherited either in public mode or in private mode.  A class can be derived from another derived class which is known as ______ inheritance.  A protected member inherited in the public mode becomes _______, whereas inherited in private mode becomes private in the derived class.  A public member inherited in public mode becomes _____, whereas inherited in private mode becomes _____ in the derived class.  There are two types of polymorphism namely ________ and ____________.  A virtual function equated to zero is called ______.
  • 48. Short Answer Questions  What are the different forms of inheritance? Give an example for each.  Different form of inheritance are:  Single Inheritance  Multiple Inheritance  Multilevel Inheritance  Hierarchical Inheritance  Hybrid Inheritance  Examples are described in the slides 5 – 9.
  • 49. Short Answer Questions  Describe the syntax of multiple inheritance. When do we use such inheritance?  When we need to combine the features of several existing classes for defining new class multiple inheritance is used.  Syntax: class D : visibility B1, visibility B2 ..... { ........... ........... };
  • 50. Short Answer Questions  We know that a private member of a base class is not inheritable. Is it anyway possible for the objects of a derived class to access the private members of the base class? If yes, how?  Private members are not inheritable.  Yes, it is possible to access the private members of base class through derived class object this is achieved through the member functions of base class.
  • 51. Short Answer Questions  In what order are the class constructors called when a derived class object is created?
  • 52. Short Answer Questions  When do we use the protected visibility specifier to a class member?  In a situation where we want the members to be accessed by the class in which they are declared and the class immediately derived from it. We use protected visibility specifier. The members are not accessed by the functions outside these two classes.
  • 53. Short Answer Questions  Class D is derived from class B. The class D does not contain any data members of its own. Does the class D require constructors? If yes, Why?  The class D will have a constructor with the arguments for the base class members . The derived class constructor is used to construct the base class object.
  • 54. Short Answer Questions  What is a Virtual Base Class?  Virtual base classes is a way of preventing multiple instances of a given class appearing in an inheritance hierarchy when using multiple inheritance.  What is an Abstract Class?  An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class.
  • 55. Short Answer Questions  Why do we need virtual function?  Virtual Functions are used to support " Run time Polymorphism".  What is run time polymorphism?  When the virtual function is called by using a Base Class Pointer, the Compiler decides at Runtime which version of the function i.e. Base Class version or the overridden Derived Class version is to be called. This is called Run time Polymorphism.
  • 56. Short Answer Questions  When do we make a virtual function “pure”? What are the implications of making a function a pure virtual function.  When we assign virtual function as zero at the time of its declaration we create a pure virtual function. It is a function declared in base class that has no definition relative to the base class. A pure virtual function is implemented by classes which are derived from the abstract class.
  • 57. BACK
  • 58. BACK
  • 59. References  Object Oriented Programming with C++ by E. Balagurusamy.