SlideShare a Scribd company logo
1 of 12
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 ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
Héritage et polymorphisme- Jihen HEDHLI
Héritage et polymorphisme- Jihen HEDHLIHéritage et polymorphisme- Jihen HEDHLI
Héritage et polymorphisme- Jihen HEDHLIJihenHedhli1
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsEng Teong Cheah
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programmingNeelesh Shukla
 
CS8592 Object Oriented Analysis & Design - UNIT I
CS8592 Object Oriented Analysis & Design - UNIT ICS8592 Object Oriented Analysis & Design - UNIT I
CS8592 Object Oriented Analysis & Design - UNIT Ipkaviya
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple InheritanceBhavyaJain137
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceOUM SAOKOSAL
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...cprogrammings
 
Access specifier
Access specifierAccess specifier
Access specifierzindadili
 

What's hot (20)

Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Héritage et polymorphisme- Jihen HEDHLI
Héritage et polymorphisme- Jihen HEDHLIHéritage et polymorphisme- Jihen HEDHLI
Héritage et polymorphisme- Jihen HEDHLI
 
Inheritance
InheritanceInheritance
Inheritance
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
CS8592 Object Oriented Analysis & Design - UNIT I
CS8592 Object Oriented Analysis & Design - UNIT ICS8592 Object Oriented Analysis & Design - UNIT I
CS8592 Object Oriented Analysis & Design - UNIT I
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
inheritance
inheritanceinheritance
inheritance
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Single inheritance
Single inheritanceSingle inheritance
Single inheritance
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Access specifier
Access specifierAccess specifier
Access specifier
 

Similar to Inheritance in c++ part1

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
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ Inheritance.pptx
C++ Inheritance.pptxC++ Inheritance.pptx
C++ Inheritance.pptx
 

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.pdfMirza Hussain
 
Generations of computers
Generations of computersGenerations of computers
Generations of computersMirza Hussain
 
History of computers
History of computersHistory of computers
History of computersMirza 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 studentsMirza 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

Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 

Recently uploaded (20)

young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 

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