Accessors and Mutators C++

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.

Leave a Comment