SlideShare a Scribd company logo
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

PPTX
Java program structure
Mukund Kumar Bharti
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPT
Java And Multithreading
Shraddha
 
PPT
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
PPTX
Data Types, Variables, and Operators
Marwa Ali Eissa
 
PDF
Javapolymorphism
karthikenlume
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PPTX
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Java program structure
Mukund Kumar Bharti
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Java And Multithreading
Shraddha
 
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
Data Types, Variables, and Operators
Marwa Ali Eissa
 
Javapolymorphism
karthikenlume
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 

What's hot (20)

PDF
Introduction to Input/Output Functions in C
Thesis Scientist Private Limited
 
PPTX
Ch.1 oop introduction, classes and objects
ITNet
 
PPTX
Java program structure
shalinikarunakaran1
 
PPTX
this keyword in Java.pptx
ParvizMirzayev2
 
PPT
Basic of Multithreading in JAva
suraj pandey
 
PDF
Learn C# Programming - Variables & Constants
Eng Teong Cheah
 
PPT
Java inheritance
Arati Gadgil
 
PPTX
Multiple inheritance in java3 (1).pptx
RkGupta83
 
PPTX
Packages in java
Elizabeth alexander
 
PPT
Inheritance in java
Lovely Professional University
 
PPTX
Inheritance
Siddhesh Palkar
 
PPT
Reflection in java
upen.rockin
 
PPTX
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
PDF
Polymorphism in Java by Animesh Sarkar
Animesh Sarkar
 
PPT
Data types
myrajendra
 
PPT
Object Oriented Programming with Java
backdoor
 
PPTX
C programming language tutorial
javaTpoint s
 
PPT
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
PDF
Function in C
Dr. Abhineet Anand
 
PDF
ITFT-Constants, variables and data types in java
Atul Sehdev
 
Introduction to Input/Output Functions in C
Thesis Scientist Private Limited
 
Ch.1 oop introduction, classes and objects
ITNet
 
Java program structure
shalinikarunakaran1
 
this keyword in Java.pptx
ParvizMirzayev2
 
Basic of Multithreading in JAva
suraj pandey
 
Learn C# Programming - Variables & Constants
Eng Teong Cheah
 
Java inheritance
Arati Gadgil
 
Multiple inheritance in java3 (1).pptx
RkGupta83
 
Packages in java
Elizabeth alexander
 
Inheritance in java
Lovely Professional University
 
Inheritance
Siddhesh Palkar
 
Reflection in java
upen.rockin
 
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
Polymorphism in Java by Animesh Sarkar
Animesh Sarkar
 
Data types
myrajendra
 
Object Oriented Programming with Java
backdoor
 
C programming language tutorial
javaTpoint s
 
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
Function in C
Dr. Abhineet Anand
 
ITFT-Constants, variables and data types in java
Atul Sehdev
 
Ad

Viewers also liked (9)

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

Similar to Inheritance (20)

PPTX
Lecture 5.mte 407
rumanatasnim415
 
PPT
Inheritance in C++
Shweta Shah
 
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
ssuser6f3c8a
 
PPTX
Inheritance
Burhan Ahmed
 
PPT
inheritance
Amir_Mukhtar
 
PPT
session 24_Inheritance.ppt
NAVANEETCHATURVEDI2
 
PDF
2 BytesC++ course_2014_c11_ inheritance
kinan keshkeh
 
PPT
11 Inheritance.ppt
LadallaRajKumar
 
PPT
Inheritance in C++
RAJ KUMAR
 
PDF
lecture 6.pdf
WaqarRaj1
 
PPTX
inheritance
krishna partiwala
 
PPT
Inheritance, Object Oriented Programming
Arslan Waseem
 
PPTX
Introduction to Inheritance
Keshav Vaswani
 
PPT
Lecturespecial
karan saini
 
PPT
OOP
karan saini
 
PPT
OOPs Lecture 2
Abbas Ajmal
 
PPTX
Inheritance
prashant prath
 
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
urvashipundir04
 
PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PDF
lecture-2021inheritance-160705095417.pdf
AneesAbbasi14
 
Lecture 5.mte 407
rumanatasnim415
 
Inheritance in C++
Shweta Shah
 
MODULE2_INHERITANCE_SESSION1.ppt computer
ssuser6f3c8a
 
Inheritance
Burhan Ahmed
 
inheritance
Amir_Mukhtar
 
session 24_Inheritance.ppt
NAVANEETCHATURVEDI2
 
2 BytesC++ course_2014_c11_ inheritance
kinan keshkeh
 
11 Inheritance.ppt
LadallaRajKumar
 
Inheritance in C++
RAJ KUMAR
 
lecture 6.pdf
WaqarRaj1
 
inheritance
krishna partiwala
 
Inheritance, Object Oriented Programming
Arslan Waseem
 
Introduction to Inheritance
Keshav Vaswani
 
Lecturespecial
karan saini
 
OOPs Lecture 2
Abbas Ajmal
 
Inheritance
prashant prath
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
urvashipundir04
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
lecture-2021inheritance-160705095417.pdf
AneesAbbasi14
 

Recently uploaded (20)

PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
Presentation about variables and constant.pptx
safalsingh810
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 

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