Home > OOP > Accessor and Mutator in C++

One of the main features of Object Oriented programming is Encapsulation which allows users to specify labels for each its data members and member functions. Encapsulation limits outside access to data of the class. As we know that the members of other classes cannot access private data of a class. To solve this class must provide member functions to access and manipulate these private data members. C++ Accessor and mutator functions help in this regard.

Functions that can access and manipulate private data members of function are called Accessor & Mutator functions. Sometimes you may refer them as Getters and Setters as well.

C++ Accessor

An accessor is a member function which allows access of private or protected data member of a class outside the class. It is also called Getter.
There are a few things that accessor/getter must meet:

  1. Must have a return type same as the data member.
  2. It does not take any argument/s.
  3. Its name should start with “Get” prefix.

The syntax for writing accessor is:

data-type GetAccessor-Name ()
{
return data-member;
}


Data-type
must be same as for data member.
Data member it is the private data member which other classes want to access. accessor function must return it using the return statement.
E.g.

class MyClass
{
     private:
          int id;
     public:
          
          // creating accessor/getter for data member id. 
          int GetId() // return type is int as for id it is int.
          {
               return id;
          }
};

C++ Mutator

Mutator is a member function which allows manipulating the contents of private or protected data member of a class outside the class. It is also called Setter.
There are a few things that Mutator/setter must meet:

  1. Does not return any value.
  2. It receives one argument and data member gets that value.
  3. Its name should start with “Set” prefix.

Its syntax is:

void SetMutator-Name (data-type variable-Name)
{
data-member = variable-Name;
}


Data-type
for the parameter must be same as for data member.
E.g.

class MyClass
{
     private:
          int id;
     public:
          
          // creating mutator/setter for data member id. 
          int SetId(int x) // data type is int as for id it is int.
          {
               id=x;
          }
};

Example program: –

#include "stdafx.h"
#include "iostream"
#include"string"

using namespace std;

class person
{
     protected:
          string name;
          string address;
     public:
          string GetName()
          {
               return name;
          }
          void SetName( string n)
          {
               name = n;
          }
          string GetAddress()
          {
               return address;
          }
          void SetAddress(string a)
          {
               address = a;
          }
};

void main()
{
     person p;
     string x;
     cout << "Please enter Name for person : ";
     getline(cin, x);
     // setting name of person.
     p.SetName(x);
     cout << "\nGetting name of person using Getter : "<<p.GetName()<<endl;
}

Output: –
C++ Accessor Mutator example

Leave a Reply

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