SlideShare a Scribd company logo
INHERITANCE in C++
BY,
Ranjana Thakuria
Assistant Professor,
Department of CSE, SVCE Bengaluru
HAPPY BIRTH DAY
INHERITANCE & Birthday Cake
Ranjana Thakuria, CSE Department
INHERITANCE-Example
Ranjana Thakuria, CSE Department
Children inherits property (genetic
characteristics ) from their parents
INHERITANCE
Ranjana Thakuria, CSE Department
 The capability of a class to derive properties and characteristics from another
class.
 New classes are created from the existing classes.
 Reusability can be achieved through inheritance.
 The new class created is called derived class or child class or Sub Class
 The existing class is known as the base class or parent class or Super class.
 The derived class now is said to be inherited from the base class.
INHERITANCE & Birthday Cake
Ranjana Thakuria, CSE Department
BASE
DERIVED FROM BASE
When to use inheritance?
Ranjana Thakuria, CSE Department
BASE CLASS
DERIVED CLASSes
INHERITANCE
Implementing Inheritance in C++:
Ranjana Thakuria, CSE Department
Syntax:
class <derived_class_name> : <access-specifier> <base_class_name> {
//body
}
keyword to create a new class
name of the base class
Public/
Private/
Protected;
private by default
new class name, which will
inherit the base class
Example:
class B: protected A{
………
};
Implementing Inheritance
Ranjana Thakuria, CSE Department
 Example:
class A{
int a;
………..
};
class B: protected A{
int b;
………
};
A
A
B
B
Does not inherit a full parent object
Implementing Inheritance
Ranjana Thakuria, CSE Department
 Accessibility:
class A{
private:
int x;
protacted:
int y;
public:
int z;
};
class derived: A{
//y, z accessible
};
class general{
// z accessible
};
Modes of Inheritance
Ranjana Thakuria, CSE Department
Public Mode: If derive a subclass from a public base class,
public members public
protected members protected
Protected Mode: If we derive a subclass from a Protected base class
public member  protected
protected members protected
Private Mode:
public member  private
protected members  private
A derived class doesn’t inherit access
to private data members.
class ABC : private XYZ
{ };
Or
class ABC: XYZ
{ }
class ABC : protected XYZ
{ };
class ABC : public XYZ
{ };
Mode-wise accessibility table
Ranjana Thakuria, CSE Department
Example: public Derivation Accessibility
Ranjana Thakuria, CSE Department
class A
{
private:
int a;
protected:
int b;
public:
int c;
};
class B: public A
{
public:
void set()
{
//a=1; //not allowed
b=2; //accessible in further derivation only
c=3; //accessible in further derivation and others
}
};
main()
{
B obj;
//obj.a=1//not allowed
//obj.b=2;//not allowed
obj.c=3;
return 0;
}
Ex: protected Derivation Accessibility
Ranjana Thakuria, CSE Department
class A
{
private:
int a;
protected:
int b;
public:
int c;
};
class B: protected A
{
public:
void set()
{
//a=1; //not allowed
b=2; //accessible in further derivation only
c=3; //accessible in further derivation only
}
};
main()
{
B obj;
//obj.a=1//not allowed
//obj.b=2;//not allowed
//obj.c=3 ;//not allowed
return 0;
}
Ex: private Derivation Accessibility
Ranjana Thakuria, CSE Department
class A
{
private:
int a;
protected:
int b;
public:
int c;
};
class B: private A
{
public:
void set()
{
//a=1; //not allowed
b=2; //not accessible in further derivation
c=3; //not accessible in further derivation
}
};
main()
{
B obj;
//obj.a=1//not allowed
//obj.b=2;//not allowed
//obj.c=3 ;//not allowed
return 0;
}
Types of Inheritances in C++
Ranjana Thakuria, CSE Department
1. Single inheritance:
3. Multilevel inheritance:
5.Hybrid inheritance:
2. Multiple inheritance:
4. Hierarchical inheritance:
Five Types of Inheritances:
Single Inheritance:
Ranjana Thakuria, CSE Department
 A class is allowed to inherit from only one class.
 Syntax:
class subclass_name : access_mode base_class {
// body of subclass
};
 Example:
class A { ... .. ...
};
class B: public A {
... .. ...
};
Single Inheritance:
Ranjana Thakuria, CSE Department
 A class is allowed to inherit from only one class.
 Syntax:
class subclass_name : access_mode base_class {
// body of subclass
};
 Example:
