• Like
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.



Published in Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 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. Syntaxclass derived-class-name : visibility modebase class-name{ ...// ...// members of derived class ...//};egg.Class ABC : private XYZ{ members of ABC};
  • 7. Visibility ModesBase Class Derived Class Derived Class Visibility: Visibility:Visibility Public Private Derivation Derivationprivate Not inherited Not inheritedprotected protected privatepublic public private
  • 8. A Exampleclass 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 beContents of object of class D ..X in class B : 1Y in class B : 2Y in class D : 3Z in class D : 4Y of B, show from D = 2
  • 11. Virtual InheritanceProblems with Multiple Inheritance:1.Ambiguos Function call2.Duplication of Data MembersSolutionUse virtual keywordclass A {};class B: virtual public A{};class C: virtual public A{};Class D: public B, public C {};
  • 12. Thank You..