SlideShare a Scribd company logo
1 of 60
Inheritance
10-3
What is Inheritance?
Generalization vs. Specialization
• Real-life objects are typically specialized versions of
other more general objects.
• The term “insect” describes a very general type of
creature with numerous characteristics.
• Grasshoppers and bumblebees are insects
– They share the general characteristics of an insect.
– However, they have special characteristics of their own.
• grasshoppers have a jumping ability, and
• bumblebees have a stinger.
• Grasshoppers and bumblebees are specialized
versions of an insect.
Inheritance
10-5
The “is a” Relationship
• The relationship between a superclass and an
inherited class is called an “is a” relationship.
– A grasshopper “is a” insect.
– A poodle “is a” dog.
– A car “is a” vehicle.
• A specialized object has:
– all of the characteristics of the general object, plus
– additional characteristics that make it special.
• In object-oriented programming, inheritance is used
to create an “is a” relationship among classes.
10-6
The “is a” Relationship
• We can extend the capabilities of a class.
• Inheritance involves a superclass and a subclass.
– The superclass is the general class and
– the subclass is the specialized class.
• The subclass is based on, or extended from, the superclass.
– Superclasses are also called base classes, and
– subclasses are also called derived classes.
• The relationship of classes can be thought of as parent
classes and child classes.
10-7
Inheritance, Fields and Methods
• Members of the superclass that are marked private:
– are not inherited by the subclass,
– exist in memory when the object of the subclass is created
– may only be accessed from the subclass by public methods
of the superclass.
• Members of the superclass that are marked public:
– are inherited by the subclass, and
– may be directly accessed from the subclass.
Inheritance
• It is the Process of creating a New class from an existing class.
• The Existing class is called Base or Parent class.
• The New class is called as Child or Derived Class
base
subclass1 subclass2
Advantages
 It permits code reusability. So, save time and
increase the program reliability.
 Improve Program Reliability
 It Permits code sharing
Syntax for Inheritance
class <derived classname > : access-specifier <base classname >
{
---
----
};
Access specifier
• Public - The members declared as Public are accessible
from outside the Class through an object of the class.
• Protected - The members declared as Protected are
accessible from outside the class BUT only in a class
derived from it.
• Private - These members are only accessible from
within the class. No outside Access is allowed.
Derived class
Base Class
Methods
and
Properties
Base class methods
+
Additional methods
Inheritance is the property that allows the
reuse of an existing class to build a new class
Inheritance
Multilevel
Inheritance
Single Inheritance
Multiple Inheritance
Hierarchical
Inheritance
Single Inheritance
A class can be derived from a single base class is called single
inheritance
Person
Employee
Solution to Problem solving sequence
Multilevel Inheritance
Vehicle
Car
Racing car
Person
Employee
Part time
Employee
A class be can derived from a derived class which is known as multilevel
inheritance.
It is the process of creating new class from more than one base
classes.
Syntax :
class <derived class >:<access specifier>
base_class1,<access specifier> base_class2...
{
private :
// members;
protected :
// members;
public :
//memebers;
};
Multiple Inheritance
Person Employee
Teacher
Same Data Member Name in Base and Derived Class
Use Scope resolution operator to access
the data member
Hierarchical Inheritance
Employee
Permanent
Employee
Temporary
Employee
If more than one class is inherited from a base class, it's known as hierarchical
inheritance. In general, all features that are common in child classes are included
in base class in hierarchical inheritance.
Hybrid Inheritance
Hybrid is nothing but the combination of Multilevel
and multiple Inheritance
Order of Constructor Call
Base class constructors are always called in the derived class constructors.
Whenever you create derived class object, first the base class default
constructor is executed and then the derived class's constructor finishes
execution.
Points to Remember
1.Whether derived class's default constructor is called or parameterised is
called, base class's default constructor is always called inside them.
2.To call base class's parameterised constructor inside derived class's
parameterised constructor, we must mention it explicitly while declaring
derived class's parameterized constructor.
Base class Parameterized Constructor in Derived class Constructor
We can explicitly mention to call the Base class's parameterized constructor
when Derived class's parameterized constructor is called.
Constructor call in Multiple Inheritance
Its almost the same, all the Base class's constructors are called inside derived class's
constructor, in the same order in which they are inherited.
class A : public B, public C ;
In this case, first class B constructor will be calledd, then class C constructor and then
class A constructor.
Function Type
Access directly to
Private Protected Public
Class member Yes Yes Yes
Derived class
member
No Yes Yes
Friend Yes Yes Yes
Friend class
member
Yes Yes Yes
Public Inheritance:
All Public members of the Base Class become Public Members of the derived class &
All Protected members of the Base Class become Protected Members of the Derived
Class.
class Base {
public: int a;
protected: int b;
private: int c;
};
class Derived:public Base {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error
}
};
int main() {
Derived obj;
obj.a = 10; //Allowed
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}
Private Inheritance:
All Public members of the Base Class become Private Members of the Derived class &
All Protected members of the Base Class become Private Members of the Derived Class.
Class Base {
public: int a;
protected: int b;
private: int c; };
class Derived:private Base //defaults access specifier is private {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error }
};
class Derived2:public Derived {
void doSomethingMore() {
a = 10; //Not Allowed, Compiler Error, a is private member of Derived now
b = 20; //Not Allowed, Compiler Error, b is private member of Derived now
c = 30; //Not Allowed, Compiler Error }
};
int main() {
Derived obj; obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error }
• Protected Inheritance:
All Public members of the Base Class become Protected Members of the derived
class & All Protected members of the Base Class become Protected Members of the
Derived Class.
Class Base {
public: int a;
protected: int b;
private: int c; };
class Derived:protected Base {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error }
};
class Derived2:public Derived {
void doSomethingMore() {
a = 10; //Allowed, a is protected member inside Derived & Derived2 is public
derivation from Derived, a is now protected member of Derived2
b = 20; //Allowed, b is protected member inside Derived & Derived2 is public
derivation from Derived, b is now protected member of Derived2
c = 30; //Not Allow ed, Compiler Error }
};
int main() {
Derived obj; obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}
Access in Base
Class
Access in
Publically
Derived Class
Access in
Privately
Derived Class
Access in
Protected
Derived Class
Public Public Private Protected
Private
Cannot be
accessed
directly
(only base
inherited class
member
functions can
access)
Cannot be
accessed
directly
(only base
inherited class
member
functions can
access)
Cannot be
accessed
directly
(only base
inherited class
member
functions can
access)
Protected Protected Private Protected
Case study – Railway reservation system
Single Inheritance
Multi level Inheritance
Hybrid Inheritance
session 24_Inheritance.ppt

More Related Content

Similar to session 24_Inheritance.ppt

Similar to session 24_Inheritance.ppt (20)

00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).ppt
 
OOP
OOPOOP
OOP
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritance
   inheritance   inheritance
inheritance
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybridInheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybrid
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
OOP.ppt
OOP.pptOOP.ppt
OOP.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
14_inheritance.ppt
14_inheritance.ppt14_inheritance.ppt
14_inheritance.ppt
 

Recently uploaded

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

session 24_Inheritance.ppt

  • 2.
  • 3. 10-3 What is Inheritance? Generalization vs. Specialization • Real-life objects are typically specialized versions of other more general objects. • The term “insect” describes a very general type of creature with numerous characteristics. • Grasshoppers and bumblebees are insects – They share the general characteristics of an insect. – However, they have special characteristics of their own. • grasshoppers have a jumping ability, and • bumblebees have a stinger. • Grasshoppers and bumblebees are specialized versions of an insect.
  • 5. 10-5 The “is a” Relationship • The relationship between a superclass and an inherited class is called an “is a” relationship. – A grasshopper “is a” insect. – A poodle “is a” dog. – A car “is a” vehicle. • A specialized object has: – all of the characteristics of the general object, plus – additional characteristics that make it special. • In object-oriented programming, inheritance is used to create an “is a” relationship among classes.
  • 6. 10-6 The “is a” Relationship • We can extend the capabilities of a class. • Inheritance involves a superclass and a subclass. – The superclass is the general class and – the subclass is the specialized class. • The subclass is based on, or extended from, the superclass. – Superclasses are also called base classes, and – subclasses are also called derived classes. • The relationship of classes can be thought of as parent classes and child classes.
  • 7. 10-7 Inheritance, Fields and Methods • Members of the superclass that are marked private: – are not inherited by the subclass, – exist in memory when the object of the subclass is created – may only be accessed from the subclass by public methods of the superclass. • Members of the superclass that are marked public: – are inherited by the subclass, and – may be directly accessed from the subclass.
  • 8. Inheritance • It is the Process of creating a New class from an existing class. • The Existing class is called Base or Parent class. • The New class is called as Child or Derived Class base subclass1 subclass2
  • 9. Advantages  It permits code reusability. So, save time and increase the program reliability.  Improve Program Reliability  It Permits code sharing
  • 10. Syntax for Inheritance class <derived classname > : access-specifier <base classname > { --- ---- };
  • 11. Access specifier • Public - The members declared as Public are accessible from outside the Class through an object of the class. • Protected - The members declared as Protected are accessible from outside the class BUT only in a class derived from it. • Private - These members are only accessible from within the class. No outside Access is allowed.
  • 12. Derived class Base Class Methods and Properties Base class methods + Additional methods Inheritance is the property that allows the reuse of an existing class to build a new class
  • 14. Single Inheritance A class can be derived from a single base class is called single inheritance Person Employee
  • 15.
  • 16.
  • 17. Solution to Problem solving sequence
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23. Multilevel Inheritance Vehicle Car Racing car Person Employee Part time Employee A class be can derived from a derived class which is known as multilevel inheritance.
  • 24.
  • 25.
  • 26. It is the process of creating new class from more than one base classes. Syntax : class <derived class >:<access specifier> base_class1,<access specifier> base_class2... { private : // members; protected : // members; public : //memebers; }; Multiple Inheritance Person Employee Teacher
  • 27.
  • 28.
  • 29. Same Data Member Name in Base and Derived Class
  • 30.
  • 31.
  • 32.
  • 33. Use Scope resolution operator to access the data member
  • 34. Hierarchical Inheritance Employee Permanent Employee Temporary Employee If more than one class is inherited from a base class, it's known as hierarchical inheritance. In general, all features that are common in child classes are included in base class in hierarchical inheritance.
  • 35.
  • 36.
  • 37. Hybrid Inheritance Hybrid is nothing but the combination of Multilevel and multiple Inheritance
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44. Order of Constructor Call Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first the base class default constructor is executed and then the derived class's constructor finishes execution. Points to Remember 1.Whether derived class's default constructor is called or parameterised is called, base class's default constructor is always called inside them. 2.To call base class's parameterised constructor inside derived class's parameterised constructor, we must mention it explicitly while declaring derived class's parameterized constructor.
  • 45.
  • 46.
  • 47. Base class Parameterized Constructor in Derived class Constructor We can explicitly mention to call the Base class's parameterized constructor when Derived class's parameterized constructor is called.
  • 48.
  • 49. Constructor call in Multiple Inheritance Its almost the same, all the Base class's constructors are called inside derived class's constructor, in the same order in which they are inherited. class A : public B, public C ; In this case, first class B constructor will be calledd, then class C constructor and then class A constructor.
  • 50. Function Type Access directly to Private Protected Public Class member Yes Yes Yes Derived class member No Yes Yes Friend Yes Yes Yes Friend class member Yes Yes Yes
  • 51. Public Inheritance: All Public members of the Base Class become Public Members of the derived class & All Protected members of the Base Class become Protected Members of the Derived Class. class Base { public: int a; protected: int b; private: int c; }; class Derived:public Base { void doSomething() { a = 10; //Allowed b = 20; //Allowed c = 30; //Not Allowed, Compiler Error } }; int main() { Derived obj; obj.a = 10; //Allowed obj.b = 20; //Not Allowed, Compiler Error obj.c = 30; //Not Allowed, Compiler Error }
  • 52.
  • 53. Private Inheritance: All Public members of the Base Class become Private Members of the Derived class & All Protected members of the Base Class become Private Members of the Derived Class. Class Base { public: int a; protected: int b; private: int c; }; class Derived:private Base //defaults access specifier is private { void doSomething() { a = 10; //Allowed b = 20; //Allowed c = 30; //Not Allowed, Compiler Error } }; class Derived2:public Derived { void doSomethingMore() { a = 10; //Not Allowed, Compiler Error, a is private member of Derived now b = 20; //Not Allowed, Compiler Error, b is private member of Derived now c = 30; //Not Allowed, Compiler Error } }; int main() { Derived obj; obj.a = 10; //Not Allowed, Compiler Error obj.b = 20; //Not Allowed, Compiler Error obj.c = 30; //Not Allowed, Compiler Error }
  • 54. • Protected Inheritance: All Public members of the Base Class become Protected Members of the derived class & All Protected members of the Base Class become Protected Members of the Derived Class. Class Base { public: int a; protected: int b; private: int c; }; class Derived:protected Base { void doSomething() { a = 10; //Allowed b = 20; //Allowed c = 30; //Not Allowed, Compiler Error } }; class Derived2:public Derived { void doSomethingMore() { a = 10; //Allowed, a is protected member inside Derived & Derived2 is public derivation from Derived, a is now protected member of Derived2 b = 20; //Allowed, b is protected member inside Derived & Derived2 is public derivation from Derived, b is now protected member of Derived2 c = 30; //Not Allow ed, Compiler Error } };
  • 55. int main() { Derived obj; obj.a = 10; //Not Allowed, Compiler Error obj.b = 20; //Not Allowed, Compiler Error obj.c = 30; //Not Allowed, Compiler Error }
  • 56. Access in Base Class Access in Publically Derived Class Access in Privately Derived Class Access in Protected Derived Class Public Public Private Protected Private Cannot be accessed directly (only base inherited class member functions can access) Cannot be accessed directly (only base inherited class member functions can access) Cannot be accessed directly (only base inherited class member functions can access) Protected Protected Private Protected
  • 57. Case study – Railway reservation system Single Inheritance