class A { ... .. ...
};
class B: public A {
... .. ...
};
Single Inheritance- Example :
Ranjana Thakuria, CSE Department
class A
{
protected:
int a;
public:
void set_A(int x) {
a=x;
}
void disp_A() {
cout<<endl<<"Value of A="<<a;
}
};
class B: public A
{
int b;
public:
void set_B(int x, int y) {
set_A(x);
b=y;
}
void disp_B() {
disp_A();
cout<<endl<<"Value of B="<<b;
}
};
main()
{
B b;
b.set_B(5,10);
b.disp_B();
return 0;
}
OUTPUT
Value of A=5
Value of B=10
Multiple Inheritance:
Ranjana Thakuria, CSE Department
 A class can inherit from more than one class.
 i.e one subclass is inherited from more than one base class.
Syntax:
class subclass_name : access_mode1 base_class1, access_mode2 base_class2 {
// body of subclass
};
Multiple Inheritance:
Ranjana Thakuria, CSE Department
 Example:
class A { ... .. ...
};
class B { ... .. ...
};
class C: public A, protected B {
... .. ...
};
Multiple Inheritance- Example :
Ranjana Thakuria, CSE Department
// first base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehiclen"; }
};
// second base class
class FourWheeler {
public:
FourWheeler(){
cout << "This is a 4 wheeler Vehiclen";
}
};
// sub class derived from two base classes
class Car : public Vehicle, public FourWheeler {
public:
Car(){cout<<"nConstructed Car";}
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
OUTPUT
This is a Vehicle
This is a 4 wheeler Vehicle
Constructed Car
MultiLevel Inheritance:
Ranjana Thakuria, CSE Department
 A class is derived from another derived
class.
 or one subclass is inherited from
another sub class.
 i.e, two levels of single inheritance
MultiLevel Inheritance:
Ranjana Thakuria, CSE Department
 Example:
class A {
... .. ...
};
class B : public A {
... .. ...
};
class C: protected B {
... .. ...
};
MultiLevel Inheritance:
Ranjana Thakuria, CSE Department
Example:
#include<iostream>
using namespace std;
class A
{
protected:
int a;
public:
void set_A(){
cout<<"Enter the Value of A=";
cin>>a;
}
void disp_A(){
cout<<endl<<"Value of A="<<a;
}
};
class B: public A
{
protected:
int b;
public:
void set_B()
{
cout<<"Enter the Value of B=";
cin>>b;
}
void disp_B()
{
cout<<endl<<"Value of B="<<b;
}
};
class C: public B
{
int c,p;
public:
void set_C()
{
cout<<"Enter the Value of C=";
cin>>c;
}
void disp_C()
{
cout<<endl<<"Value of C="<<c;
}
void cal_product()
{
p=a*b*c;
cout<<endl<<"Product of "<<a<<" * "<<b<<" *
"<<c<<" = "<<p;
}
};
main()
{
C _c;
_c.set_A();
_c.set_B();
_c.set_C();
_c.disp_A();
_c.disp_B();
_c.disp_C();
_c.cal_product();
return 0;
}
OUTPUT:
Value of A=10
Value of B=20
Value of C=30
Product of 10 * 20 * 30 = 6000
Hierarchical Inheritance:
Ranjana Thakuria, CSE Department
 More than one subclass is inherited from a single base class.
 i.e. more than one derived class is created from a single base class.
Hierarchical Inheritance:
Ranjana Thakuria, CSE Department
 Example:
class B {
... .. ...
};
class A : public B {
... .. ...
};
class C: protected B {
... .. ...
};
Hierarchical Inheritance:
Ranjana Thakuria, CSE Department
 Example:
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehiclen"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
Hybrid Inheritance:
Ranjana Thakuria, CSE Department
 Implemented by combining more than one type of inheritance.
 Example: Combining Hierarchical inheritance and Multiple Inheritance.
 Below image shows the combination of hierarchical and multiple inheritances:
Hybrid Inheritance:
Ranjana Thakuria, CSE Department
Example:
// C++ program for Hybrid Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehiclen"; }
};
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehiclen"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle, public Fare {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
}
OUTPUT:
This is a Vehicle
Fare of Vehicle
Multipath inheritance:
Ranjana Thakuria, CSE Department
 A special case of hybrid inheritance:
 A derived class with two base classes and these two base classes have one
common base class .
 Ambiguity can arise in this type of inheritance.
 Ambiguity can be resolved in two ways:
 using scope resolution operator
 using virtual base class
Multipath Ambiguity resolve:
Ranjana Thakuria, CSE Department
 Avoiding ambiguity using the scope resolution operator:
