SlideShare a Scribd company logo
INHERITANCE
Inheritance is the ability of one class to inherit the properties of another class. A new class can becreated from an existing class.  The existing class iscalled the Base class or Super class and the new class is called  the Derived class or Sub-class.e.g:Car inherits from another class auto-mobile.Science student inherits from class student
Advantages of Inheritance:Reusability of code Size of the code is reduced.Transitivity:	If  B is derived from A and C is derived from B     then C is also derived from A.
Person  -   Base ClassStudent -  Derived class
inheritance c++
inheritance c++
QUADRILATERALSQUARE                       RECTANGLE                      RHOMBUS
inheritance c++
inheritance c++
Identify the type of  inheritance:2009 DELHIclass FacetoFace{      char CenterCode[10];  public:      void Input();       void Output()   };class Online{       char website[50];      public:    void SiteIn();    void SiteOut();     };
class Training : public  FacetoFace, private Online{	long Tcode;	float Charge;int Period;      public:              void Register();              void Show();};:
Base Classes:FacetoFace          OnlineDerived Class:TrainingMultiple base classes   so    multiple inheritance
DelhiClass Dolls{	char Dcode[5];   protected:     float price;     void CalcPrice(float);Public:    Dolls();    void Dinput();    void Dshow();};
class  SoftDolls: public Dolls{      char SDName[20];       float Weight;public:SoftDolls();       void SDInput();       void SDShow();};class ElectronicDolls:  public Dolls{      char EDName[20];      char BatteryType[10];int Batteries; public:ElectronicDolls();       void EDInput();       void EDShow();};
BASE CLASS:   DOLLSElectronicDollsSoftDollsHIERARCHICAL INHERITANCE
Out-side Delhi 2006class furniture {       char Type;      char Model[10];    public:      furniture();    void Read_fur_Details();    void Disp_fur_Details();   };class Sofa : public furniture{intno_of_seats;    float cost_of_sofa;public:   void Read_sofa_details();   void Disp_sofa_details();};
class office : private  Sofa{intno_of_pieces;       char Delivery_date[10];    public:   void Read_office_details();   void Disp_office_details();};Void main(){     office MyFurniture;}
FurnitureSofaofficeSofa is derived from furnitureOffice is derived from sofa.Multi-level Inheritance
Visibility ModesIt can be public, private or protected. The private data of base class cannot be inherited.(i) If inheritance is done in public mode, public members of the base class becomethe public members of derived class and protected members of base class become the protected members of derived class.(ii) If inheritance is done in a private mode, public and protected members of  base class become the private members of derived class.(iii) If inheritance is done in a protected mode, public and protected members of base class become the protected members of derived class.
Accessibility of Base Class members:
#include<iostream.h>class one{int a;   // only for class members	 protected:int b;    // for class members and derived classes	 public:int c;   // for class members, derived classes, main	 one()	 {		  a=3;		  b=5;		  c=10;		  }	  void show()	  {cout<<a<<":"<<b<<":"<<c<<endl;		 }		 };
	 class two :public one	 {int p;		  public:		  two()		  {				p=25;				}		  void show1()		  {cout<<a<<endl;  \\ error.  Not accessiblecout<<b<<endl; \\o.k.cout<<c<<endl; \\o.k.			  }			  };
