INHERITANCE IN C++
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?
WHAT IS AN INHERTANCE? contd…
MAMMAL
All mammals have
certain characteristics.
Dog is a mammal. It has all features of
mammals in addition to its own unique
features
Cat is a mammal. It has all features of
mammals in addition to its own unique
features
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.
 A derived class can access all the protected and public members of its base class.
 It can not access private members of the base class.
ACCESS CONTROL AND INHERITENCE
PRIVATE
PROTECTED
PUBLIC
BASE CLASS
CHILD CLASS
CAN NOT BE INHERITED
We can summarize the different access types according to who can access them in
the following way:
Access public protected private
Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no
NOTE: Constructors and destructors of the base class are never inherited.
ACCESS CONTROL AND INHERITENCE contd…
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 public 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
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
EXAMPLE
#include <iostream>
using namespace std;
class Person
{
int id;
char name[100];
public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "nName: " << name <<endl;
}
};
class Student : private Person {
char course[50];
int fee;
public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}
void display_s()
{
display_p();
cout <<"Course: "<< course << "nFee: " << fee << endl;
}
};
int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
MULTILEVEL INHERITENCE
In Multi level inheritance, a subclass inherits from a
class that itself inherits from another class.
syntax
// C++ program to implement
// Multilevel Inheritance
using namespace std;
// single base class
class A {
public:
int a;
void getAdata()
{
cout << "Enter value of a: ";
cin >> a;
}
};
// derived class from base class
class B : public A {
public:
int b;
void getBdata()
{
cout << "Enter value of b: ";
cin >> b;
}
};
// derived from classB
class C : public B {
private:
int c;
public:
void getCdata()
{
cout << "Enter value of c: ";
cin >> c;
}
// function to print sum
void sum()
{
int ans = a + b + c;
cout << "sum: " << ans;
}
};
int main()
{
// object of sub class
C obj;
obj.getAdata();
obj.getBdata();
obj.getCdata();
obj.sum();
return 0;
}
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.
CONSTRUCTORS AND DESTRUCTORS IN
BASE AND DERIVED CLASSES
 Derived classes can have their own constructors and
destructors.
 When an object of a derived class is created, the base class’s
constructor is executed first, followed by the derived class’s
constructor.
 When an object of a derived class goes out of scope, its
destructor is called first, then that of the base class.

More Related Content

PPTX
EASY TO LEARN INHERITANCE IN C++
PPTX
Inheritance in c++
PPTX
inheritance_OOPC_datastream.ppttttttttttttttx
PPT
11 Inheritance.ppt
PPT
Inheritance in C++
PPT
Inheritance
PDF
Inheritance chapter-6-computer-science-with-c++ opt
PPT
Inheritance in C++
EASY TO LEARN INHERITANCE IN C++
Inheritance in c++
inheritance_OOPC_datastream.ppttttttttttttttx
11 Inheritance.ppt
Inheritance in C++
Inheritance
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance in C++

Similar to inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx (20)

PPTX
Inheritance
PPT
session 24_Inheritance.ppt
PDF
lecture 6.pdf
PDF
Inheritance
PDF
PPTX
00ps inheritace using c++
PPTX
Access controlaspecifier and visibilty modes
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
PPTX
OOPS IN C++
PDF
chapter-10-inheritance.pdf
PPTX
Inheritance in c++theory
PPT
Inheritance
PPTX
Introduction to inheritance and different types of inheritance
PPSX
PPT
Inheritance OOP Concept in C++.
PPTX
Inheritance.pptx
PPT
inheritence
PPTX
C++ presentation
PPT
Inheritance
PPTX
Inheritance
Inheritance
session 24_Inheritance.ppt
lecture 6.pdf
Inheritance
00ps inheritace using c++
Access controlaspecifier and visibilty modes
MODULE2_INHERITANCE_SESSION1.ppt computer
OOPS IN C++
chapter-10-inheritance.pdf
Inheritance in c++theory
Inheritance
Introduction to inheritance and different types of inheritance
Inheritance OOP Concept in C++.
Inheritance.pptx
inheritence
C++ presentation
Inheritance
Inheritance
Ad

More from urvashipundir04 (20)

PPTX
introduction to python in detail including .pptx
PPTX
kewords in python using 35 keywords.pptx
PPTX
stack in python using different datatypes.pptx
PPTX
Game Playing in Artificial intelligence.pptx
PPTX
extended modelling in dbms using different.pptx
PPTX
PRODUCTION SYSTEM in data science .pptx
PPTX
Presentation1 in datamining using techn.pptx
PPTX
Dependency modelling in data mining.pptx
PPTX
INTRODUCTION to datawarehouse IN DATA.pptx
PPTX
SOCIAL NETWORK ANALYISI in engeenireg.pptx
PPTX
datamining in engerring using different techniques.pptx
PPTX
datamining IN Artificial intelligence.pptx
PPTX
Underfitting and Overfitting in Machine Learning.pptx
PPTX
introduction values and best practices in
PPTX
ppt on different topics of circular.pptx
PPTX
list in python and traversal of list.pptx
PPT
ermodelN in database management system.ppt
PPTX
libraries in python using different .pptx
PPTX
tuple in python is an impotant topic.pptx
PPTX
ANIMATION in computer graphics using 3 D.pptx
introduction to python in detail including .pptx
kewords in python using 35 keywords.pptx
stack in python using different datatypes.pptx
Game Playing in Artificial intelligence.pptx
extended modelling in dbms using different.pptx
PRODUCTION SYSTEM in data science .pptx
Presentation1 in datamining using techn.pptx
Dependency modelling in data mining.pptx
INTRODUCTION to datawarehouse IN DATA.pptx
SOCIAL NETWORK ANALYISI in engeenireg.pptx
datamining in engerring using different techniques.pptx
datamining IN Artificial intelligence.pptx
Underfitting and Overfitting in Machine Learning.pptx
introduction values and best practices in
ppt on different topics of circular.pptx
list in python and traversal of list.pptx
ermodelN in database management system.ppt
libraries in python using different .pptx
tuple in python is an impotant topic.pptx
ANIMATION in computer graphics using 3 D.pptx
Ad

