| by Arround The Web | No comments

How to Control Access Modifiers in C++: Understanding Member Visibility

Data hiding is one of the key features of C++ which refers to the limiting access of data in a class to prevent the unwanted access to the data of a class from outside. Hence, the access modifiers are used to define which class members are accessible to which outside source and which not.

Access Modifiers in C++

Access modifiers are used to manage the accessibility and the visibility of the data of a class. They set some restrictions to the data members so that they cannot be accessed by the outside functions. The access modifiers are of three types in C++:

Public Access Modifier in C++

All the members of the class declared under the keyword public will be available to all the functions outside the class within the program. The direct member access operator (.) also known as the dot operator can be used with the object of that specific class to access these data members.

Example

The public access modifier is used to declare a class, and its data members are accessed by the other functions within the program.

#include<iostream>

#include<conio.h>

#include<math.h>

using namespace std;
 class Triangle
 {
    public:
        float x,y,z;
   void input()
    {
       cout<<"Enter the side x of a triangle:-";
       cin>>x;
       cout<<"Enter the side y of a triangle:-";
       cin>>y;
       cout<<"Enter the side z of a triangle:-";
       cin>>z;
    }
    void Area()
     {
       float s=(x+y+z)/2;
       float area=sqrt(s*(s-x)*(s-y)*(s-z));
       cout<<"\nArea of Triangle ="<<area;
     }
};
    int main()
      {
         Triangle t1;
         t1.input();
         t1.Area();
         return 0;
}

The Triangle is defined as the class and its parameters are under the public keyword. Then the area of the triangle is calculated outside this class through accessing the data members of the public modifier.

The three sides of the triangle are input by the user and the area of the triangle is calculated by accessing the values outside the class, using the standard formula, which is 13.4164.

Private Access Modifier in C++

All the members of the class declared under the keyword private will not be available to any function outside the class in the program. These private members can only be read by the direct members within the same class. However, friend functions and classes can also access the data.

Example

The two classes public and private are declared, and the return value of the program is to be the area of the triangle.

#include<iostream>

#include<conio.h>

#include<math.h>

  using namespace std;
  class Triangle
  {

    private:
      float x, y, z;
    public:
    void input()

    {

        cout<<"Enter the side x of a triangle:-";
        cin>>x;
        cout<<"Enter the side y of a triangle:-";
        cin>>y;
        cout<<"Enter the side z of a triangle:-";
        cin>>z;

    }
      void Area()
       {

          float s=(x+y+z)/2;
          float area=sqrt(s*(s-x)*(s-y)*(s-z));
          cout<<"\nArea of Triangle ="<<area;

       }

};

    int main()
    {
        Triangle t1;
        t1.input();
        t1.Area();
        return 0;

}

In main(), the object t1 cannot directly access the private class variable float x, y and z, so it can only be indirectly manipulated through the public function input(), since this function obtains the values of x, y and z.

The area of the triangle is obtained by accessing the private class through manipulating it using public class. If the values of x, y and z won’t be declared in public class, then they won’t be accessed in the main() for calculations.

Protected Access Modifier in C++

The members and functions under the protected keyword are protected, they can only be accessed within the class and derived class. Rest of the classes cannot access their data. They are highly protected.

Example

The protected data under the protected keyword can only be read within the class and by the class which is the derived class. This example is the practical demonstration of the protected access modifier.

#include<iostream>

#include<conio.h>

#include<math.h>

 using namespace std;
 //parent class
 class Triangle
 {

  protected:
     float x, y, z;
 };
 //child class
 class TriangleChild: public Triangle{
    public:
   void input()

    {

       cout<<"Enter the side x of a triangle:-";
       cin>>x;
       cout<<"Enter the side y of a triangle:-";
       cin>>y;
       cout<<"Enter the side z of a triangle:-";
       cin>>z;

    }
      void Area()
        {
            float s=(x+y+z)/2;
            float area=sqrt(s*(s-x)*(s-y)*(s-z));
            cout<<"\nArea of Triangle ="<<area;

        }
};

     int main()

      {
          TriangleChild Child;
          Child.input();
          Child.Area();
          return 0;

      }

TriangleChild is an inherited class which is derived from class Triangle. The variables x, y and z are declared in Triangle with the protected keyword. This means that these variables are protected but can be accessed by the TriangleChild since Triangle is its parent class. The values of variables have been assigned in TriangleChild even though they are declared in the Triangle class.

The variables are accessed through the child class, and then the area of a triangle is calculated.

Conclusion

Data hiding prevents the unwanted access to the data of a class from outside. Access modifiers are used to manage the accessibility and the visibility of the data in a program. Private, protected and public access modifiers are used for hiding the data. Data under the public keyword can be read by the functions from outside the class too. The data declared as private can only be read within the class, while protected data can be accessed by the parent and the child class as well.

Share Button

Source: linuxhint.com

Leave a Reply