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.
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

PPT
Inheritance in C++
PDF
lecture 6.pdf
PPT
Inheritance
PPTX
Opp concept in c++
PPTX
TYPES OF INHERITANCE CONCEPT IN C++.pptx
PDF
Inheritance
PPTX
C++ presentation
ODP
Ppt of c++ vs c#
Inheritance in C++
lecture 6.pdf
Inheritance
Opp concept in c++
TYPES OF INHERITANCE CONCEPT IN C++.pptx
Inheritance
C++ presentation
Ppt of c++ vs c#

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

PDF
PPT
6 Inheritance
PPTX
Inheritance
PPT
Inheritance in C++
PDF
chapter-10-inheritance.pdf
PPTX
C++ Object Oriented Programming
PPTX
Inheritance
PPT
11 Inheritance.ppt
PPTX
Inheritance
DOCX
Introduction to object oriented programming concepts
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
PDF
Inheritance chapter-6-computer-science-with-c++ opt
PPTX
Inheritance
PPTX
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
DOCX
Ganesh groups
PPTX
OOPS IN C++
PPTX
Polymorphism and Virtual Functions ppt bioinformatics
PPTX
Object Oriented Design and Programming Unit-03
6 Inheritance
Inheritance
Inheritance in C++
chapter-10-inheritance.pdf
C++ Object Oriented Programming
Inheritance
11 Inheritance.ppt
Inheritance
Introduction to object oriented programming concepts
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
Ganesh groups
OOPS IN C++
Polymorphism and Virtual Functions ppt bioinformatics
Object Oriented Design and Programming Unit-03
Ad

Recently uploaded (20)

PPTX
Math 2 Quarter 2 Week 1 Matatag Curriculum
PDF
Health aspects of bilberry: A review on its general benefits
PDF
Disorder of Endocrine system (1).pdfyyhyyyy
DOCX
THEORY AND PRACTICE ASSIGNMENT SEMESTER MAY 2025.docx
PDF
Compact First Student's Book Cambridge Official
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PPTX
ACFE CERTIFICATION TRAINING ON LAW.pptx
PDF
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
PDF
faiz-khans about Radiotherapy Physics-02.pdf
PPTX
4. Diagnosis and treatment planning in RPD.pptx
PPTX
Thinking Routines and Learning Engagements.pptx
PDF
GSA-Past-Papers-2010-2024-2.pdf CSS examination
PPSX
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
PDF
Diabetes Mellitus , types , clinical picture, investigation and managment
PPT
hemostasis and its significance, physiology
PDF
WHAT NURSES SAY_ COMMUNICATION BEHAVIORS ASSOCIATED WITH THE COMP.pdf
PPTX
principlesofmanagementsem1slides-131211060335-phpapp01 (1).ppt
PPTX
Power Point PR B.Inggris 12 Ed. 2019.pptx
PDF
Chevening Scholarship Application and Interview Preparation Guide
DOCX
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
Math 2 Quarter 2 Week 1 Matatag Curriculum
Health aspects of bilberry: A review on its general benefits
Disorder of Endocrine system (1).pdfyyhyyyy
THEORY AND PRACTICE ASSIGNMENT SEMESTER MAY 2025.docx
Compact First Student's Book Cambridge Official
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
ACFE CERTIFICATION TRAINING ON LAW.pptx
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
faiz-khans about Radiotherapy Physics-02.pdf
4. Diagnosis and treatment planning in RPD.pptx
Thinking Routines and Learning Engagements.pptx
GSA-Past-Papers-2010-2024-2.pdf CSS examination
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
Diabetes Mellitus , types , clinical picture, investigation and managment
hemostasis and its significance, physiology
WHAT NURSES SAY_ COMMUNICATION BEHAVIORS ASSOCIATED WITH THE COMP.pdf
principlesofmanagementsem1slides-131211060335-phpapp01 (1).ppt
Power Point PR B.Inggris 12 Ed. 2019.pptx
Chevening Scholarship Application and Interview Preparation Guide
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
Ad

UNIT 4.pdf.........................................

  • 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:
  • 7. 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
  • 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. 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: