C++ Class Inheritance Public Vs Private

5 min read Jul 01, 2024
C++ Class Inheritance Public Vs Private

C++ Class Inheritance: Public vs Private

Inheritance is a powerful feature in C++ that allows you to create new classes (derived classes) based on existing classes (base classes). This enables code reusability and promotes a hierarchical structure for your program. Two important access specifiers control how a derived class inherits members from its base class: public and private.

Public Inheritance

In public inheritance, members of the base class are inherited as public members in the derived class. This means:

  • Accessibility: Public members of the base class remain public in the derived class.
  • Reusability: The derived class can access, modify, and use all public members of the base class directly.
  • Interface: Public inheritance indicates an "is-a" relationship. For example, a Dog class inheriting publicly from an Animal class implies that a Dog is an Animal.

Example:

class Animal {
public:
    void eat() { cout << "Animal is eating" << endl; }
};

class Dog : public Animal {
public:
    void bark() { cout << "Dog is barking" << endl; }
};

int main() {
    Dog myDog;
    myDog.eat(); // Accessing public member 'eat' from base class
    myDog.bark();
    return 0;
}

Private Inheritance

In private inheritance, members of the base class are inherited as private members in the derived class. This means:

  • Accessibility: Public and protected members of the base class become private in the derived class. They cannot be directly accessed from objects of the derived class.
  • Reusability: The derived class can access and use base class members only through its own member functions.
  • Implementation: Private inheritance indicates an "implementation based on" relationship. For example, a Car class inheriting privately from an Engine class implies that a Car uses an Engine for its functionality.

Example:

class Engine {
public:
    void start() { cout << "Engine started" << endl; }
};

class Car : private Engine {
public:
    void drive() {
        start(); // Accessing 'start' from base class through derived class member function
        cout << "Car is driving" << endl;
    }
};

int main() {
    Car myCar;
    myCar.drive(); // Only public members of derived class are accessible
    // myCar.start(); // Error: 'start' is private in derived class
    return 0;
}

Choosing Between Public and Private Inheritance

  • Public inheritance is used when you want to establish an "is-a" relationship and allow direct access to the base class's public members. This is suitable for representing inheritance hierarchies where the derived class is a specialized version of the base class.
  • Private inheritance is used when you want to use the base class implementation for the derived class without exposing the base class's interface. This is suitable when the derived class reuses functionality from the base class but doesn't want to be considered an "is-a" subtype.

Understanding the difference between public and private inheritance is crucial for effective object-oriented programming in C++. Choose the appropriate access specifier based on the relationship you want to establish between your classes.

Latest Posts


Featured Posts