SlideShare a Scribd company logo
SARDAR PATEL COLLEGE OF ENGINEERING
Subject : Object Oriented Programming With C++
Topic : INHERITANCE
Branch : IT 4th sem
Prepaid By : Nikunj.M.Patel
INHERITANCE
Inheritance is the process by which new classes called derived classes are
created from existing classes called base classes.
The derived classes have all the features of the base class and the
programmer can choose to add new features specific to the newly created
derived class.
The idea of inheritance implements the is a relationship. For example,
mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and
so on.
WHAT IS AN INHERTANCE?
Reusability of Code
Saves Time and Effort
Faster development, easier maintenance and easy to extend.
Capable of expressing the inheritance relationship and its
transitive nature which ensures closeness with real world
problems .
FEATURES /ADVANTAGES OF INHERITANCE
To create a derived class from an already existing base class the syntax is:
class derived-class: access-specifier base-class
{
……….
……….
}
Where access specifier is one of public, protected, or private.
SYNTAX
For example, if the base class is animals and the
derived class is amphibians it is specified as:
class animals //base class
{
…….
};
class amphibians : public animals
{ //derived class
…..
};
SYNTAX contd……
In this example class amphibians have
access to both public and protected
members of base class animals.
NOTE: A class can be derived from
more than one class, which means it can
inherit data and functions from multiple
base classes. In that case a class
derivation lists names of one or more
base classes each separated by comma.
VISIBILTY MODES AND INHERITANCE
A child class can inherit base class in three ways. These are:
PRIVATE PROTECTED PUBLIC
PRIVATE NOT
INHERITED
Become private members of
child class
Become private members
of child class
PROTECTED NOT
INHERITED
Become protected members
of child class
Become protected members
of child class
PUBLIC NOT
INHERITED
Become protected members
of child class
Become public members of
child class
PRIVATE INHERITANCE
class child : private base
{
private:
int x;
void funcx();
protected:
int y;
void funcy();
public:
int z;
void funcz();
}
class child
{
private:
int x;
void funcx();
int b;
void funcb();
int c;
void funcc();
protected:
int y;
void funcy();
public:
int z;
void funcz();
}
In private inheritance protected and public members of the base class become the
private members of the derived class.
class base
{
private:
int a;
void funca();
protected:
int b;
void funcb();
public:
int c;
void funcc();
}
Private
inheritance
New child class after inheritance
Protected members
inherited from base class
Public members inherited
from base class
PROTECTED INHERITANCE
class child : protected base
{
private:
int x;
void funcx();
protected:
int y;
void funcy();
public:
int z;
void funcz();
}
class child
{
private:
int x;
void funcx();
protected:
int y;
void funcy();
int b;
void funcb();
int c;
void funcc();
public:
int z;
void funcz();
}
In protected inheritance protected and public members of the base class become the
protected members of the derived class.
class base
{
private:
int a;
void funca();
protected:
int b;
void funcb();
public:
int c;
void funcc();
}
Protected
inheritance
New child class after
inheritance
Protected members
inherited from base class
Public members
inherited from base
class
PUBLIC INHERITANCE
class child : public base
{
private:
int x;
void funcx();
protected:
int y;
void funcy();
public:
int z;
void funcz();
}
class child
{
private:
int x;
void funcx();
protected:
int y;
void funcy();
int b;
void funcb();
public:
int z;
void funcz();
int c;
void funcc();
}
In protected inheritance protected members become the protected members of the base class and public
members of the base class become the public members of the derived class.
class base
{
private:
int a;
void funca();
protected:
int b;
void funcb();
public:
int c;
void funcc();
}
Public
inheritance
New child class after
inheritance
Protected members
inherited from base
class
Public members
inherited from base
class
TYPES OF INHERITANCE
There are five different types of inheritance:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
SINGLE INHERITENCE
Single inheritance is the one where you have a single base
class and a single derived class.
EXAMPLE
class student
{
private:
char name[20];
float marks;
protected:
void result();
public:
student();
void enroll();
void display();
}
class course : public student
{
long course_code;
char course_name;
public:
course();
void commence();
void cdetail();
}
STUDENT
COURSE
MULTILEVEL INHERITENCE
In Multi level inheritance, a subclass inherits from a class
that itself inherits from another class.
EXAMPLE
class furniture
{
char type;
char model[10];
public:
furniture();
void readdata();
void dispdata();
}
class sofa: public furniture
{
int no_of_seats;
float cost;
public:
void indata();
void outdata();
};
class office: private sofa
{
int no_of_pieces;
char delivery_date[10];
public:
void readdetails()
void displaydetails();
}
FURNITURE
OFFICE
SOFA
MULTIPLE INHERITENCE
In Multiple inheritances, a derived class inherits from multiple
base classes. It has properties of both the base classes.
MULTIPLE INHERITENCE
EXAMPLE
class chaiperson
{
long chairid;
char name[20];
protected:
char description[20];
void allocate();
public:
chairperson();
void assign();
void show();
};
class director
{
long directorid;
char dname[20];
public:
director();
void entry();
void display();
};
class company: private
chairperson, public director
{
int companyid;
char city[20];
char country[20];
public:
void ctentry();
void ctdisplay();
};
COMPANY
CHAIRPERSON DIRECTOR
HIERARCHICAL INHERITENCE
In hierarchical Inheritance, it's like an inverted tree. So multiple
classes inherit from a single base class.
HIERARCHICAL INHERITENCE
EXAMPLE
class toys
{
char tcode[5];
protected:
float price;
void assign(float);
public:
toys();
void tentry();
void tdisplay();
};
class softtoys: public toys
{
chat stname[20];
float weight;
public:
softtoys();
void stentry();
void stdisplay();
};
class electronictoys: public
toys
{
char etname[20];
int no_of_batteries;
public:
void etentry();
void etdisplay();
};
TOYS
ELECTRONIC
TOYS
SOFT
TOYS
HYBRID INHERITENCE
It combines two or more types of inheritance. In this type of
inheritance we can have a mixture of number of inheritances.

More Related Content

What's hot

array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
Arpita Patel
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
Hashim Hashim
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Lovely Professional University
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
MananPasricha
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Harsh Patel
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
rajveer_Pannu
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
Raja Sekhar
 
This keyword and final keyword
This keyword and final  keywordThis keyword and final  keyword
This keyword and final keyword
kinjalbirare
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
NainaKhan28
 
Java interface
Java interfaceJava interface
Java interface
BHUVIJAYAVELU
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
RAGAVIC2
 

What's hot (20)

array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
This keyword and final keyword
This keyword and final  keywordThis keyword and final  keyword
This keyword and final keyword
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Java interface
Java interfaceJava interface
Java interface
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
 

Similar to EASY TO LEARN INHERITANCE IN C++

inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
urvashipundir04
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
deepakskb2013
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
inheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdfinheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdf
kashafishfaq21
 
Inheritance
InheritanceInheritance
inheritance
inheritanceinheritance
inheritance
Amir_Mukhtar
 
Inheritance
InheritanceInheritance
Inheritance
SangeethaSasi1
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).ppt
amal68766
 
Inheritance
InheritanceInheritance
Inheritance
InheritanceInheritance
Inheritance
Aadhi Aadhithya
 
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
itxminahil29
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
study material
 
OOP.ppt
OOP.pptOOP.ppt
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
Redwan Islam
 
Inheritance
InheritanceInheritance
Inheritance
prashant prath
 
OOP C++
OOP C++OOP C++
OOP C++
Ahmed Farag
 

Similar to EASY TO LEARN INHERITANCE IN C++ (20)

inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdfinheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
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
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
OOP.ppt
OOP.pptOOP.ppt
OOP.ppt
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
Inheritance
InheritanceInheritance
Inheritance
 
OOP C++
OOP C++OOP C++
OOP C++
 

Recently uploaded

Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 

Recently uploaded (20)

Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 

EASY TO LEARN INHERITANCE IN C++

  • 1. SARDAR PATEL COLLEGE OF ENGINEERING Subject : Object Oriented Programming With C++ Topic : INHERITANCE Branch : IT 4th sem Prepaid By : Nikunj.M.Patel
  • 3. Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class. The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on. WHAT IS AN INHERTANCE?
  • 4. Reusability of Code Saves Time and Effort Faster development, easier maintenance and easy to extend. Capable of expressing the inheritance relationship and its transitive nature which ensures closeness with real world problems . FEATURES /ADVANTAGES OF INHERITANCE
  • 5. To create a derived class from an already existing base class the syntax is: class derived-class: access-specifier base-class { ………. ………. } Where access specifier is one of public, protected, or private. SYNTAX
  • 6. For example, if the base class is animals and the derived class is amphibians it is specified as: class animals //base class { ……. }; class amphibians : public animals { //derived class ….. }; SYNTAX contd…… In this example class amphibians have access to both public and protected members of base class animals. NOTE: A class can be derived from more than one class, which means it can inherit data and functions from multiple base classes. In that case a class derivation lists names of one or more base classes each separated by comma.
  • 7. VISIBILTY MODES AND INHERITANCE A child class can inherit base class in three ways. These are: PRIVATE PROTECTED PUBLIC PRIVATE NOT INHERITED Become private members of child class Become private members of child class PROTECTED NOT INHERITED Become protected members of child class Become protected members of child class PUBLIC NOT INHERITED Become protected members of child class Become public members of child class
  • 8. PRIVATE INHERITANCE class child : private base { private: int x; void funcx(); protected: int y; void funcy(); public: int z; void funcz(); } class child { private: int x; void funcx(); int b; void funcb(); int c; void funcc(); protected: int y; void funcy(); public: int z; void funcz(); } In private inheritance protected and public members of the base class become the private members of the derived class. class base { private: int a; void funca(); protected: int b; void funcb(); public: int c; void funcc(); } Private inheritance New child class after inheritance Protected members inherited from base class Public members inherited from base class
  • 9. PROTECTED INHERITANCE class child : protected base { private: int x; void funcx(); protected: int y; void funcy(); public: int z; void funcz(); } class child { private: int x; void funcx(); protected: int y; void funcy(); int b; void funcb(); int c; void funcc(); public: int z; void funcz(); } In protected inheritance protected and public members of the base class become the protected members of the derived class. class base { private: int a; void funca(); protected: int b; void funcb(); public: int c; void funcc(); } Protected inheritance New child class after inheritance Protected members inherited from base class Public members inherited from base class
  • 10. PUBLIC INHERITANCE class child : public base { private: int x; void funcx(); protected: int y; void funcy(); public: int z; void funcz(); } class child { private: int x; void funcx(); protected: int y; void funcy(); int b; void funcb(); public: int z; void funcz(); int c; void funcc(); } In protected inheritance protected members become the protected members of the base class and public members of the base class become the public members of the derived class. class base { private: int a; void funca(); protected: int b; void funcb(); public: int c; void funcc(); } Public inheritance New child class after inheritance Protected members inherited from base class Public members inherited from base class
  • 11. TYPES OF INHERITANCE There are five different types of inheritance: 1. Single Inheritance 2. Multiple Inheritance 3. Multilevel Inheritance 4. Hierarchical Inheritance 5. Hybrid Inheritance
  • 12. SINGLE INHERITENCE Single inheritance is the one where you have a single base class and a single derived class.
  • 13. EXAMPLE class student { private: char name[20]; float marks; protected: void result(); public: student(); void enroll(); void display(); } class course : public student { long course_code; char course_name; public: course(); void commence(); void cdetail(); } STUDENT COURSE
  • 14. MULTILEVEL INHERITENCE In Multi level inheritance, a subclass inherits from a class that itself inherits from another class.
  • 15. EXAMPLE class furniture { char type; char model[10]; public: furniture(); void readdata(); void dispdata(); } class sofa: public furniture { int no_of_seats; float cost; public: void indata(); void outdata(); }; class office: private sofa { int no_of_pieces; char delivery_date[10]; public: void readdetails() void displaydetails(); } FURNITURE OFFICE SOFA
  • 16. MULTIPLE INHERITENCE In Multiple inheritances, a derived class inherits from multiple base classes. It has properties of both the base classes.
  • 17. MULTIPLE INHERITENCE EXAMPLE class chaiperson { long chairid; char name[20]; protected: char description[20]; void allocate(); public: chairperson(); void assign(); void show(); }; class director { long directorid; char dname[20]; public: director(); void entry(); void display(); }; class company: private chairperson, public director { int companyid; char city[20]; char country[20]; public: void ctentry(); void ctdisplay(); }; COMPANY CHAIRPERSON DIRECTOR
  • 18. HIERARCHICAL INHERITENCE In hierarchical Inheritance, it's like an inverted tree. So multiple classes inherit from a single base class.
  • 19. HIERARCHICAL INHERITENCE EXAMPLE class toys { char tcode[5]; protected: float price; void assign(float); public: toys(); void tentry(); void tdisplay(); }; class softtoys: public toys { chat stname[20]; float weight; public: softtoys(); void stentry(); void stdisplay(); }; class electronictoys: public toys { char etname[20]; int no_of_batteries; public: void etentry(); void etdisplay(); }; TOYS ELECTRONIC TOYS SOFT TOYS
  • 20. HYBRID INHERITENCE It combines two or more types of inheritance. In this type of inheritance we can have a mixture of number of inheritances.