DEV Community

Cover image for Inheritance with access-specifier in cpp
Rifat Khan
Rifat Khan

Posted on

Inheritance with access-specifier in cpp

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...
};
Enter fullscreen mode Exit fullscreen mode

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;
    }
};
Enter fullscreen mode Exit fullscreen mode

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;
    }
};
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Child2: Protected Inheritance

class Child2 : protected Parent
{
public:
    int getProt()
    {
        return prot;
    }
};
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Child3: Private Inheritance

class Child3 : private Parent
{
public:
    int getProt()
    {
        return prot;
    }
};
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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 ^_^

Top comments (3)

Collapse
 
khan1203 profile image
Rifat Khan

Awesome explanation and smooth understandable examples. Great man. Keep up the good job.

Collapse
 
pgradot profile image
Pierre Gradot

You've commented with the same account...

Collapse
 
khan1203 profile image
Rifat Khan

I know, This is called self-motivation. I never do it for others, but only for me. So, it is not a fact to hide from others. Even no need to use any fake account. Hehe