Cpp Tutorial: C++ Access specifiers or Modifiers

Access specifiers are used to implement most important concept in C++. i.e Data Hiding.
We shall discuss how access specifiers will help in Data Hiding at the end of the chapter.
It is usual practice that we make Data members as private and expose them to the programmer by using public getters and setters.
There are 3 different types of access specifiers in C++:
1. public
2. private
3. protected

1. Public Access Specifier.

The data members declared with public access specifiers, will be available to everyone.
The data members and member functions declared as public can be accessed by other classes too during public inheritance.
The data members that are public, can be accessed directly by the object of the class by using dot “.” Operator.
Example:
#include <iostream>
using namespace std;

class Example
{
    public:
        int num;
        void square()
        {
            cout<<"The square of "<<num <<" is ="<<(num*num)<<endl;
        }
};

int main()
{
    Example e;
    e.num = 10; // we can access “num” directly by class object.
    e.square();
    return 0;
}
Output:
The square of 10 is =100

2. Private Access specifier

The class members declared as “private” will be able to access by the functions inside of the class.
They cannot be accessed by the functions of the derived class.
They cannot be accessed by creating the object of the class.
Only member functions that are able to access are the “friend” functions.
Below example shows, what happens if we try to access private data member by creating an object?
/*
* Copyright: @ prodevelopertutorial.com
*/

#include <iostream>
using namespace std;

class Example
{
    private:
        int num;
};


int main()
{
    Example e;
    e.num = 10;
}
Output:
main.cpp: In function ‘int main()’:
main.cpp:19:7: error: ‘int Example::num’ is private within this context
     e.num = 10;
       ^~~
main.cpp:12:13: note: declared private here
         int num;
             ^~~

3. Protected Access specifier.

Class members declared as protected, can be accessed by inside of that class and by derived classes.
They cannot be accessed by outside of the class.
In below example we access protected data member in derived class.
Example:
/*
* Copyright: @ prodevelopertutorial.com
*/

#include <iostream>
using namespace std;

class Parent
{
    protected:
        int num;
};


class Derived : public Parent
{
    public:
        void set_and_display_num(int id)
        {
            num = id;
            cout<<"The number is = "<< num<<endl;
        }
};

int main()
{
    Derived d;
    d.set_and_display_num (10);
}

Output:

The number is = 10

What is the need of Access specifiers and how it will help in data hiding?

In the previous example, we saw that, when a data member is made as public, it can be accessed by creating an object of the class.

For example, we need to create program to check the vote, and the user should be able to vote if the age is greater than 18 years.

If we put the “age” variable in public access specifier, that data can be modified easily by other programmer as shown below:

#include <iostream>
using namespace std;

class Vote{
    public:
    int age;
    
    void put_vote(int id);
    
};

void Vote::put_vote(int id)
{
    cout<<"You have voted for "<<id<<endl;
}

int main()
{
    Vote v;
    v.age  = 10;
    v.put_vote(2);
    return 0;
}

Output:

You have voted for 2

But this is not the desired behavior. We need to throw an error message that the age criteria is not met.

Hence we make the data member “age” as private and expose it to the getters and setters function.
There we can do the validation.

Example:

#include <iostream>
using namespace std;

class Vote
{
    int age;

    public:
    
    void set_age(int age);

    void put_vote(int id);
    
};

void Vote::set_age(int num)
{
    if (num > 18)
        age = num;
    else
    {
        cout<<"You have entered wrong age exiting..."<<endl;
        exit(0);
    }
    
}

void Vote::put_vote(int id)
{
    cout<<"You have voted for "<<id<<endl;
}

int main()
{
    Vote v;
    v.set_age(10);
    v.put_vote(2);
    return 0;
}

Output:

You have entered wrong age exiting...

This is a simple example, but in real-time there will be numerous dependencies, in that case it will be helpful.

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *