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

PPTX
Inheritance in c++
PPTX
Abstract class in c++
PDF
Constructors and Destructors
PPTX
classes and objects in C++
PPTX
Inheritance in C++
PPTX
Friend function & friend class
PPTX
Inheritance
PPTX
Virtual base class
Inheritance in c++
Abstract class in c++
Constructors and Destructors
classes and objects in C++
Inheritance in C++
Friend function & friend class
Inheritance
Virtual base class

What's hot (20)

PPTX
Inheritance
PPTX
Data members and member functions
PPT
Friends function and_classes
PPTX
Inheritance in Object Oriented Programming
PPTX
Nested class
PPTX
[OOP - Lec 18] Static Data Member
PPTX
This keyword in java
PPTX
Classes and objects in c++
PPTX
Constructors and destructors
PPT
Inheritance : Extending Classes
PPTX
Inheritance in c++
PPTX
Class or Object
PDF
Object-oriented Programming-with C#
PPTX
Inheritance in JAVA PPT
PPTX
Delegates and events in C#
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
PPTX
Access modifier and inheritance
PPT
Inheritance OOP Concept in C++.
PPTX
Structure in c language
PPTX
20.3 Java encapsulation
Inheritance
Data members and member functions
Friends function and_classes
Inheritance in Object Oriented Programming
Nested class
[OOP - Lec 18] Static Data Member
This keyword in java
Classes and objects in c++
Constructors and destructors
Inheritance : Extending Classes
Inheritance in c++
Class or Object
Object-oriented Programming-with C#
Inheritance in JAVA PPT
Delegates and events in C#
[OOP - Lec 09,10,11] Class Members & their Accessing
Access modifier and inheritance
Inheritance OOP Concept in C++.
Structure in c language
20.3 Java encapsulation
Ad

Similar to inheritance c++ (20)

PPT
inhertance c++
PPT
Inheritance
PPT
Inheritance, polymorphisam, abstract classes and composition)
PDF
Chapter 6 and inheritance OOP C++ tu ioe
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
PPT
PPTX
OOP unit II inheritance.pptx object oriented programming
PDF
lecture-2021inheritance-160705095417.pdf
PPTX
[OOP - Lec 20,21] Inheritance
PPT
Inheritance
PDF
Inheritance chapter-6-computer-science-with-c++ opt
PPT
Inheritance
PPTX
Inheritance.pptx
PPTX
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
PPT
Lecture 5 Inheritance
PPTX
EASY TO LEARN INHERITANCE IN C++
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
PPT
Lecturespecial
PPT
inhertance c++
Inheritance
Inheritance, polymorphisam, abstract classes and composition)
Chapter 6 and inheritance OOP C++ tu ioe
MODULE2_INHERITANCE_SESSION1.ppt computer
OOP unit II inheritance.pptx object oriented programming
lecture-2021inheritance-160705095417.pdf
[OOP - Lec 20,21] Inheritance
Inheritance
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance
Inheritance.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
Lecture 5 Inheritance
EASY TO LEARN INHERITANCE IN C++
Object Oriented Programming (OOP) using C++ - Lecture 3
Lecturespecial
Ad

Recently uploaded (20)

PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Cell Types and Its function , kingdom of life
PDF
Basic Mud Logging Guide for educational purpose
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Cell Structure & Organelles in detailed.
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Cell Types and Its function , kingdom of life
Basic Mud Logging Guide for educational purpose
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial diseases, their pathogenesis and prophylaxis
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Institutional Correction lecture only . . .
Final Presentation General Medicine 03-08-2024.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Complications of Minimal Access Surgery at WLH
2.FourierTransform-ShortQuestionswithAnswers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
RMMM.pdf make it easy to upload and study
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Insiders guide to clinical Medicine.pdf
Cell Structure & Organelles in detailed.

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; }