SlideShare a Scribd company logo
1 of 40
Inheritance
Inheritance 
• A programming technique that is used to reuse 
an existing class to build a new class is known as 
inheritance. 
• The new class inherits all the behavior of the 
original class. 
• The existing class that is reused to create a new 
class is known as super class or parent class. 
• The new class that inherits the properties and 
functions of an existing class is known as 
subclass, derived class or child class. 
• The inheritance relationship between the classes 
of a program is called a class hierarchy.
• Inheritance is one of the most powerful 
features of object-oriented programming. 
• The basic principal of inheritance is that each 
subclass shares common properties with the 
class from which it is derived. 
• The child class inherits all capabilities of the 
parent class and can add its own capabilities.
• Suppose we have a class named vehicle. 
• The subclasses of this class may share similar 
properties such as wheels and motor etc. 
• Additionally, a subclass may have its own 
particular characteristics. 
• For example, a subclass Bus may have seats for 
people but another subclass Truck may have 
space to carry goods. 
• A class of animals can be divided into sub 
classes like mammals, insects etc. 
• A class of vehicles can be divided into cars, 
trucks, buses, and motorcycles.
Vehical 
Bus Truck Motorcycle 
• The above figure shows that Vehicle is parent 
class and Bus, Truck and Motorcycle are three sub 
classes. The upward arrows indicate that the 
subclasses are derived from parent Vehicle class.
Advantages of Inheritance 
Some important advantages of inheritance are as 
follows: 
1. Reusability: 
Inheritance allows the developer to reuse existing 
code in many situations. A class can be created once 
and it can be reused again and again to create many 
sub classes. 
2. Saves Time and Effort: 
inheritance saves a lot of time and effort to write the 
same classes again. 
3. Increases Program Structure and Reliability: 
A super class is already compiled and tested properly. 
This class can be used in a new application without 
compiling it again. The use of existing class increases 
program reliability.
Categories of Inheritance 
• There are two categories of inheritance 
1. Single Inheritance: 
A type of inheritance in which a child class is derived 
from single parent class is known as single inheritance. 
The child class in this inheritance inherits all data 
members and member functions of the parent class. It 
can also add further capabilities of its own. 
Parent 
Child
2. Multiple Inheritance: 
A type of inheritance in which a child class is derived 
from multiple parent classes is known as multiple 
inheritance. The child class in this inheritance inherits 
all data members and member functions of all parent 
classes. It can also add further capabilities of its own. 
Parent2 
Parent1 
Child
Protected Access Specifier 
• The private data members of a class are only accessible 
in the class in which they are declared. 
• The public data members are accessible from 
anywhere in the program. 
• The protected access specifier is different from private 
and public access specifiers. 
• It is specially used in inheritance. 
• It allows a protected data member to be accessed from 
all derived classes but not from anywhere else in the 
program. 
• It means that child class can access all protected data 
members of its parent class.
Specifying a Derived Class 
• The process of specifying derived class is same as 
specifying simple class. 
• Additionally, the reference of parent is specified 
along with derived class name to inherit the 
capabilities of parent class. 
• Syntax: 
class sub_class : specifier parent_class 
{ 
body of the class 
};
class It is keyword that is used to declare a class. 
sub_class It is the name of derived/child/sub class. 
: It creates a relationship between derived 
class and super/parent/base class. 
specifier It indicates the type of inheritance. It can be 
private, public or protected. 
parent_class It indicates the name of parent/super/base 
class that is being inherited.
class Move { 
protected: 
int position; 
public: 
Move() { 
position = 0 ; 
} 
void forward() { 
position++; 
} 
void show() { 
cout<<“Position = “<< 
position <<endl; 
} 
}; 
class Move2 : public Move { 
public: 
void backward() { 
position--; 
} 
}; 
main() { 
Move2 m; 
m.show(); 
m.forward(); 
m.show(); 
m.backward(); 
m.show(); 
}
Accessing Members of Parent Class 
• An important issue in inheritance is the 
accessibility of base class members by the 
objects of derived class. 
• It is known as accessibility. 
• The objects of derived class can access certain 
members of parent class. 
– Accessing Constructors of Parent Class 
– Accessing Member Functions of Parent Class
Accessing Constructors of Parent Class 
• The objects of derived class can access certain 
constructors of parent class. 
• If there is no constructor in derived class the 
compiler automatically uses the constructor of 
parent class. 
• The objects of derived class can automatically 
access the constructors of parent class with no 
parameter. 
• The derived class can also access constructors of 
parent class with parameters by passing values 
to them.
Syntax: 
The syntax of accessing the constructor of parent 
class in derived class is as follows: 
child_constrctr (parameters): parent_constrctr(parameters) 
{ 
body of constructor 
} 
child_constrctr Name of constructor of derived class. 
parent_constrctr Name of constructor of parent class. 
parameters List of parameters passed to the constructor of 
parent class.
class Parent { 
protected: 
int n; 
public: 
Parent() { 
n=0; 
} 
Parent(int p) { 
n=p; 
} 
void show() { 
cout<<“ n = “<<n 
<< endl; 
} 
}; 
class Child : public Parent 
{ 
private: 
char ch; 
public: 
Child() : Parent() { 
ch = ‘x’; 
} 
Child(char c, int m) : Parent(m) { 
ch=c; 
} 
void display(){ 
cout<<“ch = “<<ch<<endl; 
} 
};
main() { 
Child obj1, obj2(‘$’, 100); 
cout<<“Obj1 is as follow”<<endl; 
obj1.show(); 
obj1.display(); 
cout<<“Obj2 is as follows”<<endl; 
obj2.show(); 
obj2.display(); 
}
Accessing Member Functions of Parent 
Class 
• The objects of derived class can access all 
member functions of parent class that are 
declared as protected or public.
Write a class Person that has the attributes of 
id, name and address. It has a constructor to 
initialize, a member function to input and a 
member function to display data members. 
Create another class Student that inherits 
Person class. It has additional attributes of roll 
number and marks. It also has member 
function to input and display its data members.
class Person { 
protected: 
int id; 
string name, address; 
public: 
Person() { 
id=0; 
name = “Name”; 
address =“address” 
} 
void getInfo() { 
cout<<“Enter your id”; 
cin>>id; 
cout<<“Enter your name”; 
cin>>name; 
cout<<“Enter your 
address”; 
cin>>address; 
} 
void showInfo() { 
cout<<“your personal 
information” 
cout<<“ id = “<<id<<endl; 
cout<<“Name = 
“<<name<<endl; 
cout<<“Address = 
“<<address<<endl; 
} 
}; 
class Student : public 
Person { 
private: 
int rno, marks; 
public: 
Student() { 
Person:: Person(); 
rno=marks=0; 
}
void getEdu() { 
cout<<“Enter your roll 
no”; 
cin>>rno; 
cout<<“Enter your 
marks”; 
cin>>marks; 
void showEdu() { 
cout<<“Your education 
information is as 
follows”; 
cout<<“Roll no = 
“<<rno<<endl; 
cout<<“Marks = 
“<<marks<<endl; 
} 
}; 
main() { 
Student s; 
s.getInfo(): 
s.getEdu(); 
s.showEdu(); 
}
Function Overriding 
• The process of declaring member function in derived 
class with same name and same signature as in parent 
class is known as function overriding. 
• Function overriding allows the user to use same names 
for calling the member functions of different class. 
• When a member function is overridden in the derived 
class, the object of derived class cannot access the 
function of parent class. 
• However, the function of parent class can be accessed 
by using scope resolution operator.
class Parent { 
protected: 
int n; 
public: 
Parent(int p) 
{ 
n=p; 
} 
void show() { 
cout<<“n = “<<n<<endl; 
} 
}; 
class Child : public Parent { 
private: 
char ch; 
Public: 
Child(char c, int m) : 
Parent(m) { 
ch=c; 
} 
void show() { 
Parent::show(); 
cout<<“ch = “<<ch<<endl; 
} 
}; 
main() 
{ 
Child obj(‘$’, 100); 
obj.show(); 
}
Types of Inheritance 
• A parent class can be inherited using public, 
protected or private type of inheritance. 
• The type of inheritance defines the access 
status of parent class members in the derived 
class. 
• Different types of inheritance are as follows: 
– Public Inheritance 
– Protected Inheritance 
– Private Inheritance
Public Inheritance 
• In public inheritance, the access status of parent class 
members in the derived class remains the same. 
• The public members of parent class become public 
members of derived class. 
• The protected members of parent class become 
protected members of derived class. 
• The private members of parent class become private 
members of derived class. 
• The syntax of defining public inheritance is as follows: 
class child_class : public parent_class { 
body of the class 
}
The accessibility of derived class in public inheritance is as 
follows: 
• The derived class can access the public members of 
parent class. 
• The derived class can access the protected members of 
parent class. 
• The derived class cannot access the private members of 
parent class. 
The accessibility of an object of derived class is as follows: 
• The object of derived class can access the public 
members of parent class. 
• The object of derived class cannot access the protected 
members of parent class. 
• The object of derived class cannot access the private 
members of parent class.
Write a program that declares two 
classes and defines a relationship 
between them using public members.
class parent { 
public: 
int a; 
protected: 
int b; 
private: 
int c; 
}; 
class child : public parent 
{ 
public: 
void in() { 
cout<< “Enter a “; 
cin>>a; 
cout<<“Enter b “; 
cin>>b; 
} 
void out() { 
cout<<“a = “<<a<<endl; 
cout<<“b = “<<b<<endl; 
} 
}; 
main() { 
child obj; 
obj.in(); 
obj.out(); 
}
Protected Inheritance 
• In Protected inheritance, the access status of 
parent class members in the derived class is 
restricted. 
• The public members of parent class become 
protected members of derived class. 
• The protected members of parent class 
become protected members of derived class. 
• The private members of parent class become 
private members of derived class.
• The syntax of defining protected inheritance is 
as follows: 
class child_class : protected parent_class 
{ 
body of the class 
}
The accessibility of derived class in protected inheritance is 
as follows: 
• The derived class can access the public members of parent 
class. 
• The derived class can access the protected members of 
parent class. 
• The derived class cannot access the private members of 
parent class. 
The accessibility of an object of derived class is as follows: 
• The object of derived class cannot access the public 
members of parent class. 
• The object of derived class cannot access the protected 
members of parent class. 
• The object of derived class cannot access the private 
members of parent class.
class parent { 
public: 
int a; 
protected: 
int b; 
private: 
int c; 
}; 
class child : protected parent 
{ 
public: 
void in() { 
cout<< “Enter a “; 
cin>>a; 
cout<<“Enter b “; 
cin>>b; 
} 
void out() { 
cout<<“a = “<<a<<endl; 
cout<<“b = “<<b<<endl; 
} 
}; 
main() { 
child obj; 
obj.in(); 
obj.out(); 
}
Private Inheritance 
• In private inheritance, the access status of parent 
class members in the derived class is restricted. 
• The private, protected and public members of 
parent class all become the private members of 
derived class. 
• The syntax of defining private inheritance is as 
follows: 
class child_class : private parent_class 
{ 
body of the class 
}
The accessibility of derived class in private inheritance is as 
follows: 
• The derived class can access the public members of parent 
class. 
• The derived class can access the protected members of 
parent class. 
• The derived class cannot access the private members of 
parent class. 
The accessibility of an object of derived class is as follows: 
• The object of derived class cannot access the public 
members of parent class. 
• The object of derived class cannot access the protected 
members of parent class. 
• The object of derived class cannot access the private 
members of parent class.
Multilevel Inheritance 
• A type of inheritance in which a class is 
derived from another derived class is called 
multilevel inheritance. 
• In multilevel inheritance, the members of 
parent class are inherited to the child class 
and the members of child class are inherited 
to the grand child class. 
• In this way, the members of parent class and 
child class are combined in grand child class.
• Example: 
class A { 
body of the class 
}; 
class B : public A { 
body of the class 
}; 
class C : public B { 
body of the class 
};
class A { 
private: 
int a; 
public: 
void in() { 
cout<< “Enter a “; 
cin>>a; 
} 
void out() { 
cout<< “The value of a is 
“<<a<<endl; 
} }; 
class B : public A { 
private: 
int b; 
public: 
void in() { 
A::in(); 
cout<<“Enter b “; 
cin>>b; 
} 
void out() { 
A::out(); 
cout<<“The value of b is “<<b <<endl; 
} }; 
class C : public B { 
private : 
int c : 
public : 
void in(); { 
B::in(); 
cout<<“Enter c “; 
cin>>c; 
} 
void out() { 
B::out(); 
cout<<“The value of c is“<<c<<endl; 
} }; 
main() { 
C obj; 
obj.in(); 
obj.out(); 
}
Multiple Inheritance 
• A type of inheritance in which a derived class 
inherit multiple base classes is known as multiple 
inheritance. 
• In multiple inheritance, the derived class 
combines the members of all base classes. 
• Syntax: 
class child_class : specifier Parent_class1, 
specifier parent class2…………….. 
{ 
body of the class 
}
• Example: 
class A { 
body of the class 
}; 
class B { 
body of the class 
}; 
class c : public A, public B { 
body of the class 
};
class A { 
private: 
int a; 
public: 
void in() { 
cout<<“Enter a “; 
cin>>a; 
} 
void out() { 
cout<<“a = “a<<endl; 
} }; 
class B { 
private: 
int b; 
public: 
void input() { 
cout<<“Enter b “; 
cin>>b; 
} 
void output() { 
cout<<“b = “<<b<<endl; } }; 
class C : public A, public B { 
private: 
int c; 
public: 
void get() { 
A::in(); 
b::input(); 
cout<<“Enter c “; 
cin>>c; 
} 
void show() { 
A::out(); 
B::output(); 
cout<<“c = “<<c<<endl; 
} }; 
C obj; 
obj.get(); 
obj.show(); 
}

More Related Content

What's hot

What's hot (20)

Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Java Object Oriented Programming
Java Object Oriented Programming Java Object Oriented Programming
Java Object Oriented Programming
 
Delegetes in c#
Delegetes in c#Delegetes in c#
Delegetes in c#
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Abstract class
Abstract classAbstract class
Abstract class
 
Java- Nested Classes
Java- Nested ClassesJava- Nested Classes
Java- Nested Classes
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Java interface
Java interfaceJava interface
Java interface
 
Thread
ThreadThread
Thread
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Access Modifier.pptx
Access Modifier.pptxAccess Modifier.pptx
Access Modifier.pptx
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
inheritance
inheritanceinheritance
inheritance
 

Viewers also liked

Oop06 6
Oop06 6Oop06 6
Oop06 6schwaa
 
Oop05 6
Oop05 6Oop05 6
Oop05 6schwaa
 
Oop09 6
Oop09 6Oop09 6
Oop09 6schwaa
 
Oop04 6
Oop04 6Oop04 6
Oop04 6schwaa
 
Oop10 6
Oop10 6Oop10 6
Oop10 6schwaa
 
Oop02 6
Oop02 6Oop02 6
Oop02 6schwaa
 
Oop03 6
Oop03 6Oop03 6
Oop03 6schwaa
 
Oop07 6
Oop07 6Oop07 6
Oop07 6schwaa
 
Oop01 6
Oop01 6Oop01 6
Oop01 6schwaa
 

Viewers also liked (9)

Oop06 6
Oop06 6Oop06 6
Oop06 6
 
Oop05 6
Oop05 6Oop05 6
Oop05 6
 
Oop09 6
Oop09 6Oop09 6
Oop09 6
 
Oop04 6
Oop04 6Oop04 6
Oop04 6
 
Oop10 6
Oop10 6Oop10 6
Oop10 6
 
Oop02 6
Oop02 6Oop02 6
Oop02 6
 
Oop03 6
Oop03 6Oop03 6
Oop03 6
 
Oop07 6
Oop07 6Oop07 6
Oop07 6
 
Oop01 6
Oop01 6Oop01 6
Oop01 6
 

Similar to Inheritance

Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdfWaqarRaj1
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Shweta Shah
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptxrayanbabur
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesVinay Kumar
 

Similar to Inheritance (20)

Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance
Inheritance Inheritance
Inheritance
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Only oop
Only oopOnly oop
Only oop
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
OOP
OOPOOP
OOP
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
29c
29c29c
29c
 
29csharp
29csharp29csharp
29csharp
 
27c
27c27c
27c
 

Recently uploaded

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 

Recently uploaded (20)

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 

Inheritance

  • 2. Inheritance • A programming technique that is used to reuse an existing class to build a new class is known as inheritance. • The new class inherits all the behavior of the original class. • The existing class that is reused to create a new class is known as super class or parent class. • The new class that inherits the properties and functions of an existing class is known as subclass, derived class or child class. • The inheritance relationship between the classes of a program is called a class hierarchy.
  • 3. • Inheritance is one of the most powerful features of object-oriented programming. • The basic principal of inheritance is that each subclass shares common properties with the class from which it is derived. • The child class inherits all capabilities of the parent class and can add its own capabilities.
  • 4. • Suppose we have a class named vehicle. • The subclasses of this class may share similar properties such as wheels and motor etc. • Additionally, a subclass may have its own particular characteristics. • For example, a subclass Bus may have seats for people but another subclass Truck may have space to carry goods. • A class of animals can be divided into sub classes like mammals, insects etc. • A class of vehicles can be divided into cars, trucks, buses, and motorcycles.
  • 5. Vehical Bus Truck Motorcycle • The above figure shows that Vehicle is parent class and Bus, Truck and Motorcycle are three sub classes. The upward arrows indicate that the subclasses are derived from parent Vehicle class.
  • 6. Advantages of Inheritance Some important advantages of inheritance are as follows: 1. Reusability: Inheritance allows the developer to reuse existing code in many situations. A class can be created once and it can be reused again and again to create many sub classes. 2. Saves Time and Effort: inheritance saves a lot of time and effort to write the same classes again. 3. Increases Program Structure and Reliability: A super class is already compiled and tested properly. This class can be used in a new application without compiling it again. The use of existing class increases program reliability.
  • 7. Categories of Inheritance • There are two categories of inheritance 1. Single Inheritance: A type of inheritance in which a child class is derived from single parent class is known as single inheritance. The child class in this inheritance inherits all data members and member functions of the parent class. It can also add further capabilities of its own. Parent Child
  • 8. 2. Multiple Inheritance: A type of inheritance in which a child class is derived from multiple parent classes is known as multiple inheritance. The child class in this inheritance inherits all data members and member functions of all parent classes. It can also add further capabilities of its own. Parent2 Parent1 Child
  • 9. Protected Access Specifier • The private data members of a class are only accessible in the class in which they are declared. • The public data members are accessible from anywhere in the program. • The protected access specifier is different from private and public access specifiers. • It is specially used in inheritance. • It allows a protected data member to be accessed from all derived classes but not from anywhere else in the program. • It means that child class can access all protected data members of its parent class.
  • 10. Specifying a Derived Class • The process of specifying derived class is same as specifying simple class. • Additionally, the reference of parent is specified along with derived class name to inherit the capabilities of parent class. • Syntax: class sub_class : specifier parent_class { body of the class };
  • 11. class It is keyword that is used to declare a class. sub_class It is the name of derived/child/sub class. : It creates a relationship between derived class and super/parent/base class. specifier It indicates the type of inheritance. It can be private, public or protected. parent_class It indicates the name of parent/super/base class that is being inherited.
  • 12. class Move { protected: int position; public: Move() { position = 0 ; } void forward() { position++; } void show() { cout<<“Position = “<< position <<endl; } }; class Move2 : public Move { public: void backward() { position--; } }; main() { Move2 m; m.show(); m.forward(); m.show(); m.backward(); m.show(); }
  • 13. Accessing Members of Parent Class • An important issue in inheritance is the accessibility of base class members by the objects of derived class. • It is known as accessibility. • The objects of derived class can access certain members of parent class. – Accessing Constructors of Parent Class – Accessing Member Functions of Parent Class
  • 14. Accessing Constructors of Parent Class • The objects of derived class can access certain constructors of parent class. • If there is no constructor in derived class the compiler automatically uses the constructor of parent class. • The objects of derived class can automatically access the constructors of parent class with no parameter. • The derived class can also access constructors of parent class with parameters by passing values to them.
  • 15. Syntax: The syntax of accessing the constructor of parent class in derived class is as follows: child_constrctr (parameters): parent_constrctr(parameters) { body of constructor } child_constrctr Name of constructor of derived class. parent_constrctr Name of constructor of parent class. parameters List of parameters passed to the constructor of parent class.
  • 16. class Parent { protected: int n; public: Parent() { n=0; } Parent(int p) { n=p; } void show() { cout<<“ n = “<<n << endl; } }; class Child : public Parent { private: char ch; public: Child() : Parent() { ch = ‘x’; } Child(char c, int m) : Parent(m) { ch=c; } void display(){ cout<<“ch = “<<ch<<endl; } };
  • 17. main() { Child obj1, obj2(‘$’, 100); cout<<“Obj1 is as follow”<<endl; obj1.show(); obj1.display(); cout<<“Obj2 is as follows”<<endl; obj2.show(); obj2.display(); }
  • 18. Accessing Member Functions of Parent Class • The objects of derived class can access all member functions of parent class that are declared as protected or public.
  • 19. Write a class Person that has the attributes of id, name and address. It has a constructor to initialize, a member function to input and a member function to display data members. Create another class Student that inherits Person class. It has additional attributes of roll number and marks. It also has member function to input and display its data members.
  • 20. class Person { protected: int id; string name, address; public: Person() { id=0; name = “Name”; address =“address” } void getInfo() { cout<<“Enter your id”; cin>>id; cout<<“Enter your name”; cin>>name; cout<<“Enter your address”; cin>>address; } void showInfo() { cout<<“your personal information” cout<<“ id = “<<id<<endl; cout<<“Name = “<<name<<endl; cout<<“Address = “<<address<<endl; } }; class Student : public Person { private: int rno, marks; public: Student() { Person:: Person(); rno=marks=0; }
  • 21. void getEdu() { cout<<“Enter your roll no”; cin>>rno; cout<<“Enter your marks”; cin>>marks; void showEdu() { cout<<“Your education information is as follows”; cout<<“Roll no = “<<rno<<endl; cout<<“Marks = “<<marks<<endl; } }; main() { Student s; s.getInfo(): s.getEdu(); s.showEdu(); }
  • 22. Function Overriding • The process of declaring member function in derived class with same name and same signature as in parent class is known as function overriding. • Function overriding allows the user to use same names for calling the member functions of different class. • When a member function is overridden in the derived class, the object of derived class cannot access the function of parent class. • However, the function of parent class can be accessed by using scope resolution operator.
  • 23. class Parent { protected: int n; public: Parent(int p) { n=p; } void show() { cout<<“n = “<<n<<endl; } }; class Child : public Parent { private: char ch; Public: Child(char c, int m) : Parent(m) { ch=c; } void show() { Parent::show(); cout<<“ch = “<<ch<<endl; } }; main() { Child obj(‘$’, 100); obj.show(); }
  • 24. Types of Inheritance • A parent class can be inherited using public, protected or private type of inheritance. • The type of inheritance defines the access status of parent class members in the derived class. • Different types of inheritance are as follows: – Public Inheritance – Protected Inheritance – Private Inheritance
  • 25. Public Inheritance • In public inheritance, the access status of parent class members in the derived class remains the same. • The public members of parent class become public members of derived class. • The protected members of parent class become protected members of derived class. • The private members of parent class become private members of derived class. • The syntax of defining public inheritance is as follows: class child_class : public parent_class { body of the class }
  • 26. The accessibility of derived class in public inheritance is as follows: • The derived class can access the public members of parent class. • The derived class can access the protected members of parent class. • The derived class cannot access the private members of parent class. The accessibility of an object of derived class is as follows: • The object of derived class can access the public members of parent class. • The object of derived class cannot access the protected members of parent class. • The object of derived class cannot access the private members of parent class.
  • 27. Write a program that declares two classes and defines a relationship between them using public members.
  • 28. class parent { public: int a; protected: int b; private: int c; }; class child : public parent { public: void in() { cout<< “Enter a “; cin>>a; cout<<“Enter b “; cin>>b; } void out() { cout<<“a = “<<a<<endl; cout<<“b = “<<b<<endl; } }; main() { child obj; obj.in(); obj.out(); }
  • 29. Protected Inheritance • In Protected inheritance, the access status of parent class members in the derived class is restricted. • The public members of parent class become protected members of derived class. • The protected members of parent class become protected members of derived class. • The private members of parent class become private members of derived class.
  • 30. • The syntax of defining protected inheritance is as follows: class child_class : protected parent_class { body of the class }
  • 31. The accessibility of derived class in protected inheritance is as follows: • The derived class can access the public members of parent class. • The derived class can access the protected members of parent class. • The derived class cannot access the private members of parent class. The accessibility of an object of derived class is as follows: • The object of derived class cannot access the public members of parent class. • The object of derived class cannot access the protected members of parent class. • The object of derived class cannot access the private members of parent class.
  • 32. class parent { public: int a; protected: int b; private: int c; }; class child : protected parent { public: void in() { cout<< “Enter a “; cin>>a; cout<<“Enter b “; cin>>b; } void out() { cout<<“a = “<<a<<endl; cout<<“b = “<<b<<endl; } }; main() { child obj; obj.in(); obj.out(); }
  • 33. Private Inheritance • In private inheritance, the access status of parent class members in the derived class is restricted. • The private, protected and public members of parent class all become the private members of derived class. • The syntax of defining private inheritance is as follows: class child_class : private parent_class { body of the class }
  • 34. The accessibility of derived class in private inheritance is as follows: • The derived class can access the public members of parent class. • The derived class can access the protected members of parent class. • The derived class cannot access the private members of parent class. The accessibility of an object of derived class is as follows: • The object of derived class cannot access the public members of parent class. • The object of derived class cannot access the protected members of parent class. • The object of derived class cannot access the private members of parent class.
  • 35. Multilevel Inheritance • A type of inheritance in which a class is derived from another derived class is called multilevel inheritance. • In multilevel inheritance, the members of parent class are inherited to the child class and the members of child class are inherited to the grand child class. • In this way, the members of parent class and child class are combined in grand child class.
  • 36. • Example: class A { body of the class }; class B : public A { body of the class }; class C : public B { body of the class };
  • 37. class A { private: int a; public: void in() { cout<< “Enter a “; cin>>a; } void out() { cout<< “The value of a is “<<a<<endl; } }; class B : public A { private: int b; public: void in() { A::in(); cout<<“Enter b “; cin>>b; } void out() { A::out(); cout<<“The value of b is “<<b <<endl; } }; class C : public B { private : int c : public : void in(); { B::in(); cout<<“Enter c “; cin>>c; } void out() { B::out(); cout<<“The value of c is“<<c<<endl; } }; main() { C obj; obj.in(); obj.out(); }
  • 38. Multiple Inheritance • A type of inheritance in which a derived class inherit multiple base classes is known as multiple inheritance. • In multiple inheritance, the derived class combines the members of all base classes. • Syntax: class child_class : specifier Parent_class1, specifier parent class2…………….. { body of the class }
  • 39. • Example: class A { body of the class }; class B { body of the class }; class c : public A, public B { body of the class };
  • 40. class A { private: int a; public: void in() { cout<<“Enter a “; cin>>a; } void out() { cout<<“a = “a<<endl; } }; class B { private: int b; public: void input() { cout<<“Enter b “; cin>>b; } void output() { cout<<“b = “<<b<<endl; } }; class C : public A, public B { private: int c; public: void get() { A::in(); b::input(); cout<<“Enter c “; cin>>c; } void show() { A::out(); B::output(); cout<<“c = “<<c<<endl; } }; C obj; obj.get(); obj.show(); }