Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Inheritance

5,634 views

Published on

Inheritance in C++

Published in: Education, Sports
  • Be the first to comment

Inheritance

  1. 1. Inheritance
  2. 2. Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationship between them. Base class. Derived class. Advantages : <ul><li>Reusability </li></ul><ul><li>Extendibility </li></ul><ul><li>Easy debugging </li></ul>
  3. 3. Visibility Modifier : <ul><li>Public : A member declared as public is accessible by the member functions within its class and any class that derives it. </li></ul><ul><li>Private : A member declared as private is accessible by the member functions of its class only. </li></ul><ul><li>Protected : A member declared as protected is accessible by the member functions within its class and any class immediately derived from it. </li></ul>
  4. 4. Defining Derived Class Class derived_class_name : visibility mode base_class_name { … // members of derived class }; Eg: Class ABC : public XYZ {…};
  5. 5. Types of Inheritance : Single Inheritance Multilevel Inheritance A B A B C Class A {….}; Class B : public A {----}; Class A {…}; Class B : public A {…}; Class C : public B {…};
  6. 6. Multiple Inheritance Hierarchical Inheritance A B C A B C D Class A {…}; Class B {…}; Class C : public A, public B {…}; Class A {…}; Class B : public A {…}; Class C : public B {…}; Class D : public C {…};
  7. 7. Hybrid Inheritance A D B C Class A {…}; Class B : public A {…}; Class C : public B {…}; Class D : public C {…};
  8. 8. Visibility mode : <ul><li>When the base class is privately inherited by the derived class,’public members of the base class becomes ‘private members’ of the derived class. </li></ul><ul><li>When the base class is publicly inherited,’public members’ of the base class become ‘public members’ of the derived class. </li></ul><ul><li>When a base class is derived in protected mode,both the public and protected members of the class become protected members of the derived class. </li></ul>
  9. 9. Virtual Base Class A B C D Here, D will have duplicate sets of members inherited from A I.e, via B and C. Duplicate ambiguity can be avoided by making the common base class as virtual class
  10. 10. Class A {-----}; Class B : virtual public A {------}; Class C : public virtual A {------}; Class D : public B, public C {------}; When a class is made virtual base class, C++ takes necessary care to see that only one copy of that class is inherited, regardless of how many inheritance paths exist between the virtual base class and a derived class.
  11. 11. Thank You

×