INHERITANCE

 Inheritance:
A class can inherit the properties of its parent class.

The inheritance can be of the following types:
    (i) Single Inheritance
   (ii) MultiLevel Inheritance
  (iii) Multiple Inheritance
   (iv) Hybrid Inheritance
   
(i) Single Inheritance:
class A
class B : public A
(ii) MultiLevel Inheritance:
class A
class B : public A
class C : public B
(iii) Multiple Inheritance:
class A
class B : public A
class C : public A
class D : public B, public C

We can inherit a class as private (or) public.  

Private Inheritance:
If we inherit a base class as private, the public data members of the base class becomes private members to the derived class.

Public Inheritance:
If we inherit a base class as public, the public data members of the base class becomes public members to the derived class.

Protected Data Members:
   (i) The data members under the protected section are private members to the container class.
   (ii) The data members under the protected section of the base class becomes private to the derived class, if we derived the base class as private.
  (iii) The data members under the protected section of the base class becomes protected to the derived class, if we derived the base class as public.

Virtual Base Class:
Example:
class A
class B : public A
class C : public A
class D : public B, public C

    a) In the above example, both classes B and C are derived from A.  
    b) So both the classes inherit the properties of A.
    c) Class D is derived from class B and class C.  
    d) So an ambiguity occurs, when we access the members of A using D.

We need to inherit class A virtually from B and C.
class B : virtual A
class C : virtual A

So B and C shares the object A.

Popular posts from this blog

OBJECT ORIENTED ANALYSIS AND DESIGN (OOAD)

OBJECT ORIENTED PROGRAMMING

STARTING A BUSINESS