Recently uploaded (20)

PPTX
Software-Development-Life-Cycle-SDLC.pptx
DOCX
ENVIRONMENTAL PROTECTION AND MANAGEMENT (18CVL756)
PDF
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
PPTX
IOP Unit 1.pptx for btech 1st year students
PDF
V2500 Owner and Operatore Guide for Airbus
PPTX
DATA STRCUTURE LABORATORY -BCSL305(PRG1)
PPTX
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
PPT
UNIT-I Machine Learning Essentials for 2nd years
PDF
Software defined netwoks is useful to learn NFV and virtual Lans
PPTX
Unit IImachinemachinetoolopeartions.pptx
PPTX
Design ,Art Across Digital Realities and eXtended Reality
PDF
Using Technology to Foster Innovative Teaching Practices (www.kiu.ac.ug)
PDF
Mechanics of materials week 2 rajeshwari
PPTX
1. Effective HSEW Induction Training - EMCO 2024, O&M.pptx
PPTX
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
DOCX
An investigation of the use of recycled crumb rubber as a partial replacement...
PDF
MACCAFERRY GUIA GAVIONES TERRAPLENES EN ESPAÑOL
PDF
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
PPTX
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
PDF
Introduction to Machine Learning -Basic concepts,Models and Description
Software-Development-Life-Cycle-SDLC.pptx
ENVIRONMENTAL PROTECTION AND MANAGEMENT (18CVL756)
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
IOP Unit 1.pptx for btech 1st year students
V2500 Owner and Operatore Guide for Airbus
DATA STRCUTURE LABORATORY -BCSL305(PRG1)
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
UNIT-I Machine Learning Essentials for 2nd years
Software defined netwoks is useful to learn NFV and virtual Lans
Unit IImachinemachinetoolopeartions.pptx
Design ,Art Across Digital Realities and eXtended Reality
Using Technology to Foster Innovative Teaching Practices (www.kiu.ac.ug)
Mechanics of materials week 2 rajeshwari
1. Effective HSEW Induction Training - EMCO 2024, O&M.pptx
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
An investigation of the use of recycled crumb rubber as a partial replacement...
MACCAFERRY GUIA GAVIONES TERRAPLENES EN ESPAÑOL
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
Introduction to Machine Learning -Basic concepts,Models and Description

inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx

  • 2. 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?
  • 3. WHAT IS AN INHERTANCE? contd… MAMMAL All mammals have certain characteristics. Dog is a mammal. It has all features of mammals in addition to its own unique features Cat is a mammal. It has all features of mammals in addition to its own unique features
  • 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.  A derived class can access all the protected and public members of its base class.  It can not access private members of the base class. ACCESS CONTROL AND INHERITENCE PRIVATE PROTECTED PUBLIC BASE CLASS CHILD CLASS CAN NOT BE INHERITED
  • 8. We can summarize the different access types according to who can access them in the following way: Access public protected private Same class yes yes yes Derived classes yes yes no Outside classes yes no no NOTE: Constructors and destructors of the base class are never inherited. ACCESS CONTROL AND INHERITENCE contd…
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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 public 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
  • 13. 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
  • 14. SINGLE INHERITENCE Single inheritance is the one where you have a single base class and a single derived class.
  • 15. 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
  • 18. EXAMPLE #include <iostream> using namespace std; class Person { int id; char name[100]; public: void set_p() { cout << "Enter the Id:"; cin >> id; cout << "Enter the Name:"; cin >> name; } void display_p() { cout << endl <<"Id: "<< id << "nName: " << name <<endl; } };
  • 19. class Student : private Person { char course[50]; int fee; public: void set_s() { set_p(); cout << "Enter the Course Name:"; cin >> course; cout << "Enter the Course Fee:"; cin >> fee; } void display_s() { display_p(); cout <<"Course: "<< course << "nFee: " << fee << endl; } };
  • 21. MULTILEVEL INHERITENCE In Multi level inheritance, a subclass inherits from a class that itself inherits from another class.
  • 23. // C++ program to implement // Multilevel Inheritance using namespace std; // single base class class A { public: int a; void getAdata() { cout << "Enter value of a: "; cin >> a; } };
  • 24. // derived class from base class class B : public A { public: int b; void getBdata() { cout << "Enter value of b: "; cin >> b; } };
  • 25. // derived from classB class C : public B { private: int c; public: void getCdata() { cout << "Enter value of c: "; cin >> c; } // function to print sum void sum() { int ans = a + b + c; cout << "sum: " << ans; } };
  • 26. int main() { // object of sub class C obj; obj.getAdata(); obj.getBdata(); obj.getCdata(); obj.sum(); return 0; }
  • 27. MULTIPLE INHERITENCE In Multiple inheritances, a derived class inherits from multiple base classes. It has properties of both the base classes.
  • 28. 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
  • 29. HIERARCHICAL INHERITENCE In hierarchical Inheritance, it's like an inverted tree. So multiple classes inherit from a single base class.
  • 30. 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
  • 31. HYBRID INHERITENCE It combines two or more types of inheritance. In this type of inheritance we can have a mixture of number of inheritances.
  • 32. CONSTRUCTORS AND DESTRUCTORS IN BASE AND DERIVED CLASSES  Derived classes can have their own constructors and destructors.  When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor.  When an object of a derived class goes out of scope, its destructor is called first, then that of the base class.