Access Specifiers in C++: The Complete Guide

0
348
Access Specifiers in C++ Example | C++ Access Specifiers

The data hiding feature prevents direct access to the internal class representation. Instead, the access restriction uses the class’s private, public, and protected keywords. These keywords, private, public, and protected, are known as access specifiers or modifiers in C++.

Access Specifiers in C++

Access Specifiers in C++ are used to define how we will access the class members. Access specifiers are also known as access modifiers. In addition, an access specifier is used to implement the property of data hiding in C++, an essential feature of object-oriented programming.

See the following syntax.

Syntax

See the following syntax.

class class_name
{
   private:   

        // private members are present under this column

   public:

       // public members are present under this column

   protected:

       // protected members are present under this column
};

Defining the access modifiers:

Public in C++

All the data members under the public keyword are available to everyone in the class, whether in the member functions or the main function; we can directly access the class members quoted under the public keyword.

We can directly access it by using the dot operator(.) with the object of the class. Public attributes of the class can be accessed by other classes, also.

Private in C++

All the data members under the private keyword are only accessible by the functions present inside the class. They cannot be directly accessed with the object of that particular class. A friend function can access the private members of the class.

By default, all the class members are private. The main use of using private is to keep the data secure, and we maintain it to the class internal representation only so that the user’s data is secure.

An example of a bank can be used here as we can store the bank employees’ salary as private data members as it is confidential data.

Protected in C++

A protected member possesses most of the properties of a private member. It has an additional feature than private data members in that it can be accessed by the derived or child classes. We can get an idea about derived and base classes in inheritance.

For now, we can say that the derived class is the class that has to inherit the properties of the base class. In addition, a protected member of the class is also inaccessible from outside the class until the member is used in the subclass.

Permissions for the class members under specific access specifier:

#Specifiers In same class Outside class In the derived class

Public        Yes    Yes    Yes

Private       Yes    No     No

Protected  Yes     No     Yes

#Examples for access specifiers in C++

#Example 1

Write a program to show the mechanism of public and private class members. Declare a data member as private in a class and use it in the functions derived in public and call the function using object of the class. See the following program.

#include <iostream>
using namespace std;
class A
{
private:
  int a, b;

public:
  void getdata(int x, int y)
  {
    a = x;
    b = y;
  }
  void showdata()
  {
    cout << "The sum of two private integer data members are = " << a + b << endl;
    cout << "The product of two private integer data members are = " << a * b << endl;
  }
};
int main()
{
  A obj;
  obj.getdata(4, 5);
  obj.showdata();
  return (0);
}

See the following output.

Access Specifiers in C++ Example

#Example 2

Write a program to show the mechanism of the protected access specifier. Use a variable declared in the class’s protected section in that class’s subclass.

See the following code example.

#include <iostream>
using namespace std;
class A
{
protected:
  int a, b;

public:
  void getdata(int x, int y)
  {
    a = x;
    b = y;
  }
};
class B : public A
{
public:
  void showdata()
  {
    cout << "The value of the first protected variable(output of subclass)= " << a << endl;
    cout << "The value of the second protected variable(output of subclass)" << b << endl;
    cout << "Sum of the protected variables = " << a + b << endl;
  }
};
int main()
{
  B obj;
  obj.getdata(4, 5);
  obj.showdata();
  return (0);
}

See the following output.

 

C++ Access Specifiers

That’s it for this tutorial.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.