0% found this document useful (0 votes)
23 views

Public, Private and Protected Inheritance 1. Public Inheritance

This document discusses the different types of inheritance in C++ - public, private, and protected. Public inheritance makes public and protected members of the base class public and protected in the derived class. Private inheritance makes all members private. Protected inheritance makes public members protected and protected members remain protected. The default is private inheritance if no access specifier is provided. The access level of members changes depending on whether the base class is inherited publicly, privately, or protected.

Uploaded by

Sudha Poojary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Public, Private and Protected Inheritance 1. Public Inheritance

This document discusses the different types of inheritance in C++ - public, private, and protected. Public inheritance makes public and protected members of the base class public and protected in the derived class. Private inheritance makes all members private. Protected inheritance makes public members protected and protected members remain protected. The default is private inheritance if no access specifier is provided. The access level of members changes depending on whether the base class is inherited publicly, privately, or protected.

Uploaded by

Sudha Poojary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PUBLIC, PRIVATE AND PROTECTED INHERITANCE

1. Public Inheritance

Consider the dummy code given below for 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.

(c) Private members are never inherited.

2. Private Inheritance

Consider the dummy code given below for 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.

(c) Private members are never inherited.

The above dummy code can be written as too.

class B : A

};

As the default inheritance mode is private mode.


3. Protected Inheritance

Consider the dummy code given below for inheritance :

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.

(c) Private members are never inherited.

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.

Access base class as private

When a derived class inherit base class as private, protected and public members of base class will
become private members of derived class.

Access base class as protected


When a derived class inherit base class as protected, protected and public members of base class will
become protected members of derived class.

Access base class as public

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.

You might also like