SlideShare a Scribd company logo
1 of 12
Inheritance



              Anurag Pramod Daware
              TE H 22
Inheritance
• One of the Major pillars of OOP approach.
• Process by which one object acquires properties
  of another.
• New classes to be built from older one.
• ‘IS A’ relationship

• Ex:                Parent
         Parent     Features   Base Class



                     Parent
                    Features
          Child       Child    Derived Class
                    Features
Inheritance
• Allows Hierarchical Classification of Objects.
Benefits
• Reusable code, reliability and decreased
  maintenance cost.

• Code Sharing: software level and class level

• Extension: Construction of new system from old
  components: focus on new and unusual parts
  of system
Types of Inheritance in C++
                                                  A
     A


                                        B         C        D
     B

                                            (D) Hierarchical
(a) Single       A                  B

     A
                                                  A
                           C

     B               (C) Multiple       B                  C


     C
                                                  D
(b) Multilevel                              (E) Hybrid
Syntax
class derived-class-name : visibility mode
base class-name
{
  ...//
  ...// members of derived class
  ...//
};

egg.
Class ABC : private XYZ
{
  members of ABC
};
Visibility Modes
Base Class   Derived Class   Derived Class
             Visibility:     Visibility:
Visibility
             Public          Private
             Derivation      Derivation

private      Not inherited Not inherited
protected    protected       private

public       public          private
A Example
class B //base class
{
 protected:
                int x; int y;
 public:
        B() { }             //default constructor
        void read()
         {
           cout<<“ Enter X of Class B :”; cin>>x;

         cout<<“ Enter Y of Class B :”; cin>>y;
        }
        void show()
        {
         cout<<“ X in class B:” << x <<endl;
         cout<<“ y in class B:” << y <<endl;
class D: public B //derived class
{
 protected:
                int y, int z;
 public:
        D() { }             //default constructor
        void read()
         {
           B :: read(); //read base class data first
           cout<<“ Enter Y of Class D :”; cin>>y;

         cout<<“ Enter Z of Class D :”; cin>>z;
        }
        void show()
        {
          B :: show(); //display base class data
         cout<<“ X in class B:” << x <<endl;
         cout<<“ y in class B:” << y <<endl;
void main()
{
  D d;           //obj of derived

  cout<< “Enter data for object of class D ..” <<endl;
  d.read();

   cout<< “Contents of object of class D ..” <<endl;
   d.show();
  }
--------------------------------------------------------------
if input given is 1 2 3 4 op will be
Contents of object of class D ..
X in class B : 1
Y in class B : 2
Y in class D : 3
Z in class D : 4
Y of B, show from D = 2
Virtual Inheritance
Problems with Multiple Inheritance:
1.Ambiguos Function call
2.Duplication of Data Members

Solution

Use virtual keyword
class A {};
class B: virtual public A{};
class C: virtual public A{};
Class D: public B, public C {};
Thank You..

More Related Content

What's hot

Inheritance
InheritanceInheritance
Inheritance
Tech_MX
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Friend functions
Friend functions Friend functions
Friend functions
Megha Singh
 

What's hot (20)

Single inheritance
Single inheritanceSingle inheritance
Single inheritance
 
Lab3
Lab3Lab3
Lab3
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Inheritance
InheritanceInheritance
Inheritance
 
Friend functions
Friend functions Friend functions
Friend functions
 
inheritance
inheritanceinheritance
inheritance
 
Design of OO language
Design of OO languageDesign of OO language
Design of OO language
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 

Viewers also liked

C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
shatha00
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 

Viewers also liked (13)

C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
Oop inheritance
Oop inheritanceOop inheritance
Oop inheritance
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
OOP Chapter 8 : Inheritance
OOP Chapter 8 : InheritanceOOP Chapter 8 : Inheritance
OOP Chapter 8 : Inheritance
 
Interesting Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming Interesting Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming
 
OOP Inheritance
OOP InheritanceOOP Inheritance
OOP Inheritance
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 

Similar to Inheritance

Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
deepakskb2013
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
Payel Guria
 

Similar to Inheritance (20)

C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
lecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdflecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdf
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Presentation on Polymorphism (SDS).ppt
Presentation on Polymorphism (SDS).pptPresentation on Polymorphism (SDS).ppt
Presentation on Polymorphism (SDS).ppt
 
Inheritance_PART2.pptx
Inheritance_PART2.pptxInheritance_PART2.pptx
Inheritance_PART2.pptx
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Recently uploaded (20)

Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 

Inheritance

  • 1. Inheritance Anurag Pramod Daware TE H 22
  • 2. Inheritance • One of the Major pillars of OOP approach. • Process by which one object acquires properties of another. • New classes to be built from older one. • ‘IS A’ relationship • Ex: Parent Parent Features Base Class Parent Features Child Child Derived Class Features
  • 3. Inheritance • Allows Hierarchical Classification of Objects.
  • 4. Benefits • Reusable code, reliability and decreased maintenance cost. • Code Sharing: software level and class level • Extension: Construction of new system from old components: focus on new and unusual parts of system
  • 5. Types of Inheritance in C++ A A B C D B (D) Hierarchical (a) Single A B A A C B (C) Multiple B C C D (b) Multilevel (E) Hybrid
  • 6. Syntax class derived-class-name : visibility mode base class-name { ...// ...// members of derived class ...// }; egg. Class ABC : private XYZ { members of ABC };
  • 7. Visibility Modes Base Class Derived Class Derived Class Visibility: Visibility: Visibility Public Private Derivation Derivation private Not inherited Not inherited protected protected private public public private
  • 8. A Example class B //base class { protected: int x; int y; public: B() { } //default constructor void read() { cout<<“ Enter X of Class B :”; cin>>x; cout<<“ Enter Y of Class B :”; cin>>y; } void show() { cout<<“ X in class B:” << x <<endl; cout<<“ y in class B:” << y <<endl;
  • 9. class D: public B //derived class { protected: int y, int z; public: D() { } //default constructor void read() { B :: read(); //read base class data first cout<<“ Enter Y of Class D :”; cin>>y; cout<<“ Enter Z of Class D :”; cin>>z; } void show() { B :: show(); //display base class data cout<<“ X in class B:” << x <<endl; cout<<“ y in class B:” << y <<endl;
  • 10. void main() { D d; //obj of derived cout<< “Enter data for object of class D ..” <<endl; d.read(); cout<< “Contents of object of class D ..” <<endl; d.show(); } -------------------------------------------------------------- if input given is 1 2 3 4 op will be Contents of object of class D .. X in class B : 1 Y in class B : 2 Y in class D : 3 Z in class D : 4 Y of B, show from D = 2
  • 11. Virtual Inheritance Problems with Multiple Inheritance: 1.Ambiguos Function call 2.Duplication of Data Members Solution Use virtual keyword class A {}; class B: virtual public A{}; class C: virtual public A{}; Class D: public B, public C {};