Inheritance

680 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
680
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Inheritance

  1. 1. Inheritance Anurag Pramod Daware TE H 22
  2. 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. 3. Inheritance• Allows Hierarchical Classification of Objects.
  4. 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. 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. 6. Syntaxclass derived-class-name : visibility modebase class-name{ ...// ...// members of derived class ...//};egg.Class ABC : private XYZ{ members of ABC};
  7. 7. Visibility ModesBase Class Derived Class Derived Class Visibility: Visibility:Visibility Public Private Derivation Derivationprivate Not inherited Not inheritedprotected protected privatepublic public private
  8. 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. 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. 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. 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. 12. Thank You..

×