Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ why use public, private or protected inheritance?

Well there is enough information about this subject. For example this thread was very clear to me: Difference between private, public, and protected inheritance

Except one point; Why is it useful?

like image 877
Tim Avatar asked Jan 19 '13 12:01

Tim


People also ask

Why do we need protected inheritance?

public inheritance makes public members of the base class public in the derived class, and the protected members of the base class remain protected in the derived class. protected inheritance makes the public and protected members of the base class protected in the derived class.

Why and when do we use protected instead of private?

- Private data members cannot be accessed outside the class. - When a class inherits a base class, all the data members except the private get inherited into it. So if we want data members to be accessible to only derived classes and not privately or publicly accessible, then we can use protected.

What is the difference between public/private protected inheritance?

Private Inheritance : All the public and protected members in base become private. Protected Inheritance : All the public and protected members in base class become protected. Public Inheritance : In case of public inheritance, public remains public and protected remains protected..

What is public/private and protected?

Broadly speaking, public means everyone is allowed to access, private means that only members of the same class are allowed to access, and protected means that members of subclasses are also allowed.


1 Answers

The answer to this question concerns class interfaces and data encapsulation, rather than language capabilities.

The use cases of protected and private inheritance are rather limited, since there are often other options available which better solve the problem (such as using composition, rather than inheritance). However, there are times when you necessarily must inherit from some type (for example to interface with a third-party library), but you would strongly prefer (for reasons related to user interface of your class) to hide most members inherited from the base class from the users of your new type. A typical scenario would be when you need your type to have the member functions of a certain class for internal use, but it would break the logic of your new type if it was called from outside the class itself.

In these situations, you need to use private or protectedinheritance (depending on whether the interface should be similarly restricted to further derived classes or not.

Bear in mind, however, that this is all just about (strongly) hinting to the users of your class how they should use it. You're adapting its public interface to hide certain features which were public in its base class. This doesn't strictly speaking prevent people from accessing these members, since anyone can still cast a pointer to your derived class to a pointer to the base, and reach the "hidden" resources that way.

like image 199
Agentlien Avatar answered Oct 05 '22 17:10

Agentlien