Private Inheritance In C++ - C++ Programming Tutorial
C++ Course / Inheritance / Private Inheritance In C++

Private Inheritance In C++

BLUF: Mastering Private Inheritance In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Private Inheritance In C++

C++ is renowned for its efficiency. Learn how Private Inheritance In C++ enables low-level control and high-performance computing in the tutorial below.

Private Inheritance

Private inheritance is a method that allows us to establish the has-a relationship in a distinct way. Through private inheritance, it is possible to transform the protected and public elements of a class into private members. Consequently, upon integrating the private member class, all the bases of the member class assume the status of private classes. Nevertheless, the inclusion of these private classes can be executed within the member function of the derived class.

Let's explore private inheritance through an example program.

Example

class Person {};
class Student:private Person {}; // private
void eat(const Person& p){} // anyone can eat
void study(const Student& s){} // only students study

int main() 
{
       Person p; // p is a Person
       Student s; // s is a Student
       eat(p); // fine, p is a Person
       eat(s); // error! s isn't a Person
       return 0;
}

In the provided code, within the context of public inheritance, when the association between two classes is designated as private, the compiler lacks the capability to transform the derived class into the base class. Consequently, the method eat is inaccessible for invocation by the object S.

In the scenario of public inheritance, the public method from the base class is carried over as a public method in the derived class. Essentially, this means that the derived class acquires the characteristics of the base class through inheritance, establishing an IS-A relationship. However, with private inheritance, the public method in the base class transforms into a private method in the derived class, regardless of the base class's access level being protected or public. Consequently, the derived class does not acquire the features of the base class in this context.

However, it is crucial to exercise caution when dealing with private inheritance as it can be quite intricate. In the context of private inheritance, the term "inherit" does not imply ownership. To illustrate, envision a scenario where a parent entrusts their special candy recipe to their child under the condition of maintaining its secrecy. While the child can distribute various types of candy to others, they are prohibited from disclosing the recipe. Through private inheritance, the derived class can be accessed by the base class but is not considered its possession. Consequently, the derived class conceals its interface from external entities, thereby only revealing the end product to the outside world.

By utilizing private inheritance, the implementation can inherit a class. Access to the class declaration is available to the user within the interface. Indirect access to the implementation class is also achievable through the class declaration.

Example

Example

#include <iostream>
 
using namespace std;
 
class Engine 
{
 public:
        Engine(int nc){
                cylinder = nc;
        }
 
        void start() {
               cout << getCylinder() <<" cylinder engine started" << endl;
        };
 
        int getCylinder() {
               return cylinder;
        }
        
private:
        int cylinder;
 };
 
 class Car : private Engine
{ // Car has-a Engine
 public:
   Car(int nc = 4) : Engine(nc) { }        
   void start() {
        cout << "car with " << Engine::getCylinder() <<
                  "cylinder engine started" << endl;
        Engine:: start();
   }
 }; 
 
int main( ) 
{ 
        Car c(8);
        c.start();
        return 0; 
}

Output:

Explanation:

As demonstrated in the example, the Car class ultimately receives an inherited Engine component like a piston. The Car method is able to internally utilize the Engine method, getCylinder, to retrieve the Engine component, piston.

Conclusion

  • Private inheritance is most likely to be a legitimate design strategy when you're dealing with two classes not related by is-a where one either needs access to the protected members of another or needs to redefine one or more of its virtual functions."
  • "Private inheritance means is-implemented-in-terms-of . It's usually inferior to composition."
  • "If you make class D privately inherit from a class B , you do so because you are interested in taking advantage of some of the features available in class B , not because there is any conceptual relationship between objects of types B and D ."
  • "Private inheritance means nothing during software design , only during software implementation ."

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience