AppDividend
Latest Code Tutorials

Access Specifiers in C++ Example | C++ Access Specifiers

0

Access Specifiers in C++ Example | C++ Access Specifiers is today’s topic. Access Specifiers are used to defining how we are going to access the members of the class. Access specifiers are also known as access modifiers. Access specifier is used to implementing the property of data hiding in C++, which is an essential feature of object-oriented programming.

Access Specifiers in C++

Data hiding feature prevents direct access to the internal class representation. The access restriction is done by using private, public, and protected keywords inside the class. These keywords, private, public, and protected, are known as access specifiers or modifiers in C++. 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

All the data members who are under the public keyword is available to everyone in the class be it the member functions, or in the main function, we can directly access the class members who are 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

All the data members who are under private keyword is 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 data of the user is secure.

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

A protected member possesses most of the properties of a private member. It has an additional feature than of private data members that it is 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 derived class is the class which has to inherit the properties of the base class. Protected member of the class is also inaccessible from outside the class until the member is being 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 protected access specifier. Use a variable declared in the protected section of the class in the subclass of that class.

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

Finally, Access Specifiers in C++ Example | C++ Access Specifiers is over.

Leave A Reply

Your email address will not be published.

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