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

PPTX
Inheritance in java
PPTX
Inheritance in Object Oriented Programming
PPTX
Inheritance in oops
PPTX
oops concept in java | object oriented programming in java
PPTX
Inheritance in c++
PPTX
Access specifier
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
PPTX
Inheritance in c++theory
Inheritance in java
Inheritance in Object Oriented Programming
Inheritance in oops
oops concept in java | object oriented programming in java
Inheritance in c++
Access specifier
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Inheritance in c++theory

What's hot (20)

PPTX
Inheritance in C++
PPTX
Inheritance in c++
PPT
Unary operator overloading
PPTX
Inheritance in OOPS
PPTX
Destructors
PPTX
Inheritance in java
PPTX
Single inheritance
PPTX
Polymorphism In c++
PPTX
Inheritance in c++
PPTX
Templates in C++
PPT
Inheritance, Object Oriented Programming
PPTX
Hybrid inheritance
PPTX
Virtual base class
PPTX
inheritance c++
PPTX
OOPS Basics With Example
PPT
Inheritance
PPT
Class and object in C++
PPTX
Object oriented programming
PPTX
Java abstract class & abstract methods
ODP
OOP java
Inheritance in C++
Inheritance in c++
Unary operator overloading
Inheritance in OOPS
Destructors
Inheritance in java
Single inheritance
Polymorphism In c++
Inheritance in c++
Templates in C++
Inheritance, Object Oriented Programming
Hybrid inheritance
Virtual base class
inheritance c++
OOPS Basics With Example
Inheritance
Class and object in C++
Object oriented programming
Java abstract class & abstract methods
OOP java
Ad

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

PPTX
inheritance_OOPC_datastream.ppttttttttttttttx
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
PPT
Inheritance in C++
PPTX
Introduction to Inheritance
PPTX
Inheritance
PPSX
Inheritance and Polymorphism in Oops
PDF
PPT
week14 (1).ppt
PPTX
Introduction to inheritance and different types of inheritance
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
PDF
lecture-2021inheritance-160705095417.pdf
PPTX
[OOP - Lec 20,21] Inheritance
PPT
inheritance
PPTX
Inheritance
PPTX
Object oriented programming new syllabus presentation
PPT
Inheritance
PPTX
Inheritance
PPTX
Lecture 5.mte 407
PPT
Inheritance.ppt
PPT
session 24_Inheritance.ppt
inheritance_OOPC_datastream.ppttttttttttttttx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
Inheritance in C++
Introduction to Inheritance
Inheritance
Inheritance and Polymorphism in Oops
week14 (1).ppt
Introduction to inheritance and different types of inheritance
MODULE2_INHERITANCE_SESSION1.ppt computer
lecture-2021inheritance-160705095417.pdf
[OOP - Lec 20,21] Inheritance
inheritance
Inheritance
Object oriented programming new syllabus presentation
Inheritance
Inheritance
Lecture 5.mte 407
Inheritance.ppt
session 24_Inheritance.ppt
Ad

Recently uploaded (20)

PPTX
Onica Farming 24rsclub profitable farm business
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
English Language Teaching from Post-.pdf
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Onica Farming 24rsclub profitable farm business
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Open Quiz Monsoon Mind Game Final Set.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Microbial diseases, their pathogenesis and prophylaxis
Renaissance Architecture: A Journey from Faith to Humanism
English Language Teaching from Post-.pdf
NOI Hackathon - Summer Edition - GreenThumber.pptx
Pharma ospi slides which help in ospi learning
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Microbial disease of the cardiovascular and lymphatic systems
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.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.