Public, Private and Protected Inheritance 1. Public Inheritance
Public, Private and Protected Inheritance 1. Public Inheritance
1. Public Inheritance
class B : public A
};
The line class B : public A tells the compiler that we are inheriting class A in class B in public
followings :
(a) All the public members of class A becomes public members of class B.
(b) All the protected members of class A becomes protected members of class B.
2. Private Inheritance
class B : private A
};
The line class B : private A tells the compiler that we are inheriting class A in class B in private mode.
In private mode inheritance note the following points :
(a) All the public members of class A becomes private members of the class B.
(b) All the protected members of the class A becomes private members of class B.
class B : A
};
class B : protected A
};
The line class B : protected A tells the compiler that we are inheriting class A in class B in protected
mode. In protected mode inheritance note the following points :
(a) All the public members of class A becomes protected members of class B.
(b) All the protected members of class A becomes protected members of class B.
Note : A class A is inherited in class B in public mode, all protected members of class A becomes
protected for class B. Now if this class B is inherited to some new class C, then these protected
members inherited from A will be available from A will be available to class C, in whatever mode
you inherit class B to class C.
When a derived class inherit base class as private, protected and public members of base class will
become private members of derived class.
When a derived class inherit base class as public, protected members of base class will become
protected members of derived class and public members of base class will become public members of
derived class.