C++ Access Specifiers

In this lesson, we will learn how to use public, protected, and private inheritance in C++ using examples to understand the topic better.


C++ Access Specifiers in inheritance public, protected, and private

Different access modes can derive a child class from the base class in C++ inheritance.


What are Access Specifiers?

An access specifier is a defining piece of code that can specify which program elements are permitted access to a particular variable or other pieces of code. Access specifiers specify how a class’s members—its attributes and methods—can be accessed. For instance, in C++ inheritance, three keywords are known as access specifiers.

  • public
  • protected
  • private
Accessibility Within Same Class In Derived Class Outside Class
Private Members Yes No No
Protected Members Yes Yes No
Public Members Yes Yes Yes

Example

class Base {
  public:
    int var1;
  protected:
    int var2;
  private:
    int var3;
};
class PublicDerived: public Base {
  // var1 is public
  // var2 is protected
  // var3 is not accessible from PublicDerived
};

class ProtectedDerived: protected Base {
  // var1 is protected
  // var2 is protected
  // var3 is not accessible from ProtectedDerived
};

class PrivateDerived: private Base {
  // var1 is private
  // var2 is private
  // var3 is not accessible from PrivateDerived
};

C++ Access Specifiers Public Inheritance

Public inheritance makes protected base class members protected in the derived class and makes public base class members public in the derived class.

Example: C++ Access Specifiers Public Inheritance

// C++ program demonstrates public inheritance
#include <iostream>
using namespace std;
class Base {
  private:
    int privateVariable = 100;
  protected:
    int protectedVariable = 200;
  public:
    int publicVariable = 300;
  // function to access private member
  int getPrivateMember() {
    return privateVariable;
  }
};
class PublicDerived: public Base {
  public:
    // function to access protected member from Base
    int getProtected() {
      return protectedVariable;
    }
};
int main() {
  PublicDerived obj;
  cout << "Private Access Specifier = " << obj.getPrivateMember() << endl;
  cout << "Protected Access Specifier = " << obj.getProtected() << endl;
  cout << "Public Access Specifier = " << obj.publicVariable << endl;
  return 0;
}

Output

Private Access Specifier = 100
Protected Access Specifier = 200
Public Access Specifier = 300

In this case, we used public mode to derive PublicDerived from Base. Because of this, in PublicDerived:

  • It is inherited that protectedVariable is protected.
  • getPrivateMember() and publicVariable are inherited as public.
  • Since privateVariable is private in Base, you cannot access it.
  • We must build the public functions getPrivateMember() and getProtected() to access private and protected members since they are not accessible from the main().

C++ Access Specifiers Protected Inheritance

Base Class public and protected members are protected in the derived class due to protected inheritance.

Example: C++ Access Specifiers Protected Inheritance

// C++ program demonstrates protected inheritance
#include <iostream>

using namespace std;
class Base {
  private:
    int privateVariable = 100;
  protected:
    int protectedVariable = 200;
  public:
    int publicVariable = 300;
  // function to access private member
  int getPrivateMember() {
    return privateVariable;
  }
};
class ProtectedDerived: protected Base {
  public:
    // function to access protected members from Base
    int getProtected() {
      return protectedVariable;
    }

  // function to access public members from Base
  int getPublic() {
    return publicVariable;
  }
};
int main() {
  ProtectedDerived obj;
  cout << " error getPrivateMember() is inaccessible within this context" << endl;
  cout << "Protected Access Specifier = " << obj.getProtected() << endl;
  cout << "Public Access Specifier = " << obj.getPublic() << endl;
  return 0;
}

Output

error getPrivateMember() is inaccessible within this context
Protected Access Specifier = 200
Public Access Specifier = 300
  • In the above example, we derived ProtectedDerived from Base in protected mode.
  • As a result, in ProtectedDerived: protectedVariable, publicVariable, and getPrivateMember() are inherited as protected.
  • privateVariable is inaccessible since it is private in the Base.
  • Since privateVariable is private in Base, you cannot access it.
  • Protected members are known to be inaccessible from outside the class. As a result, we cannot use getPrivateMember() from the ProtectedDerived class.
  • That’s why we need to create the getPublic() function in ProtectedDerived to access the publicVariable variable.

C++ Access Specifiers Private Inheritance

Due to private inheritance, the base class of public and protected members becomes private in the derived class.

Note
The derived class cannot access the private members of the base class.

Example: C++ Access Specifiers Private Inheritance

// C++ program demonstrates private inheritance
#include <iostream>
using namespace std;
class Base {
  private:
    int privateVariable = 100;
  protected:
    int protectedVariable = 200;
  public:
    int publicVariable = 300;
  // function to access private member
  int getPrivateMember() {
    return privateVariable;
  }
};
class PrivateDerived: private Base {
  public: int getProtected() {
    return protectedVariable;
  }
  int getPublic() {
    return publicVariable;
  }
};
int main() {
  PrivateDerived obj;
  cout << "error: 'int Base::getPrivateMember()' is inaccessible within this context" << endl;
  cout << "Protected Access Specifier = " << obj.getProtected() << endl;
  cout << "Public Access Specifier = " << obj.getPublic() << endl;
  return 0;
}

Output

error: 'int Base::getPrivateMember()' is inaccessible within this context
Protected Access Specifier = 200
Public Access Specifier = 300
  • In the above illustration, we have derived the PrivateDerived class from Base in private mode.
  • As a result, in PrivateDerived class, protectedVariable, publicVariable, and getPrivateMember() are inherited as private.
  • privateVariable is inaccessible since it has a private mode in the Base class.
  • We cannot access private members directly from outside the class. As a result, we cannot use getPrivateMember() from the PrivateDerived.
  • That’s why we need to create the getPublic() function in PrivateDerived to access the publicVariable variable.

This concludes the C++ Access Specifiers lesson. In The next lesson, you will learn about Inheritance Types in C++ and their usage.