UNIT 4
LC:1- INHERITANCE & TYPES OF INHERITANCE, TYPE-SUBSTITUTABILITY
 INHERITANCE: A feature in C++ where one class (called the derived class) inherits properties and
behaviour (data members and methods) from another class (called the base class)
 SYNTAX OF INHERITANCE IN C++:
NOTE: -> except the private properties all (public, protected) will get inherited
-> this mode can be either private or public
-> the members of class can be either private, public, protected
 ACCESS CONTROL IN INHERITANCE:
MODE BASE CLASS DERIVED CLASS
Public Private ❌
Public Public Public
Public Protected Protected
Private Private ❌
Private Public Private
Private Protected Private
 ADVANTAGE OF INHERITANCE: reusability of code
 TYPES OF INHERITANCE:
Single Inheritance: One class inherits from one base class
Multiple Inheritance: One class inherits from more than one base class.
Multilevel Inheritance: A class is derived from a class that is already derived from another class.
Hierarchical Inheritance: Multiple classes inherit from the same base class.
Hybrid Inheritance: A combination of two or more types of inheritance.
 Type-Substitutability: A derived class object can be substituted wherever the base class object is
expected. This is useful in polymorphism where a base class pointer can point to a derived class
object.
LC:2- MULTIPLE INHERITANCES, ISSUES WITH MULTIPLE INHERITANCE
 MULTIPLE INHERITANCE: A class can inherit from more than one base class. This allows the
derived class to inherit features from multiple classes.
 SYNTAX:
 CODE:
 OUTPUT:
 ISSUES WITH MULTIPLE INHERITANCE:
The “Diamond Problem” is a common issue in multiple inheritance where a class inherits from
two or more classes that both share a common base class. This creates confusion because the class
that inherits from both might have two copies of the same method or member from the common
ancestor.
LC:3- COMPOSITION VERSUS INHERITANCE, VIRTUAL BASE CLASS
 COMPOSITION: Composition means that one class contains an object (or objects) of another
class as a member. In other words, a class is made up of objects from other classes.
 CODE:
 OUTPUT:
 INHERITANCE: acquiring the properties from base class to derived class
 CODE:
 OUTPUT:
 COMPOSITION VS INHERITANCE:
 Virtual Base Class: A virtual base class is a way to make sure that when you have a class that is
inherited by multiple other classes, there is only one copy of the base class. This helps avoid
confusion and saves memory.
 This concept is primarily used to resolve ambiguities that arise in multiple inheritance scenarios,
particularly the diamond problem.
 Solves the diamond problem by making a common base class virtual, ensuring that only one copy
of the base class is shared among all derived classes.
 CODE:
 Output:
LC:4-STATIC POLYMORPHISM USING INHERITANCE
STATIC POLYMORPHISM USING INHERITANCE:
Static polymorphism means that the function call is resolved during the compilation of the program.
It is commonly achieved through inheritance when a derived class provides additional overloaded
functions or operators while inheriting base class methods.
 CODE:
 Output:
LC:5-FUNCTIONS OVERRIDING
 "If the base class and the derived class contain the same function name, same number of
arguments, and same type of arguments, then the function in the derived class overrides the
function in the base class. The concept is called function overriding. The function in the base class
is called the overridden function, and the function in the derived class is called the
overriding function."
 CODE:
 OUTPUT:
Output is girls are nice because the print function in derived class overridden the print function in the base class
In order to print the derived class, we can use default constructors (default constructors will be executed
depending upon order of execution)
 Code:
 Output:
LC:6-CONSTRUCTORS IN INHERITANCE & DESTRUCTORS INHERITANCE
CONSTRUCTORS IN INHERITANCE:
PARAMETER CONSTRUCTOR IN INHERITANCE:
 CODE:
 OUTPUT:
CODE: C++ program to implement constructor and instructor in inheritance
OUTPUT:
1)DEFAULT
CONSTRUCTOR IN
INHERITANCE
2)PARAMETER
CONSTRUCTOR IN
INHERITANCE
LC:7- POINTERS IN INHERITANCE, VIRTUAL FUNCTIONS
POINTERS IN INHERITANCE: Pointer in inheritance, the base class Pointer, can hold the address of the
derived class Object, but reverse is not possible. Derived class Pointer cannot hold the address of the
base class Object.
CODE:
OUTPUT:
VIRTUAL FUNTIONS:
A virtual function in C++ is a member function in a base class that can be overridden in a derived class. It
allows for run-time polymorphism. It can be achieved with the help of a virtual function.
To make any function virtual, use the keyword virtual.
FEATURES OF VIRTUAL FUNCTION:
1. A virtual function cannot be static.
2. A virtual function can be a friend function of another class.
3. A virtual function can be accessed using a pointer to a base class to achieve runtime polymorphism.
4. The prototype of a virtual function should be the same in the base as well as the derived class.
5. They are always defined in the base class and overridden in the derived class.
CODE:
OUPUT:
LC:8- PURE VIRTUAL FUNCTIONS AND ABSTRACT CLASSES
PURE VIRTUAL FUNTION: if we assign zero value for virtual function that function will become as
pure virtual function.
Syntax: and example
CODE:
OUTPUT:
ABSTRACT CLASS: if a class contain at least one pure virtual function them that class will become an
abstract class. in abstract class we cannot create objects
CODE:
OUTPUT:

More Related Content

PDF
Inheritance
PPTX
INHERITANCE.pptx
PPSX
Inheritance and Polymorphism in Oops
PPTX
C++ Object Oriented Programming
PPTX
TYPES OF INHERITANCE CONCEPT IN C++.pptx
PPTX
full defination of final opp.pptx
PPTX
inheritance in OOPM
PPTX
Inheritance
Inheritance
INHERITANCE.pptx
Inheritance and Polymorphism in Oops
C++ Object Oriented Programming
TYPES OF INHERITANCE CONCEPT IN C++.pptx
full defination of final opp.pptx
inheritance in OOPM
Inheritance

Similar to UNIT 4.docx......................................... (20)

PPTX
Inheritance, Polymorphism, and Virtual Functions.pptx
PPTX
Inheritance
PPTX
Inheritance in c++
PPTX
Introduction to inheritance and different types of inheritance
PPTX
OOPS Basics With Example
PPTX
Virtual function
PDF
22 scheme OOPs with C++ BCS306B_module4.pdf
PPTX
Inheritance.pptx
PDF
PPT
Inheritance
PPTX
OOPS & C++(UNIT 4)
PPTX
Lecture 5.mte 407
PPTX
Inheritance
DOCX
oop database doc for studevsgdy fdsyn hdf
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
PPTX
Aryan's pres. entation.pptx
PPT
Inheritance.ppt
PPTX
Object Oriented Design and Programming Unit-03
PPTX
Inheritance in C++ (Programming Fundamentals)
Inheritance, Polymorphism, and Virtual Functions.pptx
Inheritance
Inheritance in c++
Introduction to inheritance and different types of inheritance
OOPS Basics With Example
Virtual function
22 scheme OOPs with C++ BCS306B_module4.pdf
Inheritance.pptx
Inheritance
OOPS & C++(UNIT 4)
Lecture 5.mte 407
Inheritance
oop database doc for studevsgdy fdsyn hdf
MODULE2_INHERITANCE_SESSION1.ppt computer
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
Aryan's pres. entation.pptx
Inheritance.ppt
Object Oriented Design and Programming Unit-03
Inheritance in C++ (Programming Fundamentals)
Ad

Recently uploaded (20)

PDF
Disorder of Endocrine system (1).pdfyyhyyyy
PPTX
Theoretical for class.pptxgshdhddhdhdhgd
PDF
African Communication Research: A review
PPTX
IT infrastructure and emerging technologies
PDF
Physical pharmaceutics two in b pharmacy
PDF
CHALLENGES FACED BY TEACHERS WHEN TEACHING LEARNERS WITH DEVELOPMENTAL DISABI...
PDF
Laparoscopic Imaging Systems at World Laparoscopy Hospital
PDF
FAMILY PLANNING (preventative and social medicine pdf)
PPTX
Diploma pharmaceutics notes..helps diploma students
PPTX
Math 2 Quarter 2 Week 1 Matatag Curriculum
DOCX
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
PPSX
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
PDF
POM_Unit1_Notes.pdf Introduction to Management #mba #bba #bcom #bballb #class...
PPT
hemostasis and its significance, physiology
PPTX
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
PPTX
Neurological complocations of systemic disease
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PPTX
operating_systems_presentations_delhi_nc
PDF
anganwadi services for the b.sc nursing and GNM
PPTX
Power Point PR B.Inggris 12 Ed. 2019.pptx
Disorder of Endocrine system (1).pdfyyhyyyy
Theoretical for class.pptxgshdhddhdhdhgd
African Communication Research: A review
IT infrastructure and emerging technologies
Physical pharmaceutics two in b pharmacy
CHALLENGES FACED BY TEACHERS WHEN TEACHING LEARNERS WITH DEVELOPMENTAL DISABI...
Laparoscopic Imaging Systems at World Laparoscopy Hospital
FAMILY PLANNING (preventative and social medicine pdf)
Diploma pharmaceutics notes..helps diploma students
Math 2 Quarter 2 Week 1 Matatag Curriculum
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
POM_Unit1_Notes.pdf Introduction to Management #mba #bba #bcom #bballb #class...
hemostasis and its significance, physiology
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
Neurological complocations of systemic disease
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
operating_systems_presentations_delhi_nc
anganwadi services for the b.sc nursing and GNM
Power Point PR B.Inggris 12 Ed. 2019.pptx
Ad

UNIT 4.docx.........................................

  • 1. UNIT 4 LC:1- INHERITANCE & TYPES OF INHERITANCE, TYPE-SUBSTITUTABILITY  INHERITANCE: A feature in C++ where one class (called the derived class) inherits properties and behaviour (data members and methods) from another class (called the base class)  SYNTAX OF INHERITANCE IN C++: NOTE: -> except the private properties all (public, protected) will get inherited -> this mode can be either private or public -> the members of class can be either private, public, protected  ACCESS CONTROL IN INHERITANCE: MODE BASE CLASS DERIVED CLASS Public Private ❌ Public Public Public Public Protected Protected Private Private ❌ Private Public Private Private Protected Private  ADVANTAGE OF INHERITANCE: reusability of code  TYPES OF INHERITANCE: Single Inheritance: One class inherits from one base class Multiple Inheritance: One class inherits from more than one base class. Multilevel Inheritance: A class is derived from a class that is already derived from another class.
  • 2. Hierarchical Inheritance: Multiple classes inherit from the same base class. Hybrid Inheritance: A combination of two or more types of inheritance.  Type-Substitutability: A derived class object can be substituted wherever the base class object is expected. This is useful in polymorphism where a base class pointer can point to a derived class object. LC:2- MULTIPLE INHERITANCES, ISSUES WITH MULTIPLE INHERITANCE  MULTIPLE INHERITANCE: A class can inherit from more than one base class. This allows the derived class to inherit features from multiple classes.  SYNTAX:  CODE:
  • 3.  OUTPUT:  ISSUES WITH MULTIPLE INHERITANCE: The “Diamond Problem” is a common issue in multiple inheritance where a class inherits from two or more classes that both share a common base class. This creates confusion because the class that inherits from both might have two copies of the same method or member from the common ancestor. LC:3- COMPOSITION VERSUS INHERITANCE, VIRTUAL BASE CLASS  COMPOSITION: Composition means that one class contains an object (or objects) of another class as a member. In other words, a class is made up of objects from other classes.  CODE:  OUTPUT:
  • 4.  INHERITANCE: acquiring the properties from base class to derived class  CODE:  OUTPUT:  COMPOSITION VS INHERITANCE:  Virtual Base Class: A virtual base class is a way to make sure that when you have a class that is inherited by multiple other classes, there is only one copy of the base class. This helps avoid confusion and saves memory.  This concept is primarily used to resolve ambiguities that arise in multiple inheritance scenarios, particularly the diamond problem.  Solves the diamond problem by making a common base class virtual, ensuring that only one copy of the base class is shared among all derived classes.  CODE:  Output:
  • 5. LC:4-STATIC POLYMORPHISM USING INHERITANCE STATIC POLYMORPHISM USING INHERITANCE: Static polymorphism means that the function call is resolved during the compilation of the program. It is commonly achieved through inheritance when a derived class provides additional overloaded functions or operators while inheriting base class methods.  CODE:  Output:
  • 6. LC:5-FUNCTIONS OVERRIDING  "If the base class and the derived class contain the same function name, same number of arguments, and same type of arguments, then the function in the derived class overrides the function in the base class. The concept is called function overriding. The function in the base class is called the overridden function, and the function in the derived class is called the overriding function."  CODE:  OUTPUT: Output is girls are nice because the print function in derived class overridden the print function in the base class In order to print the derived class, we can use default constructors (default constructors will be executed depending upon order of execution)  Code:  Output: LC:6-CONSTRUCTORS IN INHERITANCE & DESTRUCTORS INHERITANCE
  • 7. CONSTRUCTORS IN INHERITANCE: PARAMETER CONSTRUCTOR IN INHERITANCE:  CODE:  OUTPUT: CODE: C++ program to implement constructor and instructor in inheritance OUTPUT: 1)DEFAULT CONSTRUCTOR IN INHERITANCE 2)PARAMETER CONSTRUCTOR IN INHERITANCE
  • 8. LC:7- POINTERS IN INHERITANCE, VIRTUAL FUNCTIONS POINTERS IN INHERITANCE: Pointer in inheritance, the base class Pointer, can hold the address of the derived class Object, but reverse is not possible. Derived class Pointer cannot hold the address of the base class Object. CODE: OUTPUT: VIRTUAL FUNTIONS: A virtual function in C++ is a member function in a base class that can be overridden in a derived class. It allows for run-time polymorphism. It can be achieved with the help of a virtual function. To make any function virtual, use the keyword virtual. FEATURES OF VIRTUAL FUNCTION: 1. A virtual function cannot be static. 2. A virtual function can be a friend function of another class. 3. A virtual function can be accessed using a pointer to a base class to achieve runtime polymorphism. 4. The prototype of a virtual function should be the same in the base as well as the derived class. 5. They are always defined in the base class and overridden in the derived class.
  • 9. CODE: OUPUT: LC:8- PURE VIRTUAL FUNCTIONS AND ABSTRACT CLASSES PURE VIRTUAL FUNTION: if we assign zero value for virtual function that function will become as pure virtual function. Syntax: and example CODE: OUTPUT:
  • 10. ABSTRACT CLASS: if a class contain at least one pure virtual function them that class will become an abstract class. in abstract class we cannot create objects CODE: OUTPUT: