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

PDF
Inheritance and interface
Shubham Sharma
 
PPTX
Inheritance in c++
Paumil Patel
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPTX
Templates in c++
ThamizhselviKrishnam
 
PPTX
File handling
priya_trehan
 
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
PPTX
Inheritance in C++
Laxman Puri
 
PPTX
Inheritance in Object Oriented Programming
Ashita Agrawal
 
Inheritance and interface
Shubham Sharma
 
Inheritance in c++
Paumil Patel
 
Inheritance in c++
Vineeta Garg
 
Templates in c++
ThamizhselviKrishnam
 
File handling
priya_trehan
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
Inheritance in C++
Laxman Puri
 
Inheritance in Object Oriented Programming
Ashita Agrawal
 

What's hot (20)

PPTX
Polymorphism in java
Elizabeth alexander
 
PPT
Inheritance OOP Concept in C++.
MASQ Technologies
 
PPTX
Constructor in java
Hitesh Kumar
 
PPTX
Inheritance in c++
Vishal Patil
 
PPTX
Friend functions
Megha Singh
 
PPS
Inheritance
Ashish Awasthi
 
PPTX
virtual function
VENNILAV6
 
PPTX
Inheritance In Java
Manish Sahu
 
PDF
Classes and objects
Nilesh Dalvi
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPT
Java packages
Raja Sekhar
 
PPT
08 c++ Operator Overloading.ppt
Tareq Hasan
 
PDF
Structures in c++
Swarup Kumar Boro
 
PPTX
Friend function & friend class
Abhishek Wadhwa
 
PPTX
Multiple inheritance in c++
Sujan Mia
 
PDF
JavaScript and jQuery - Web Technologies (1019888BNR)
Beat Signer
 
PPTX
this keyword in Java.pptx
ParvizMirzayev2
 
PPTX
Presentation on-exception-handling
Nahian Ahmed
 
PPTX
Constructors in C++
RubaNagarajan
 
PPTX
Chapter 07 inheritance
Praveen M Jigajinni
 
Polymorphism in java
Elizabeth alexander
 
Inheritance OOP Concept in C++.
MASQ Technologies
 
Constructor in java
Hitesh Kumar
 
Inheritance in c++
Vishal Patil
 
Friend functions
Megha Singh
 
Inheritance
Ashish Awasthi
 
virtual function
VENNILAV6
 
Inheritance In Java
Manish Sahu
 
Classes and objects
Nilesh Dalvi
 
classes and objects in C++
HalaiHansaika
 
Java packages
Raja Sekhar
 
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Structures in c++
Swarup Kumar Boro
 
Friend function & friend class
Abhishek Wadhwa
 
Multiple inheritance in c++
Sujan Mia
 
JavaScript and jQuery - Web Technologies (1019888BNR)
Beat Signer
 
this keyword in Java.pptx
ParvizMirzayev2
 
Presentation on-exception-handling
Nahian Ahmed
 
Constructors in C++
RubaNagarajan
 
Chapter 07 inheritance
Praveen M Jigajinni
 
Ad

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

PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
urvashipundir04
 
PPTX
inheritance_OOPC_datastream.ppttttttttttttttx
SanskritiGupta39
 
PPT
11 Inheritance.ppt
LadallaRajKumar
 
PPT
Inheritance in C++
RAJ KUMAR
 
PDF
Inheritance chapter-6-computer-science-with-c++ opt
deepakskb2013
 
PDF
Inheritance
Pranali Chaudhari
 
PDF
inheritance-16031525566nbhij56604452.pdf
kashafishfaq21
 
PDF
Inheritance
Prof. Dr. K. Adisesha
 
PPT
inheritance
Amir_Mukhtar
 
PPTX
Inheritance
SangeethaSasi1
 
PPT
week14 (1).ppt
amal68766
 
PPSX
Inheritance
Selvin Josy Bai Somu
 
PPT
Inheritance
Aadhi Aadhithya
 
DOCX
oop database doc for studevsgdy fdsyn hdf
itxminahil29
 
PDF
lecture 6.pdf
WaqarRaj1
 
PPTX
simple notes Unit 4-Inheritance (2).pptx
riyanahameed04
 
PDF
chapter-10-inheritance.pdf
study material
 
PPT
OOP.ppt
Tanmay Dhatrak
 
PPTX
Inheritance (with classifications)
Redwan Islam
 
PPTX
Inheritance
prashant prath
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
urvashipundir04
 
inheritance_OOPC_datastream.ppttttttttttttttx
SanskritiGupta39
 
11 Inheritance.ppt
LadallaRajKumar
 
Inheritance in C++
RAJ KUMAR
 
Inheritance chapter-6-computer-science-with-c++ opt
deepakskb2013
 
Inheritance
Pranali Chaudhari
 
inheritance-16031525566nbhij56604452.pdf
kashafishfaq21
 
inheritance
Amir_Mukhtar
 
Inheritance
SangeethaSasi1
 
week14 (1).ppt
amal68766
 
Inheritance
Aadhi Aadhithya
 
oop database doc for studevsgdy fdsyn hdf
itxminahil29
 
lecture 6.pdf
WaqarRaj1
 
simple notes Unit 4-Inheritance (2).pptx
riyanahameed04
 
chapter-10-inheritance.pdf
study material
 
Inheritance (with classifications)
Redwan Islam
 
Inheritance
prashant prath
 
Ad

Recently uploaded (20)

PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 

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.