Inheritance with access-specifier in cpp
Tags: oop, cpp, inheritance, accessspecifier
Published: 2024-01-18 04:42:23

When inheriting from a base class, the choice of access specifier determines how the members of the base class will be inherited and what their access level will be in the derived class. The syntax for inheritance is as follows:

```
class ChildClass : [access-specifier] ParentClass {
    // ...code...
};
```
Let's dive into the lines of codes:

```
class Parent
{
private:
    int pvt = 12;
    
protected:
    int prot = 42; // A protected member of the Base class

public:
    int pub = 16;
};

class Child1 : public Parent
{
public:
    int getProt()
    {
        return prot;
    }
};

class Child2 : protected Parent
{
public:
    int getProt()
    {
        return prot;
    }
};

class Child3 : private Parent
{
public:
    int getProt()
    {
        return prot;
    }
};
```

As you can see we have made three different children of the same parent class namely Parent. Now, the question is :

> What are the differences between these classes though they contain the exactly same lines of code inside them?

In the provided code, there are three classes: _Child1_, _Child2_, and _Child3_, each inheriting from the Parent class with different access specifiers. The access specifiers (public, protected, and private) determine the visibility of the base class members in the derived class. Let's explore the differences between these classes:

## Child1: Public Inheritance

```
class Child1 : public Parent
{
public:
    int getProt()
    {
        return prot;
    }
};
```
In this case, Child1 publicly inherits from Parent. The protected member_ prot_ of the Parent class is accessible in _Child1_ through its public member function _getProt()_. Objects of Child1 can access the protected member directly.

**Usage example:**

```
Child1 child1Obj;
int value = child1Obj.getProt(); // This is valid because getProt() is a public member function.
```

## Child2: Protected Inheritance

```
class Child2 : protected Parent
{
public:
    int getProt()
    {
        return prot;
    }
};
```
In this case, Child2 inherits from Parent with a _protected specifier_. This means that the protected and public members of the Parent class become protected members in_ Child2_. Outside the Child2 class, the protected members are not accessible.

**Usage example:**

```
Child2 child2Obj;
int value = child2Obj.getProt(); // This is valid because getProt() is a public member function.


// Accessing prot directly outside the class is not allowed.
// int protValue = child2Obj.prot; // This would result in a compilation error.
```

##Child3: Private Inheritance
```
class Child3 : private Parent
{
public:
    int getProt()
    {
        return prot;
    }
};
```
In this case, Child3 inherits from Parent with a private specifier. This means that both the protected and public members of the Parent class become private members in Child3. Outside the Child3 class, the inherited members are not accessible.

**Usage example:**
```
Child3 child3Obj;
int value = child3Obj.getProt(); // This is valid because getProt() is a public member function.

// Accessing prot directly outside the class is not allowed.
// int protValue = child3Obj.prot; // This would result in a compilation error.
```
To summarize, the main difference lies in the accessibility of the inherited members outside the derived classes. Public inheritance allows access to the inherited members, protected inheritance restricts access to the outside world, and private inheritance makes all inherited members private to the derived class.

Let's get connected on [Linkedin](https://www.linkedin.com/in/khan1203/) ^_^