class three : public two	 {int x;			public :			three()			{				x=100;				}				void show2()				{cout<<x<<endl; \\o.k.cout<<p<<endl; \\error. Not accessiblecout<<b<<endl; \\o.k.cout<<c<<endl; \\o.k.					}					};
int main()		{				three ob;cout<<ob.c<<endl; \\o.k. public membercout<<ob.b<<endl; \\ error.  Not availableob.show();				ob.show1();				ob.show2();				return 0;                                                               }
#include<iostream.h>class one{int a;   // only for class members	 protected:int b;    // for class members and derived classes	 public:int c;   // for class members, derived classes,main	 one()	 {		  a=3;		  b=5;		  c=10;		  }	  void show()	  {cout<<a<<":"<<b<<":"<<c<<endl;		 }		 };
class two :protected one	 {int p;		  public:		  two()		  {				p=25;				}		  void show1()		  {cout<<a<<endl;  // error.  Not accessiblecout<<b<<endl; // o.k. protectedcout<<c<<endl; // o.k. becomes protected			  }			  };
class three : protected  two	 {int x;			public :			three()			{				x=100;				}				void show2()				{cout<<x<<endl; // o.k. its own member				cout<<p<<endl; // error. Not accessiblecout<<b<<endl; // o.k. protected				cout<<c<<endl; // o.k. has become protected					}					};
int main()			{			three ob;cout<<ob.c<<endl; // error has become protected not availablecout<<ob.b<<endl; // error.  Not available   				ob.show();   // error.  Has become protected not available	ob.show1();  // error.  Has become protected not available 	ob.show2(); // O.K.	return 0;				}
#include<iostream.h>class one{int a;   // only for class members	 protected:int b;    // for class members and derived classes	 public:int c;   // for class members, derived classes, main	 one()	 {		  a=3;		  b=5;		  c=10;		  }	  void show()	  {cout<<a<<":"<<b<<":"<<c<<endl;		 }		 };
	 class two :private  one	 {int p;		  public:		  two()		  {				p=25;				}		  void show1()		  {cout<<p<<endl; // o.k. its own membercout<<a<<endl;  // error.  Not accessiblecout<<b<<endl; // error.  has become private .cout<<c<<endl; // error .  has become private						  }			  };
class three : private   two	 {int x;			public :			three()			{			x=100;				}		void show2()				{					cout<<x<<endl; // o.k. its own membercout<<p<<endl; // error. Not accessible				cout<<b<<endl; // error.  not availablecout<<c<<endl; // error. not available					}				};
int main()			{			three ob;cout<<ob.c<<endl; // error not available		cout<<ob.b<<endl; // error.  Not available		ob.show();   // error.   not available			ob.show1();  // error . not available			ob.show2(); // o.k. its own member			return 0;				}

More Related Content

What's hot (20)

PPTX
Python-Polymorphism.pptx
Karudaiyar Ganapathy
 
PPTX
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
virtual function
VENNILAV6
 
PPTX
Classes and Objects in C#
Adeel Rasheed
 
PPTX
Inheritance in Object Oriented Programming
Ashita Agrawal
 
PPTX
07. Virtual Functions
Haresh Jaiswal
 
PPT
OOP in C++
ppd1961
 
PPTX
Operator overloading
Kumar
 
PPTX
Functions in c++
Rokonuzzaman Rony
 
PDF
Structures and Pointers
Prabu U
 
PPT
Function overloading(c++)
Ritika Sharma
 
PDF
Exception handling
Pranali Chaudhari
 
PPT
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
cprogrammings
 
PPTX
Inheritance in OOPS
Ronak Chhajed
 
PPTX
class and objects
Payel Guria
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PPTX
Array in c++
Mahesha Mano
 
PPTX
Destructors
DeepikaT13
 
PPTX
Polymorphism
Arif Ansari
 
Python-Polymorphism.pptx
Karudaiyar Ganapathy
 
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
virtual function
VENNILAV6
 
Classes and Objects in C#
Adeel Rasheed
 
Inheritance in Object Oriented Programming
Ashita Agrawal
 
07. Virtual Functions
Haresh Jaiswal
 
OOP in C++
ppd1961
 
Operator overloading
Kumar
 
Functions in c++
Rokonuzzaman Rony
 
Structures and Pointers
Prabu U
 
Function overloading(c++)
Ritika Sharma
 
Exception handling
Pranali Chaudhari
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
cprogrammings
 
Inheritance in OOPS
Ronak Chhajed
 
class and objects
Payel Guria
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Array in c++
Mahesha Mano
 
Destructors
DeepikaT13
 
Polymorphism
Arif Ansari
 

Viewers also liked (6)

PPTX
Principles and advantages of oop ppt
daxesh chauhan
 
PPTX
Encapsulation C++
Hashim Hashim
 
PPTX
encapsulation
shalini392
 
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
PPTX
Encapsulation
Githushan Gengaparam
 
PPTX
Inheritance
Sapna Sharma
 
Principles and advantages of oop ppt
daxesh chauhan
 
Encapsulation C++
Hashim Hashim
 
encapsulation
shalini392
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
Encapsulation
Githushan Gengaparam
 
Inheritance
Sapna Sharma
 
Ad

Similar to inheritance c++ (20)

PPT
inhertance c++
abhilashagupta
 
PPT
Inheritance
poonam.rwalia
 
PPT
Inheritance, polymorphisam, abstract classes and composition)
farhan amjad
 
PDF
Chapter 6 and inheritance OOP C++ tu ioe
EZERR1
 
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
ssuser6f3c8a
 
PPT
Lab3
karan saini
 
PPTX
OOP unit II inheritance.pptx object oriented programming
Srishti951154
 
PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PDF
lecture-2021inheritance-160705095417.pdf
AneesAbbasi14
 
PPT
Inheritance
piyush shukla
 
PDF
Inheritance chapter-6-computer-science-with-c++ opt
deepakskb2013
 
PPT
Inheritance
Mustafa Khan
 
PPTX
Inheritance.pptx
RutujaTandalwade
 
PPTX
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
DeepasCSE
 
PPT
Lecture 5 Inheritance
bunnykhan
 
PPTX
EASY TO LEARN INHERITANCE IN C++
Nikunj Patel
 
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
PPT
OOP
karan saini
 
PPT
Lecturespecial
karan saini
 
inhertance c++
abhilashagupta
 
Inheritance
poonam.rwalia
 
Inheritance, polymorphisam, abstract classes and composition)
farhan amjad
 
Chapter 6 and inheritance OOP C++ tu ioe
EZERR1
 
MODULE2_INHERITANCE_SESSION1.ppt computer
ssuser6f3c8a
 
OOP unit II inheritance.pptx object oriented programming
Srishti951154
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
lecture-2021inheritance-160705095417.pdf
AneesAbbasi14
 
Inheritance
piyush shukla
 
Inheritance chapter-6-computer-science-with-c++ opt
deepakskb2013
 
Inheritance
Mustafa Khan
 
Inheritance.pptx
RutujaTandalwade
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
DeepasCSE
 
Lecture 5 Inheritance
bunnykhan
 
EASY TO LEARN INHERITANCE IN C++
Nikunj Patel
 
Object Oriented Programming (OOP) using C++ - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Lecturespecial
karan saini
 
Ad

Recently uploaded (20)

PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Dimensions of Societal Planning in Commonism
StefanMz
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 

inheritance c++

  • 2. Inheritance is the ability of one class to inherit the properties of another class. A new class can becreated from an existing class. The existing class iscalled the Base class or Super class and the new class is called the Derived class or Sub-class.e.g:Car inherits from another class auto-mobile.Science student inherits from class student
  • 3. Advantages of Inheritance:Reusability of code Size of the code is reduced.Transitivity: If B is derived from A and C is derived from B then C is also derived from A.
  • 4. Person - Base ClassStudent - Derived class
  • 7. QUADRILATERALSQUARE RECTANGLE RHOMBUS
  • 10. Identify the type of inheritance:2009 DELHIclass FacetoFace{ char CenterCode[10]; public: void Input(); void Output() };class Online{ char website[50]; public: void SiteIn(); void SiteOut(); };
  • 11. class Training : public FacetoFace, private Online{ long Tcode; float Charge;int Period; public: void Register(); void Show();};:
  • 12. Base Classes:FacetoFace OnlineDerived Class:TrainingMultiple base classes so multiple inheritance
  • 13. DelhiClass Dolls{ char Dcode[5]; protected: float price; void CalcPrice(float);Public: Dolls(); void Dinput(); void Dshow();};
  • 14. class SoftDolls: public Dolls{ char SDName[20]; float Weight;public:SoftDolls(); void SDInput(); void SDShow();};class ElectronicDolls: public Dolls{ char EDName[20]; char BatteryType[10];int Batteries; public:ElectronicDolls(); void EDInput(); void EDShow();};
  • 15. BASE CLASS: DOLLSElectronicDollsSoftDollsHIERARCHICAL INHERITANCE
  • 16. Out-side Delhi 2006class furniture { char Type; char Model[10]; public: furniture(); void Read_fur_Details(); void Disp_fur_Details(); };class Sofa : public furniture{intno_of_seats; float cost_of_sofa;public: void Read_sofa_details(); void Disp_sofa_details();};
  • 17. class office : private Sofa{intno_of_pieces; char Delivery_date[10]; public: void Read_office_details(); void Disp_office_details();};Void main(){ office MyFurniture;}
  • 18. FurnitureSofaofficeSofa is derived from furnitureOffice is derived from sofa.Multi-level Inheritance
  • 19. Visibility ModesIt can be public, private or protected. The private data of base class cannot be inherited.(i) If inheritance is done in public mode, public members of the base class becomethe public members of derived class and protected members of base class become the protected members of derived class.(ii) If inheritance is done in a private mode, public and protected members of base class become the private members of derived class.(iii) If inheritance is done in a protected mode, public and protected members of base class become the protected members of derived class.
  • 20. Accessibility of Base Class members:
  • 21. #include<iostream.h>class one{int a; // only for class members protected:int b; // for class members and derived classes public:int c; // for class members, derived classes, main one() { a=3; b=5; c=10; } void show() {cout<<a<<":"<<b<<":"<<c<<endl; } };
  • 22. class two :public one {int p; public: two() { p=25; } void show1() {cout<<a<<endl; \\ error. Not accessiblecout<<b<<endl; \\o.k.cout<<c<<endl; \\o.k. } };
  • 23. class three : public two {int x; public : three() { x=100; } void show2() {cout<<x<<endl; \\o.k.cout<<p<<endl; \\error. Not accessiblecout<<b<<endl; \\o.k.cout<<c<<endl; \\o.k. } };
  • 24. int main() { three ob;cout<<ob.c<<endl; \\o.k. public membercout<<ob.b<<endl; \\ error. Not availableob.show(); ob.show1(); ob.show2(); return 0; }
  • 25. #include<iostream.h>class one{int a; // only for class members protected:int b; // for class members and derived classes public:int c; // for class members, derived classes,main one() { a=3; b=5; c=10; } void show() {cout<<a<<":"<<b<<":"<<c<<endl; } };
  • 26. class two :protected one {int p; public: two() { p=25; } void show1() {cout<<a<<endl; // error. Not accessiblecout<<b<<endl; // o.k. protectedcout<<c<<endl; // o.k. becomes protected } };
  • 27. class three : protected two {int x; public : three() { x=100; } void show2() {cout<<x<<endl; // o.k. its own member cout<<p<<endl; // error. Not accessiblecout<<b<<endl; // o.k. protected cout<<c<<endl; // o.k. has become protected } };
  • 28. int main() { three ob;cout<<ob.c<<endl; // error has become protected not availablecout<<ob.b<<endl; // error. Not available ob.show(); // error. Has become protected not available ob.show1(); // error. Has become protected not available ob.show2(); // O.K. return 0; }
  • 29. #include<iostream.h>class one{int a; // only for class members protected:int b; // for class members and derived classes public:int c; // for class members, derived classes, main one() { a=3; b=5; c=10; } void show() {cout<<a<<":"<<b<<":"<<c<<endl; } };
  • 30. class two :private one {int p; public: two() { p=25; } void show1() {cout<<p<<endl; // o.k. its own membercout<<a<<endl; // error. Not accessiblecout<<b<<endl; // error. has become private .cout<<c<<endl; // error . has become private } };
  • 31. class three : private two {int x; public : three() { x=100; } void show2() { cout<<x<<endl; // o.k. its own membercout<<p<<endl; // error. Not accessible cout<<b<<endl; // error. not availablecout<<c<<endl; // error. not available } };
  • 32. int main() { three ob;cout<<ob.c<<endl; // error not available cout<<ob.b<<endl; // error. Not available ob.show(); // error. not available ob.show1(); // error . not available ob.show2(); // o.k. its own member return 0; }