---
title: "Difference between public, private, and protected inheritance in C++?"  
description: "Difference between public, private, and protected inheritance in C++?"  
author: "Utpal Vishwas"  
published: 2023-07-07  
updated: 2023-07-10  
canonical: https://www.mindstick.com/forum/158995/difference-between-public-private-and-protected-inheritance-in-c-plus-plus  
category: "visual c++"  
tags: ["c++", "programming language"]  
reading_time: 3 minutes  

---

# Difference between public, private, and protected inheritance in C++?

[Difference between public](https://www.mindstick.com/forum/159586/what-is-the-difference-between-public-protected-package-private-and-private-in-java), [private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers), and protected [inheritance](https://www.mindstick.com/articles/13095/inheritance-and-its-types) in [C++](https://www.mindstick.com/blog/745/array-in-c-plus-plus)?

## Replies

### Reply by Aryan Kumar

In C++, inheritance is a mechanism that allows a new class to inherit the properties and behavior of an existing class. The three types of inheritance are public, private, and protected.

- **Public inheritance** makes all the members of the base class public in the derived class. This means that the members of the base class can be accessed by the derived class and by any other class that uses the derived class.
- **Private inheritance** makes all the members of the base class private in the derived class. This means that the members of the base class can only be accessed by the derived class, and not by any other class that uses the derived class.
- **Protected inheritance** makes all the members of the base class protected in the derived class. This means that the members of the base class can be accessed by the derived class and by any class that is derived from the derived class.

The type of inheritance that you use will depend on the specific needs of your application. If you want the derived class to have full access to the members of the base class, then you should use public inheritance. If you want to restrict access to the members of the base class, then you should use private or protected inheritance.

Here is an example of how to use public inheritance:

C++

```plaintext
class Base {
 public:
  int x;
  void print() {
    cout << x << endl;
  }
};

class Derived : public Base {
 public:
  void doSomething() {
    x = 10; // This is allowed because x is public in Base
    print(); // This is allowed because print() is public in Base
  }
};
```

In this example, the `Derived` class inherits from the `Base` class using public inheritance. This means that all the members of the `Base` class are public in the `Derived` class. Therefore, the `Derived` class can access the `x` variable and the `print()` method from the `Base` class.

Here is an example of how to use private inheritance:

C++

```plaintext
class Base {
 private:
  int x;
  void print() {
    cout << x << endl;
  }
};

class Derived : private Base {
 public:
  void doSomething() {
    // This is not allowed because x is private in Base
    // x = 10;
    // This is also not allowed because print() is private in Base
    // print();
  }
};
```

In this example, the `Derived` class inherits from the `Base` class using private inheritance. This means that all the members of the `Base` class are private in the `Derived` class. Therefore, the `Derived` class cannot access the `x` variable or the `print()` method from the `Base` class.

Here is an example of how to use protected inheritance:

C++

```plaintext
class Base {
 protected:
  int x;
  void print() {
    cout << x << endl;
  }
};

class Derived : protected Base {
 public:
  void doSomething() {
    // This is allowed because x is protected in Base
    x = 10;
    // This is also allowed because print() is protected in Base
    print();
  }
};
```

In this example, the `Derived` class inherits from the `Base` class using protected inheritance. This means that all the members of the `Base` class are protected in the `Derived` class. Therefore, the `Derived` class can access the `x` variable and the `print()` method from the `Base` class, but only from within the `Derived` class or from any class that is derived from the `Derived` class.


---

Original Source: https://www.mindstick.com/forum/158995/difference-between-public-private-and-protected-inheritance-in-c-plus-plus

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
