SlideShare a Scribd company logo
1 of 35
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 computerssuser6f3c8a
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxDeepasCSE
 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Abhishek Khune
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interfaceShubham Sharma
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaMananPasricha
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
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 SYLLABUSVenugopalavarma Raja
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphismFALLEE31188
 

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

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 

Recently uploaded (20)

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 

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