obj.ClassB::a = 10; // Statement 3
obj.ClassC::a = 100; // Statement 4
Multipath Ambiguity resolve:
Ranjana Thakuria, CSE Department
2) Avoiding ambiguity using the virtual base class:
Class-D has only one copy of ClassA
class ClassA{
public:
int a;
};
class ClassB : virtual public ClassA{
public:
int b;
};
class ClassC : virtual public ClassA{
public:
int c;
};
class ClassD : public ClassB, public ClasC{
public:
int d;
};
int main()
{
ClassD obj;
obj.a = 10; // Statement 3
obj.a = 100; // Statement 4
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout << "n a : " << obj.a;
cout << "n b : " << obj.b;
cout << "n c : " << obj.c;
cout << "n d : " << obj.d << 'n';
}
Inheritance and Constructors:
Ranjana Thakuria, CSE Department
 Constructors can be inherited.
 The constructors of inherited classes are called in the same order in which
they are inherited.
Example Constructors in inheritance:
class A {
A(){}
... .. ...
};
class B: public A {
B():A(){}
... .. ...
};
Order of Inheritance- Constructors :
Ranjana Thakuria, CSE Department
class A1{
public:
A1(){
cout << "Constructor of the base class A1 n";
}
};
class A2{
public:
A2(){
cout << "Constructor of the base class A2 n";
}
}; 3 1 2
class S: public A1, virtual A2
{
public:
S(): A1(), A2(){
cout << "Constructor of the derived class S n";
}
};
// Driver code
int main()
{
S obj;
return 0;
}
THANK YOU
Ranjana Thakuria, CSE Department

More Related Content

Similar to c++Inheritance.pdf

MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
ssuser6f3c8a
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
study material
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
DeepasCSE
 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)
Abhishek Khune
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
Amritsinghmehra
 
Java unit2
Java unit2Java unit2
Java unit2
Abhishek Khune
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
Shubham Sharma
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
Ronak Chhajed
 
Inheritance
InheritanceInheritance
Inheritance
Jancirani Selvam
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
MananPasricha
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
Gurpreet singh
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
Gurpreet singh
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Laxman Puri
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
ProfSonaliGholveDoif
 
Inheritance
InheritanceInheritance
Inheritance
prabhat kumar
 
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSINHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
Inheritance
InheritanceInheritance
Inheritance
Padma Kannan
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
FALLEE31188
 

Similar to c++Inheritance.pdf (20)

MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Java unit2
Java unit2Java unit2
Java unit2
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
Inheritance
InheritanceInheritance
Inheritance
 
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSINHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 

Recently uploaded

Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
edwin408357
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
PIMR BHOPAL
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
Prakhyath Rai
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 

Recently uploaded (20)

Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 

c++Inheritance.pdf

  • 1. INHERITANCE in C++ BY, Ranjana Thakuria Assistant Professor, Department of CSE, SVCE Bengaluru
  • 2. HAPPY BIRTH DAY INHERITANCE & Birthday Cake Ranjana Thakuria, CSE Department
  • 3. INHERITANCE-Example Ranjana Thakuria, CSE Department Children inherits property (genetic characteristics ) from their parents
  • 4. INHERITANCE Ranjana Thakuria, CSE Department  The capability of a class to derive properties and characteristics from another class.  New classes are created from the existing classes.  Reusability can be achieved through inheritance.  The new class created is called derived class or child class or Sub Class  The existing class is known as the base class or parent class or Super class.  The derived class now is said to be inherited from the base class.
  • 5. INHERITANCE & Birthday Cake Ranjana Thakuria, CSE Department BASE DERIVED FROM BASE
  • 6. When to use inheritance? Ranjana Thakuria, CSE Department BASE CLASS DERIVED CLASSes INHERITANCE
  • 7. Implementing Inheritance in C++: Ranjana Thakuria, CSE Department Syntax: class <derived_class_name> : <access-specifier> <base_class_name> { //body } keyword to create a new class name of the base class Public/ Private/ Protected; private by default new class name, which will inherit the base class Example: class B: protected A{ ……… };
  • 8. Implementing Inheritance Ranjana Thakuria, CSE Department  Example: class A{ int a; ……….. }; class B: protected A{ int b; ……… }; A A B B Does not inherit a full parent object
  • 9. Implementing Inheritance Ranjana Thakuria, CSE Department  Accessibility: class A{ private: int x; protacted: int y; public: int z; }; class derived: A{ //y, z accessible }; class general{ // z accessible };
  • 10. Modes of Inheritance Ranjana Thakuria, CSE Department Public Mode: If derive a subclass from a public base class, public members public protected members protected Protected Mode: If we derive a subclass from a Protected base class public member  protected protected members protected Private Mode: public member  private protected members  private A derived class doesn’t inherit access to private data members. class ABC : private XYZ { }; Or class ABC: XYZ { } class ABC : protected XYZ { }; class ABC : public XYZ { };
  • 11. Mode-wise accessibility table Ranjana Thakuria, CSE Department
  • 12. Example: public Derivation Accessibility Ranjana Thakuria, CSE Department class A { private: int a; protected: int b; public: int c; }; class B: public A { public: void set() { //a=1; //not allowed b=2; //accessible in further derivation only c=3; //accessible in further derivation and others } }; main() { B obj; //obj.a=1//not allowed //obj.b=2;//not allowed obj.c=3; return 0; }
  • 13. Ex: protected Derivation Accessibility Ranjana Thakuria, CSE Department class A { private: int a; protected: int b; public: int c; }; class B: protected A { public: void set() { //a=1; //not allowed b=2; //accessible in further derivation only c=3; //accessible in further derivation only } }; main() { B obj; //obj.a=1//not allowed //obj.b=2;//not allowed //obj.c=3 ;//not allowed return 0; }
  • 14. Ex: private Derivation Accessibility Ranjana Thakuria, CSE Department class A { private: int a; protected: int b; public: int c; }; class B: private A { public: void set() { //a=1; //not allowed b=2; //not accessible in further derivation c=3; //not accessible in further derivation } }; main() { B obj; //obj.a=1//not allowed //obj.b=2;//not allowed //obj.c=3 ;//not allowed return 0; }
  • 15. Types of Inheritances in C++ Ranjana Thakuria, CSE Department 1. Single inheritance: 3. Multilevel inheritance: 5.Hybrid inheritance: 2. Multiple inheritance: 4. Hierarchical inheritance: Five Types of Inheritances:
  • 16. Single Inheritance: Ranjana Thakuria, CSE Department  A class is allowed to inherit from only one class.  Syntax: class subclass_name : access_mode base_class { // body of subclass };  Example: class A { ... .. ... }; class B: public A { ... .. ... };
  • 17. Single Inheritance: Ranjana Thakuria, CSE Department  A class is allowed to inherit from only one class.  Syntax: class subclass_name : access_mode base_class { // body of subclass };  Example: class A { ... .. ... }; class B: public A { ... .. ... };
  • 18. Single Inheritance- Example : Ranjana Thakuria, CSE Department class A { protected: int a; public: void set_A(int x) { a=x; } void disp_A() { cout<<endl<<"Value of A="<<a; } }; class B: public A { int b; public: void set_B(int x, int y) { set_A(x); b=y; } void disp_B() { disp_A(); cout<<endl<<"Value of B="<<b; } }; main() { B b; b.set_B(5,10); b.disp_B(); return 0; } OUTPUT Value of A=5 Value of B=10
  • 19. Multiple Inheritance: Ranjana Thakuria, CSE Department  A class can inherit from more than one class.  i.e one subclass is inherited from more than one base class. Syntax: class subclass_name : access_mode1 base_class1, access_mode2 base_class2 { // body of subclass };
  • 20. Multiple Inheritance: Ranjana Thakuria, CSE Department  Example: class A { ... .. ... }; class B { ... .. ... }; class C: public A, protected B { ... .. ... };
  • 21. Multiple Inheritance- Example : Ranjana Thakuria, CSE Department // first base class class Vehicle { public: Vehicle() { cout << "This is a Vehiclen"; } }; // second base class class FourWheeler { public: FourWheeler(){ cout << "This is a 4 wheeler Vehiclen"; } }; // sub class derived from two base classes class Car : public Vehicle, public FourWheeler { public: Car(){cout<<"nConstructed Car";} }; // main function int main() { // Creating object of sub class will // invoke the constructor of base classes. Car obj; return 0; } OUTPUT This is a Vehicle This is a 4 wheeler Vehicle Constructed Car
  • 22. MultiLevel Inheritance: Ranjana Thakuria, CSE Department  A class is derived from another derived class.  or one subclass is inherited from another sub class.  i.e, two levels of single inheritance
  • 23. MultiLevel Inheritance: Ranjana Thakuria, CSE Department  Example: class A { ... .. ... }; class B : public A { ... .. ... }; class C: protected B { ... .. ... };
  • 24. MultiLevel Inheritance: Ranjana Thakuria, CSE Department Example: #include<iostream> using namespace std; class A { protected: int a; public: void set_A(){ cout<<"Enter the Value of A="; cin>>a; } void disp_A(){ cout<<endl<<"Value of A="<<a; } }; class B: public A { protected: int b; public: void set_B() { cout<<"Enter the Value of B="; cin>>b; } void disp_B() { cout<<endl<<"Value of B="<<b; } }; class C: public B { int c,p; public: void set_C() { cout<<"Enter the Value of C="; cin>>c; } void disp_C() { cout<<endl<<"Value of C="<<c; } void cal_product() { p=a*b*c; cout<<endl<<"Product of "<<a<<" * "<<b<<" * "<<c<<" = "<<p; } }; main() { C _c; _c.set_A(); _c.set_B(); _c.set_C(); _c.disp_A(); _c.disp_B(); _c.disp_C(); _c.cal_product(); return 0; } OUTPUT: Value of A=10 Value of B=20 Value of C=30 Product of 10 * 20 * 30 = 6000
  • 25. Hierarchical Inheritance: Ranjana Thakuria, CSE Department  More than one subclass is inherited from a single base class.  i.e. more than one derived class is created from a single base class.
  • 26. Hierarchical Inheritance: Ranjana Thakuria, CSE Department  Example: class B { ... .. ... }; class A : public B { ... .. ... }; class C: protected B { ... .. ... };
  • 27. Hierarchical Inheritance: Ranjana Thakuria, CSE Department  Example: // base class class Vehicle { public: Vehicle() { cout << "This is a Vehiclen"; } }; // first sub class class Car : public Vehicle { }; // second sub class class Bus : public Vehicle { }; // main function int main() { // Creating object of sub class will // invoke the constructor of base class. Car obj1; Bus obj2; return 0; } Output This is a Vehicle This is a Vehicle
  • 28. Hybrid Inheritance: Ranjana Thakuria, CSE Department  Implemented by combining more than one type of inheritance.  Example: Combining Hierarchical inheritance and Multiple Inheritance.  Below image shows the combination of hierarchical and multiple inheritances:
  • 29. Hybrid Inheritance: Ranjana Thakuria, CSE Department Example: // C++ program for Hybrid Inheritance #include <iostream> using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehiclen"; } }; // base class class Fare { public: Fare() { cout << "Fare of Vehiclen"; } }; // first sub class class Car : public Vehicle { }; // second sub class class Bus : public Vehicle, public Fare { }; // main function int main() { // Creating object of sub class will // invoke the constructor of base class. Bus obj2; return 0; } OUTPUT: This is a Vehicle Fare of Vehicle
  • 30. Multipath inheritance: Ranjana Thakuria, CSE Department  A special case of hybrid inheritance:  A derived class with two base classes and these two base classes have one common base class .  Ambiguity can arise in this type of inheritance.  Ambiguity can be resolved in two ways:  using scope resolution operator  using virtual base class
  • 31. Multipath Ambiguity resolve: Ranjana Thakuria, CSE Department  Avoiding ambiguity using the scope resolution operator: obj.ClassB::a = 10; // Statement 3 obj.ClassC::a = 100; // Statement 4
  • 32. Multipath Ambiguity resolve: Ranjana Thakuria, CSE Department 2) Avoiding ambiguity using the virtual base class: Class-D has only one copy of ClassA class ClassA{ public: int a; }; class ClassB : virtual public ClassA{ public: int b; }; class ClassC : virtual public ClassA{ public: int c; }; class ClassD : public ClassB, public ClasC{ public: int d; }; int main() { ClassD obj; obj.a = 10; // Statement 3 obj.a = 100; // Statement 4 obj.b = 20; obj.c = 30; obj.d = 40; cout << "n a : " << obj.a; cout << "n b : " << obj.b; cout << "n c : " << obj.c; cout << "n d : " << obj.d << 'n'; }
  • 33. Inheritance and Constructors: Ranjana Thakuria, CSE Department  Constructors can be inherited.  The constructors of inherited classes are called in the same order in which they are inherited. Example Constructors in inheritance: class A { A(){} ... .. ... }; class B: public A { B():A(){} ... .. ... };
  • 34. Order of Inheritance- Constructors : Ranjana Thakuria, CSE Department class A1{ public: A1(){ cout << "Constructor of the base class A1 n"; } }; class A2{ public: A2(){ cout << "Constructor of the base class A2 n"; } }; 3 1 2 class S: public A1, virtual A2 { public: S(): A1(), A2(){ cout << "Constructor of the derived class S n"; } }; // Driver code int main() { S obj; return 0; }
  • 35. THANK YOU Ranjana Thakuria, CSE Department