SlideShare une entreprise Scribd logo
1  sur  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(); 
}

Contenu connexe

Tendances

class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
file handling c++
file handling c++file handling c++
file handling c++
Guddu Spy
 

Tendances (20)

Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
OOP Assignment 03.pdf
OOP Assignment 03.pdfOOP Assignment 03.pdf
OOP Assignment 03.pdf
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Inheritance
InheritanceInheritance
Inheritance
 
class and objects
class and objectsclass and objects
class and objects
 
Inheritance
InheritanceInheritance
Inheritance
 
file handling c++
file handling c++file handling c++
file handling c++
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
C++ programming
C++ programmingC++ programming
C++ programming
 

En vedette

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

En vedette (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
 

Similaire à Inheritance

Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
 

Similaire à 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
 
29csharp
29csharp29csharp
29csharp
 
29c
29c29c
29c
 
27c
27c27c
27c
 

Dernier

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
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...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

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(); }