Wednesday, December 5, 2018
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 c++ Getters and Setters as well.

C++ Accessor

An accessor is a member function which allows access to 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:

Must have a return type same as the data member.

It does not take any argument/s.

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:

Does not return any value.

It receives one argument and data member gets that value.

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

This is all about using getters and setters for single variables. It is very often to use arrays in our programs. What is the way to use getters and setters for arrays? Below are two programs explaining how to use getters and setters with arrays.

C++ Getters and Setters for Arrays

The first program sets an returns the individual elements of the array. This is just like the variables. Each time we need to set a value we pass the value along with the index of the array where we place this value in the array. Similarly to access a particular value from the array we pass the required index and get the value.

#include "stdafx.h"
#include "iostream"
#include"string"
using namespace std;
class person
{
   protected:
       string names[2];
       string address[2];
   public:
       string GetNames( int index)
       {
              return names[index];
       }
       void SetNames(int index, string n)
       {
              names[index] = n;
       }
       string GetAddress(int index)
       {
              return address[index];
       }
       void SetAddress(int index, string a)
       {
              address[index] = a;
       }
};
void main()
{
       person p;
       string n,a;
       int i = 0;
       cout << "Please enter Name for person : ";
       getline(cin, n);
       // setting name of person.
       p.SetNames(i, n);   
       cout << "Please enter Address for person : ";
       getline(cin, a);
       // setting Address of person.
       p.SetAddress(i, a);

       cout << "\nGetting Name of person using Getter : " << p.GetNames(i) << endl;
       cout << "\nGetting Address of person using Getter : " << p.GetAddress(i) << endl;
       i++;
}


c++ getter and Setters for arrays

 

 

 

 

 

This below program uses arrays. To set an array we pass the complete array to setter function and each element of the passed array is set to each position of the array declared in the class. The Getter function is just returning the address of the array. In Our program, we are storing the address in the pointer and thus gaining the access to elements of the array.

#include "stdafx.h"
#include "iostream"
#include"string"
using namespace std;
#define size 2
class person
{
   protected:
       string names[size];
       string address[size];
   public:
       string * GetNames()
       {
              return names;
       }
       void SetNames(string n[])
       {
              for (int i = 0;i < size;i++)
              {
                     names[i] = n[i];
              }         
       }
       string * GetAddress()
       {
              return address;
       }
       void SetAddress(string a[])
       {
              for (int i = 0;i < size;i++)
              {
                     address[i] = a[i];
              }
       }
};
void main()
{
       person p;
       string n[size],a[size],* names, *addresses;
       for (int i = 0;i < size;i++)
       {
              cout << "Please enter Name for person "<<i+1<<" : ";
              getline(cin, n[i]);
              cout << "Please enter Address for person " << i + 1 << " : ";
              getline(cin, a[i]);
              cout << endl;
       }  
       // setting Array of names.
       p.SetNames(n);      
    
       // setting Array of Addresses.
       p.SetAddress(a);

       //Getting Array of Names of person using Getter
       names = p.GetNames();

       //Getting Addresses of person using Getter
       addresses = p.GetAddress();
 
       for (int i = 0;i < size;i++)
       {
              cout << "\nUser Details\n";
              cout << "Name = " << names[i];
              cout << "\nAddress="<<addresses[i];
              cout << endl;
       }
}

 

c++ getters and Setters for arrays

Leave a Reply

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

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