Accessors and Mutators C++

accessor, c++, mutators

Solution

Accessor - Member function used to retrieve the data of protected members.

Mutators - Member function used to edit the data of protected members.

In your case,

class Customer
{
public:
    Customer();
    ~Customer();
    string getName(); // Accessor for the m_name variable
    void editName(string in); // Mutator for the m_name variable

private:
    string m_name;
    int m_age;

};

Inside your .cpp file:

string Customer::getName() {
    return m_name;
}

void Customer::editName(string in) {
    m_name = in;
}

Problem

I am currently trying to learn C++ and following an instruction. I've researched on mutators and accessors but I need a simple explanation. ``` class Customer { public: Customer(); ~Customer(); private: string m_name; int m_age; }; ``` Right the code above is in a header file. Within the instructions it is asking me to set a public accessors and mutator for both data. How do I do this? Also it mentions checking the age is not negative in the mutator. I know how to implement the code but I'm just confused on where to place it. Do I place the validation in this header file? or in the .cpp? or in the main method? I know this sounds all silly and I'm sure simple but I'd like to try and understand this.

Original source