How Access Modifiers Behaves C++ Inheritance

Share Your Love

In this post, we are going to learn about access modifiers behaves from the parent-child relationship.

The private member cannot be passed down through the generations. If we change the visibility mode to the public, however, we lose the benefit of data hiding.

Protected is a third visibility modification introduced by C++. All member functions inside the class, as well as the class that is directly derived from it, will have access to the protected member.

There are three different types of visibility modes:

visibility mode
Access Modifiers

public:

When a member is made public, it is available to all of the program’s operations.

private:

When a member is marked private, it can only be accessed by other members of the class.

protected:

When a member is designated protected, it is available both within its own class and the class that is derived from it immediately.

Join Our Community

Join our WhatsApp Group To know more About Programming Language tips, tricks and knowledge about programming and how to start learning any programming language.

The Inherited Visibility Members Are:

Base class visibilityDerived class visibility
Public PrivateProtected
PrivateNot InheritedNot InheritedNot Inherited
ProtectedProtectedPrivateProtected
PublicPublicPrivateProtected
Protected

Parent Class Inherited To Child Class As Public:

//Access Modifiers In C++

#include <iostream>

using namespace std;

class Parent
{
public:
    Parent()
    {
        cout << "Parent Constructor." << endl;
    }
};
class Child : public Parent
{
public:
    Child()
    {
        cout << "Child Constructor." << endl;
    }
};

int main()
{
    Child C;
    return 0;
}

Output:

Parent Constructor.
Child Constructor.

Parent Class Inherited To Child Class As Private:

//Access Modifiers In C++

#include <iostream>

using namespace std;

class Parent
{
public:
    Parent()
    {
        cout << "Parent Constructor." << endl;
    }
};
class Child : private Parent
{
public:
    Child()
    {
        cout << "Child Constructor." << endl;
    }
};

int main()
{
    Child C;
    return 0;
}

Output:

Parent Constructor.
Child Constructor.

Join Our Community

Join our WhatsApp Group To know more About Programming Language tips, tricks and knowledge about programming and how to start learning any programming language.

Parent Class Inherited To Child Class As Protected:

//Access Modifiers In C++

#include <iostream>

using namespace std;

class Parent
{
public:
    Parent()
    {
        cout << "Parent Constructor." << endl;
    }
};
class Child : protected Parent
{
public:
    Child()
    {
        cout << "Child Constructor." << endl;
    }
};

int main()
{
    Child C;
    return 0;
}

Output:

Parent Constructor.
Child Constructor.

Now, To Change Parent Class Members As Public, Private and Protected:

Parent Class Member As Public And Inherited As Public
//Access Modifiers In C++

#include <iostream>

using namespace std;

class Parent
{
public:
    int x = 90;
    Parent()
    {
        cout << "Parent Constructor." << endl;
    }
};
class Child : protected Parent
{
public:
    Child()
    {
        cout << x << endl;
        cout << "Child Constructor." << endl;
    }
};

int main()
{
    Child C;
    return 0;
}

Output:

Parent Constructor.
90
Child Constructor.
Parent Class Member As Private And Inherited As Public:
//Access Modifiers In C++

#include <iostream>

using namespace std;

class Parent
{
private:
    int x = 90;
    Parent()
    {
        cout << "Parent Constructor." << endl;
    }
};
class Child : public Parent
{
public:
    Child()
    {
        cout << x << endl;
        cout << "Child Constructor." << endl;
    }
};

int main()
{
    Child C;
    return 0;
}

Output:

access_modifiers.cpp: In constructor 'Child::Child()':
access_modifiers.cpp:20:5: error: 'Parent::Parent()' is private within this context
     {
     ^
access_modifiers.cpp:11:5: note: declared private here
     Parent()
     ^~~~~~
access_modifiers.cpp:21:17: error: 'int Parent::x' is private within this context
         cout << x << endl;
                 ^
access_modifiers.cpp:10:13: note: declared private here
     int x = 90;

In the above output, if parent class members are private then they can’t access outside of class.

Parent Class Member As Protected And Inherited As Protected:
//Access Modifiers In C++

#include <iostream>

using namespace std;

class Parent
{
protected:
    int x = 90;
    Parent()
    {
        cout << "Parent Constructor." << endl;
    }
};
class Child : protected Parent
{
public:
    Child()
    {
        cout << x << endl;
        cout << "Child Constructor." << endl;
    }
};

int main()
{
    Child C;
    return 0;
}

Output:

Parent Constructor.
90
Child Constructor.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 366

Newsletter Updates

Enter your email address below to subscribe to our newsletter