Site icon TestingDocs.com

C++ Accessor & Mutator Member Functions

Overview

In this tutorial, we will learn about C++ Accessor & Mutator Member Functions. We use these methods to access or modify the private data members of a class from the code outside the class.

Accessor Member Function

The accessor member function gets the value of a private data member of the class.

This method is also known as Get or Getter method.

//C++ Accessor Member Function
    string getName()
    {
        return name; // return employees's name
    }

Mutator Member Function

The mutator member function sets a new value to the private data member of the class. There are other use cases where we use the mutator method to clean or ensure that the private data variable can only have valid data.

This method is also known as Set or Setter method.

 

//C++ Mutator Member Function
    void setName(string name)
    {
        this->name = name;// set employees's name
    }

C++ Tutorials

C++ Tutorials on this website:

https://www.testingdocs.com/c-coding-tutorials/

For more information on the current ISO C++ standard

https://isocpp.org/std/the-standard

Exit mobile version