SlideShare a Scribd company logo
1 of 28
INTRODUCTION
 Inheritance is the most important feature of OOP. It is
a form of a program re-usability in which new classes
are made by using code of existing classes.
Program/Software re-usability saves times of
programmer in program development.
 The word “inherit” means “to receive”.
Definition
 Inheritance is the process of creating new classes,
called “derived class” from existing classes called “base
classes”.
 The base class is also known as super class or parent
class while derived class is also known as sub class or
child class.
LOC of base class
LOC of derived
class
Figure Explained…
 The above figure shows the relationship between a
derived class and the base class. The arrow is drawn
from the derived class to the base class. The direction
of the arrow indicates that the derived class can access
members of the base class but base class cannot access
members of its derived class.
 The derived class inherits all the capabilities of the
base class. It means that it can use the data members
and member function of its base class. It can also have
its own data members and member functions.
Cont…
 So the derived class can be larger than its base class.
 Each derived class it self becomes a candidate to be a
base class for some future derived classes.
Categories Of Inheritance
 A new class can be derived from one or more existing
classes. Therefore inheritance is further divided into
different categories given below:
1) Single Inheritance
2) Multiple Inheritance
3) Multilevel Inheritance
Single Inheritance
 With single inheritance, a new class is derived from
one base class. It is very simple and straight forward
process.
 For example: A new class B is derived from base class
A. The derived class inherits all the attributes(data
members) and operations (member function) of the
base class.
Member 1
Member 2
Member 1
Member 2
Member 3
Member 4
Class A
Class B
Explanation
 In figure, the derived class has two members of its own
and the members shown with dark shadow represents
the members of the base class. The derived class can
also access these two members of the base class. Thus
an object of the base class can access only two
members, while an object of the derived class can
access four members (two members of the base class
and tow of the derived class)
Defining Derived Class with Single
Inheritance
 A derived class is defined in the similar way as base
class is defined. The syntax for defining a derived class
is slightly different from syntax of base class definition.
 The general syntax for defining a derived class is:
class derived_class_name : access_specifier base_class
{
Body of derived class
}
Example:
#include<iostream>
using namespace std;
class std_address //base class
{
private:
int code;
string name, phone_no;
public:
void input();
void display();
};
// derived class
class std_result : public std_address
{
private:
float sub1, sub2,sub3, average ,
total;
public:
void input_marks();
void display_marks();
};
Cont…
//Defining Member Functions of Base
Class
void std_address :: input()
{
cout<<“Enter code of student”<<endl;
cin>>code;
cout<<“Enter name of student”<<endl;
cin>>name;
cout<<“Enter phone of student”<<endl;
cin>>phone_no;
}
void std_address :: display()
{
cout<<“Code : “<<code<<endl;
cout<<“Name : “<<name<<endl;
cout<<“Phone : “<<phone_no<<endl;
}
//Defining Member Functions of Derived Class
void std_result :: input_marks();
{
cout<<“Enter marks of first subject?”<<endl;
cin>>sub1;
cout<<“Enter marks of second subject?”<<endl;
cin>>sub2;
cout<<“Enter marks of third subject?”<<endl;
cin>>sub3;
total = sub1+sub2+sub3;
Average = total/3;
}
void std_result :: display_marks()
{
cout<<“Marks of first subject: “<<sub1;
cout<<“Marks of second subject:“<<sub2;
cout<<“Marks of third subject: “<<sub3;
cout<<“Total Marks :“<<total;
cout<<“Average marks : “<<average;
}
//main function
int main()
{
std_result res;
res.input();
res.input_marks();
res.display();
res.display_marks();
}
Multiple Inheritance
 A new class can be derived from more than one base class
(or multiple base classes).
 This type of inheritance is called multiple inheritance.
 In multiple inheritance, the derived class inherits members
of all base classes. It can also have its own data members.
 Multiple inheritance reduces the program size and saves
program development time in single inheritance.
 Multiple inheritance is complex than single inheritance.
Some object oriented programming language do not
support this feature but C++ support this feature.
Diagrammatic Representation
Class A
Class C
Class B
Example
#include<iostream>
using namespace std;
class std_address
{
private:
string name, city;
public:
void input();
void print();
};
class std_marks
{
private:
float sub1, sub2, sub3, average, total;
public:
void input_marks()
void print_marks()
};
class std_result : public std_address, public std_marks
{
public:
void display()
{
cout<<“COMPLETE RECORD”<<endl;
print();
print_marks();
}
};
int main()
{
std_result res;
res.input();
res.input_marks();
res.display();
}
void std_address:: input()
{
cout<<“Enter student name”<<endl;
cin>>name;
cout<<“Enter name of city”<<endl;
cin>>city;
void std_address :: print()
{
cout<<“Name: “<<name<<endl;
cout<<“City: “<<city<<endl;
}
void std_marks :: input_marks()
{
cout<<“Enter marks of first subject”<<endl;
cin>>sub1;
cout<<“Enter marks of second subject”<<endl;
cin>>sub2;
cout<<“Enter marks of third subject”<<endl;
cin>>sub3;
total = sub1+sub2+sub3;
average = total/3;
}
void std_marks :: display()
{
cout<<“Marks of first subject: “<<sub1<<endl;
cout<<“Marks of second subject: “<<sub2<<endl;
cout<<“Marks of third subject: “<<sub3<<endl;
cout<<“Total marks: “<<total<<endl;
cout<<“Average marks:” <<average<<endl;
}
Multilevel Inheritance
 A class can be derived from another derived class. The
type of inheritance is called multilevel inheritance. For
example if a class “C” is derived from base class “B” and
a new class “D” is derived from derived class C. Then
the derived class D will inherit all the capabilities of C
and B.
Syntax
class A
{
body of class A;
};
class B : public A
{
body of derived class B;
};
class C : public B
{
body of derived class C;
};
Example
#include<iostream>
class values
{
private:
float m, n;
public:
float assign(float x, float y)
{
m = x;
n = y;
}
};
class sum : public values
{
public:
float sum()
{
cout<<“sum of”<<m<<“and”<<n<<“=“<<m+n;
}
};
class product : public sum
{
float pro()
{
cout<<“product of”<<m<<“and”<<n<<“=“<<m*n;
}
};
main()
{
product x;
x.assign(24,45);
x.sum();
x.pro();
}
Types of inheritance
The accessibility of members of the base class can be
controlled by using private, public and protected
Specifier. Thus, the inheritance can be divide on the
basis of accessing members of the base class.
Therefore, types of inheritance are:
 Private Inheritance
 Public Inheritance
 Protected Inheritance
Private Inheritance
 With private inheritance public and protected
members of the base class become the private
members of the derived class. The public and
protected members of the base class can only be
accessed inside the derived class but private members
of base class cannot be accessed inside the derived
class.
Public Inheritance
 With public inheritance public members of the base
class become the public members of derived class.
Similarly, protected members of the base class
becomes the protected members of the derived class.
Protected Inheritance
 The protected inheritance is similar to private
inheritance. It is rarely used. With protected
inheritance, public and protected members of the base
class become the protected member of derived class.
The object of derived class can access only protected
members of base class.

More Related Content

What's hot

Inheritance
InheritanceInheritance
InheritanceTech_MX
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Ameen Sha'arawi
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismJawad Khan
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++Nikunj Patel
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritanceharshaltambe
 

What's hot (20)

inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Inheritance
InheritanceInheritance
Inheritance
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 
C++ oop
C++ oopC++ oop
C++ oop
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 

Similar to Inheritance

chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple InheritanceBhavyaJain137
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdfWaqarRaj1
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxDeepasCSE
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Shweta Shah
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfitxminahil29
 

Similar to Inheritance (20)

Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Inheritance
InheritanceInheritance
Inheritance
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Inheritance
InheritanceInheritance
Inheritance
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdf
 

More from Burhan Ahmed

Wireless mobile communication
Wireless mobile communicationWireless mobile communication
Wireless mobile communicationBurhan Ahmed
 
Uses misuses and risk of software
Uses misuses and risk of softwareUses misuses and risk of software
Uses misuses and risk of softwareBurhan Ahmed
 
The distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
The distinction of prophet muhammad (s.a.w) among the teachers of moral conductThe distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
The distinction of prophet muhammad (s.a.w) among the teachers of moral conductBurhan Ahmed
 
Software house organization
Software house organizationSoftware house organization
Software house organizationBurhan Ahmed
 
Social interaction
Social interactionSocial interaction
Social interactionBurhan Ahmed
 
Planning work activities
Planning work activitiesPlanning work activities
Planning work activitiesBurhan Ahmed
 
Peripheral devices
Peripheral devicesPeripheral devices
Peripheral devicesBurhan Ahmed
 
Parallel computing and its applications
Parallel computing and its applicationsParallel computing and its applications
Parallel computing and its applicationsBurhan Ahmed
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingBurhan Ahmed
 
Job analysis and job design
Job analysis and job designJob analysis and job design
Job analysis and job designBurhan Ahmed
 
Intellectual property
Intellectual propertyIntellectual property
Intellectual propertyBurhan Ahmed
 

More from Burhan Ahmed (20)

Wireless mobile communication
Wireless mobile communicationWireless mobile communication
Wireless mobile communication
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Uses misuses and risk of software
Uses misuses and risk of softwareUses misuses and risk of software
Uses misuses and risk of software
 
Types of computer
Types of computerTypes of computer
Types of computer
 
Trees
TreesTrees
Trees
 
Topology
TopologyTopology
Topology
 
The distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
The distinction of prophet muhammad (s.a.w) among the teachers of moral conductThe distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
The distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
 
Software house organization
Software house organizationSoftware house organization
Software house organization
 
Social interaction
Social interactionSocial interaction
Social interaction
 
Role model
Role modelRole model
Role model
 
Rights and duties
Rights and dutiesRights and duties
Rights and duties
 
Planning work activities
Planning work activitiesPlanning work activities
Planning work activities
 
Peripheral devices
Peripheral devicesPeripheral devices
Peripheral devices
 
Parallel computing and its applications
Parallel computing and its applicationsParallel computing and its applications
Parallel computing and its applications
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Normalization
NormalizationNormalization
Normalization
 
Managing strategy
Managing strategyManaging strategy
Managing strategy
 
Letter writing
Letter writingLetter writing
Letter writing
 
Job analysis and job design
Job analysis and job designJob analysis and job design
Job analysis and job design
 
Intellectual property
Intellectual propertyIntellectual property
Intellectual property
 

Recently uploaded

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 

Recently uploaded (20)

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 

Inheritance

  • 1.
  • 2. INTRODUCTION  Inheritance is the most important feature of OOP. It is a form of a program re-usability in which new classes are made by using code of existing classes. Program/Software re-usability saves times of programmer in program development.  The word “inherit” means “to receive”.
  • 3. Definition  Inheritance is the process of creating new classes, called “derived class” from existing classes called “base classes”.  The base class is also known as super class or parent class while derived class is also known as sub class or child class.
  • 4. LOC of base class LOC of derived class
  • 5. Figure Explained…  The above figure shows the relationship between a derived class and the base class. The arrow is drawn from the derived class to the base class. The direction of the arrow indicates that the derived class can access members of the base class but base class cannot access members of its derived class.  The derived class inherits all the capabilities of the base class. It means that it can use the data members and member function of its base class. It can also have its own data members and member functions.
  • 6. Cont…  So the derived class can be larger than its base class.  Each derived class it self becomes a candidate to be a base class for some future derived classes.
  • 7. Categories Of Inheritance  A new class can be derived from one or more existing classes. Therefore inheritance is further divided into different categories given below: 1) Single Inheritance 2) Multiple Inheritance 3) Multilevel Inheritance
  • 8. Single Inheritance  With single inheritance, a new class is derived from one base class. It is very simple and straight forward process.  For example: A new class B is derived from base class A. The derived class inherits all the attributes(data members) and operations (member function) of the base class.
  • 9. Member 1 Member 2 Member 1 Member 2 Member 3 Member 4 Class A Class B
  • 10. Explanation  In figure, the derived class has two members of its own and the members shown with dark shadow represents the members of the base class. The derived class can also access these two members of the base class. Thus an object of the base class can access only two members, while an object of the derived class can access four members (two members of the base class and tow of the derived class)
  • 11. Defining Derived Class with Single Inheritance  A derived class is defined in the similar way as base class is defined. The syntax for defining a derived class is slightly different from syntax of base class definition.  The general syntax for defining a derived class is: class derived_class_name : access_specifier base_class { Body of derived class }
  • 12. Example: #include<iostream> using namespace std; class std_address //base class { private: int code; string name, phone_no; public: void input(); void display(); }; // derived class class std_result : public std_address { private: float sub1, sub2,sub3, average , total; public: void input_marks(); void display_marks(); };
  • 13. Cont… //Defining Member Functions of Base Class void std_address :: input() { cout<<“Enter code of student”<<endl; cin>>code; cout<<“Enter name of student”<<endl; cin>>name; cout<<“Enter phone of student”<<endl; cin>>phone_no; } void std_address :: display() { cout<<“Code : “<<code<<endl; cout<<“Name : “<<name<<endl; cout<<“Phone : “<<phone_no<<endl; } //Defining Member Functions of Derived Class void std_result :: input_marks(); { cout<<“Enter marks of first subject?”<<endl; cin>>sub1; cout<<“Enter marks of second subject?”<<endl; cin>>sub2; cout<<“Enter marks of third subject?”<<endl; cin>>sub3; total = sub1+sub2+sub3; Average = total/3; }
  • 14. void std_result :: display_marks() { cout<<“Marks of first subject: “<<sub1; cout<<“Marks of second subject:“<<sub2; cout<<“Marks of third subject: “<<sub3; cout<<“Total Marks :“<<total; cout<<“Average marks : “<<average; } //main function int main() { std_result res; res.input(); res.input_marks(); res.display(); res.display_marks(); }
  • 15. Multiple Inheritance  A new class can be derived from more than one base class (or multiple base classes).  This type of inheritance is called multiple inheritance.  In multiple inheritance, the derived class inherits members of all base classes. It can also have its own data members.  Multiple inheritance reduces the program size and saves program development time in single inheritance.  Multiple inheritance is complex than single inheritance. Some object oriented programming language do not support this feature but C++ support this feature.
  • 17. Example #include<iostream> using namespace std; class std_address { private: string name, city; public: void input(); void print(); }; class std_marks { private: float sub1, sub2, sub3, average, total; public: void input_marks() void print_marks() }; class std_result : public std_address, public std_marks { public: void display() { cout<<“COMPLETE RECORD”<<endl; print(); print_marks(); } };
  • 18. int main() { std_result res; res.input(); res.input_marks(); res.display(); } void std_address:: input() { cout<<“Enter student name”<<endl; cin>>name; cout<<“Enter name of city”<<endl; cin>>city; void std_address :: print() { cout<<“Name: “<<name<<endl; cout<<“City: “<<city<<endl; } void std_marks :: input_marks() { cout<<“Enter marks of first subject”<<endl; cin>>sub1; cout<<“Enter marks of second subject”<<endl; cin>>sub2; cout<<“Enter marks of third subject”<<endl; cin>>sub3; total = sub1+sub2+sub3;
  • 19. average = total/3; } void std_marks :: display() { cout<<“Marks of first subject: “<<sub1<<endl; cout<<“Marks of second subject: “<<sub2<<endl; cout<<“Marks of third subject: “<<sub3<<endl; cout<<“Total marks: “<<total<<endl; cout<<“Average marks:” <<average<<endl; }
  • 20. Multilevel Inheritance  A class can be derived from another derived class. The type of inheritance is called multilevel inheritance. For example if a class “C” is derived from base class “B” and a new class “D” is derived from derived class C. Then the derived class D will inherit all the capabilities of C and B.
  • 21. Syntax class A { body of class A; }; class B : public A { body of derived class B; }; class C : public B { body of derived class C; };
  • 22. Example #include<iostream> class values { private: float m, n; public: float assign(float x, float y) { m = x; n = y; } }; class sum : public values { public: float sum() { cout<<“sum of”<<m<<“and”<<n<<“=“<<m+n; } }; class product : public sum { float pro() { cout<<“product of”<<m<<“and”<<n<<“=“<<m*n; } };
  • 24.
  • 25. Types of inheritance The accessibility of members of the base class can be controlled by using private, public and protected Specifier. Thus, the inheritance can be divide on the basis of accessing members of the base class. Therefore, types of inheritance are:  Private Inheritance  Public Inheritance  Protected Inheritance
  • 26. Private Inheritance  With private inheritance public and protected members of the base class become the private members of the derived class. The public and protected members of the base class can only be accessed inside the derived class but private members of base class cannot be accessed inside the derived class.
  • 27. Public Inheritance  With public inheritance public members of the base class become the public members of derived class. Similarly, protected members of the base class becomes the protected members of the derived class.
  • 28. Protected Inheritance  The protected inheritance is similar to private inheritance. It is rarely used. With protected inheritance, public and protected members of the base class become the protected member of derived class. The object of derived class can access only protected members of base class.