SlideShare a Scribd company logo
Inheritance in C++
Dr.Mirza Waseem Hussain 1
By: Dr. Mirza Waseem Hussain
Lecturer Computer Science
Table of Contents
1. Concept of Inheritance
2. Advantages of Inheritance
3. General Syntax of Inheritance
4. Inheritance Access specifier/ Visibility modes.
Public mode.
Protected mode.
Private mode.
Dr.Mirza Waseem Hussain 2
1. Concept of inheritance:
Inheritance is one of the feature of Object Oriented Programming
System(OOPs), it allows the child class to acquire the properties (the data
members) and functionality (the member functions) of parent class.
Inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically.
The technique of deriving a new class from an old one is called inheritance.
Inheritance is a mechanism in which one class acquires the property of
another class.
 For example, a child inherits the traits of his/her parents.
It allows user to create a new class (derived class, subclass, child class) from
an existing class(base class, super class, parent class).
 The derived class inherits all the features from the base class and can have additional
features of its own.
Dr.Mirza Waseem Hussain 3
2. Advantages of inheritance:
a) Reusability
When child class inherits the properties and functionality of parent class, we need not to
write the same code again in child class.
b) Readability
 It’s easier to reuse the code, makes us write the less code and the code becomes much
more readable.
3. General Syntax of inheritance:
class base_class1{ //Member functions of base class };
class base_classN{ //Member functions of base class };
derived_class:: visibility_mode base_class1, visibility_mode base_class2, ….,
visibility_mode base_classN { ……. //Member functions of derived
class.
};
Dr.Mirza Waseem Hussain 4
Note: A class that inherits another class is known
as child class, it is also known as derived class or
subclass.
Note: The class that is being inherited by other
class is known as parent class, super class or base
class.
4. Inheritance Access specifier/ Visibilty modes:
Access Specifiers defines the visibility of the Property /Fields/ Variable/ Methods of a
class.
Access modifiers (or access specifiers) are keywords in object-oriented languages that
set the accessibility of classes, methods, and other members.
There are three types of Access specifiers in C++ to define the visibility modes of
inheritance.
• Private.
• Public .
• Protected.
A. Public mode:
If we derive a sub class from a public base class. Then the public member of the base
class will become public in the derived class and protected members of the base class
will become protected in derived class.
syntax:
class derive_class::public base_class{ … //Member functions of derived class
};
Dr.Mirza Waseem Hussain 5
B. Protected:
If we derive a sub class from a Protected base class. Then both public member
and protected members of the base class will become protected in derived class.
syntax:
class derive_class::protected base_class{ … //Member functions of derived class
};
C. Private:
If we derive a sub class from a Private base class. Then both public member and
protected members of the base class will become Private in derived class.
syntax:
class derive_class::private base_class{ … //Member functions of derived class
};
Dr.Mirza Waseem Hussain 6
Note:The private members in the base class cannot be directly accessed in the derived class, while
public and protected members can be directly accessed.
Table 1 summarizes the visibility modes of inheritance.
Dr.Mirza Waseem Hussain 7
Base
Class
Derived
Class
Public
Public
Protected
Protected
Private
Public Protected
Private
Protected Protected Private
Private
Not Accessible Not Accessible Not Accessible
Table 1: Visibility modes of inheritance
Max(L1,L2)
L2
L1
L2
L3
L1 L2 L3
Max(L2,L3)
L3
// C++ Implementation to show that a derived class
// doesn’t inherit access to private data members.
// However, it does inherit a full parent object
class A
{
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A
{
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A
{
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A // 'private' is default for classes
{
// x is private
// y is private
// z is not accessible from D
};
Dr.Mirza Waseem Hussain 8
Dr.Mirza Waseem Hussain 9
Dr.Mirza Waseem Hussain 10
Source:
• https://www.javatpoint.com/cpp-inheritance
• https://www.w3schools.in/cplusplus-tutorial/inheritance/
• https://www.tutorialcup.com/cplusplus/inheritance.htm
• https://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/
• https://beginnersbook.com/2017/08/cpp-inheritance/
• https://www.programiz.com/cpp-programming/inheritance
• https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
• https://www.geeksforgeeks.org/inheritance-in-c/
Dr.Mirza Waseem Hussain 11
Dr. Mirza Waseem Hussain 12

More Related Content

What's hot

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Friend functions
Friend functions Friend functions
Friend functions Megha Singh
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
Dyah Fajar Nur Rohmah
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
Ronak Chhajed
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Packages in java
Packages in javaPackages in java
Packages in java
Kavitha713564
 
concept of oops
concept of oopsconcept of oops
concept of oops
prince sharma
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Class or Object
Class or ObjectClass or Object
Class or Object
Rahul Bathri
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
Gajendra Singh Thakur
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
Nicole Ryan
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Object and class
Object and classObject and class
Object and class
mohit tripathi
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
ProfSonaliGholveDoif
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 

What's hot (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Friend functions
Friend functions Friend functions
Friend functions
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Packages in java
Packages in javaPackages in java
Packages in java
 
concept of oops
concept of oopsconcept of oops
concept of oops
 
Java- Nested Classes
Java- Nested ClassesJava- Nested Classes
Java- Nested Classes
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Object and class
Object and classObject and class
Object and class
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 

Similar to Inheritance in c++ part1

lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
preetirawat242004
 
Mca 2nd sem u-3 inheritance
Mca 2nd  sem u-3 inheritanceMca 2nd  sem u-3 inheritance
Mca 2nd sem u-3 inheritance
Rai University
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
 
Bca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritanceBca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritance
Rai University
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
SadiqullahGhani1
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
NAVANEETCHATURVEDI2
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Shweta Shah
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
sushamaGavarskar1
 
Inheritance
InheritanceInheritance
Inheritance
prashant prath
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
MASQ Technologies
 
Inheritance
InheritanceInheritance
Inheritance
Amit Dixit
 
Inheritance
InheritanceInheritance
Inheritance
Munsif Ullah
 
C++ programming public, private inheritance
C++ programming public, private inheritanceC++ programming public, private inheritance
C++ programming public, private inheritance
bilalisb999
 
inheritance
inheritanceinheritance
inheritance
Amir_Mukhtar
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 

Similar to Inheritance in c++ part1 (20)

lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Mca 2nd sem u-3 inheritance
Mca 2nd  sem u-3 inheritanceMca 2nd  sem u-3 inheritance
Mca 2nd sem u-3 inheritance
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
Bca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritanceBca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritance
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ programming public, private inheritance
C++ programming public, private inheritanceC++ programming public, private inheritance
C++ programming public, private inheritance
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 

More from Mirza Hussain

Constructive Teaching & Learning with Technology( CTLT.pdf
Constructive Teaching & Learning with Technology( CTLT.pdfConstructive Teaching & Learning with Technology( CTLT.pdf
Constructive Teaching & Learning with Technology( CTLT.pdf
Mirza Hussain
 
Generations of computers
Generations of computersGenerations of computers
Generations of computers
Mirza Hussain
 
History of computers
History of computersHistory of computers
History of computers
Mirza Hussain
 
Destructors
DestructorsDestructors
Destructors
Mirza Hussain
 
Constructors
ConstructorsConstructors
Constructors
Mirza Hussain
 
Library function in c++ specially designed for clas 11th students
Library function in c++ specially designed for clas 11th studentsLibrary function in c++ specially designed for clas 11th students
Library function in c++ specially designed for clas 11th students
Mirza Hussain
 

More from Mirza Hussain (6)

Constructive Teaching & Learning with Technology( CTLT.pdf
Constructive Teaching & Learning with Technology( CTLT.pdfConstructive Teaching & Learning with Technology( CTLT.pdf
Constructive Teaching & Learning with Technology( CTLT.pdf
 
Generations of computers
Generations of computersGenerations of computers
Generations of computers
 
History of computers
History of computersHistory of computers
History of computers
 
Destructors
DestructorsDestructors
Destructors
 
Constructors
ConstructorsConstructors
Constructors
 
Library function in c++ specially designed for clas 11th students
Library function in c++ specially designed for clas 11th studentsLibrary function in c++ specially designed for clas 11th students
Library function in c++ specially designed for clas 11th students
 

Recently uploaded

Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 

Recently uploaded (20)

Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 

Inheritance in c++ part1

  • 1. Inheritance in C++ Dr.Mirza Waseem Hussain 1 By: Dr. Mirza Waseem Hussain Lecturer Computer Science
  • 2. Table of Contents 1. Concept of Inheritance 2. Advantages of Inheritance 3. General Syntax of Inheritance 4. Inheritance Access specifier/ Visibility modes. Public mode. Protected mode. Private mode. Dr.Mirza Waseem Hussain 2
  • 3. 1. Concept of inheritance: Inheritance is one of the feature of Object Oriented Programming System(OOPs), it allows the child class to acquire the properties (the data members) and functionality (the member functions) of parent class. Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. The technique of deriving a new class from an old one is called inheritance. Inheritance is a mechanism in which one class acquires the property of another class.  For example, a child inherits the traits of his/her parents. It allows user to create a new class (derived class, subclass, child class) from an existing class(base class, super class, parent class).  The derived class inherits all the features from the base class and can have additional features of its own. Dr.Mirza Waseem Hussain 3
  • 4. 2. Advantages of inheritance: a) Reusability When child class inherits the properties and functionality of parent class, we need not to write the same code again in child class. b) Readability  It’s easier to reuse the code, makes us write the less code and the code becomes much more readable. 3. General Syntax of inheritance: class base_class1{ //Member functions of base class }; class base_classN{ //Member functions of base class }; derived_class:: visibility_mode base_class1, visibility_mode base_class2, …., visibility_mode base_classN { ……. //Member functions of derived class. }; Dr.Mirza Waseem Hussain 4 Note: A class that inherits another class is known as child class, it is also known as derived class or subclass. Note: The class that is being inherited by other class is known as parent class, super class or base class.
  • 5. 4. Inheritance Access specifier/ Visibilty modes: Access Specifiers defines the visibility of the Property /Fields/ Variable/ Methods of a class. Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. There are three types of Access specifiers in C++ to define the visibility modes of inheritance. • Private. • Public . • Protected. A. Public mode: If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class. syntax: class derive_class::public base_class{ … //Member functions of derived class }; Dr.Mirza Waseem Hussain 5
  • 6. B. Protected: If we derive a sub class from a Protected base class. Then both public member and protected members of the base class will become protected in derived class. syntax: class derive_class::protected base_class{ … //Member functions of derived class }; C. Private: If we derive a sub class from a Private base class. Then both public member and protected members of the base class will become Private in derived class. syntax: class derive_class::private base_class{ … //Member functions of derived class }; Dr.Mirza Waseem Hussain 6 Note:The private members in the base class cannot be directly accessed in the derived class, while public and protected members can be directly accessed.
  • 7. Table 1 summarizes the visibility modes of inheritance. Dr.Mirza Waseem Hussain 7 Base Class Derived Class Public Public Protected Protected Private Public Protected Private Protected Protected Private Private Not Accessible Not Accessible Not Accessible Table 1: Visibility modes of inheritance Max(L1,L2) L2 L1 L2 L3 L1 L2 L3 Max(L2,L3) L3
  • 8. // C++ Implementation to show that a derived class // doesn’t inherit access to private data members. // However, it does inherit a full parent object class A { public: int x; protected: int y; private: int z; }; class B : public A { // x is public // y is protected // z is not accessible from B }; class C : protected A { // x is protected // y is protected // z is not accessible from C }; class D : private A // 'private' is default for classes { // x is private // y is private // z is not accessible from D }; Dr.Mirza Waseem Hussain 8
  • 11. Source: • https://www.javatpoint.com/cpp-inheritance • https://www.w3schools.in/cplusplus-tutorial/inheritance/ • https://www.tutorialcup.com/cplusplus/inheritance.htm • https://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/ • https://beginnersbook.com/2017/08/cpp-inheritance/ • https://www.programiz.com/cpp-programming/inheritance • https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm • https://www.geeksforgeeks.org/inheritance-in-c/ Dr.Mirza Waseem Hussain 11
  • 12. Dr. Mirza Waseem Hussain 12