answersLogoWhite

0

A "get" or "getter" function is a function that retrieves information. They are called getters because they are often named with a "get_" prefix, such as:

date get_current_date ();

The "get_" prefix is not actually required in C++, it is only required in languages that do not support function overloading. This is because most get functions also have a corresponding set function and you cannot use the same name twice without overloading:

date get_current_date ();

void set_current_date (const date&);

Getters and setters are usually defined as class member functions. However, rather than referring to them informally as getters and setters we refer to them formally as accessors and mutators, respectively. This reflects the fact that a getter typically accesses information without changing it whereas a setter typically mutates or changes information. For example:

class X {

private:

int data;

public:

X (int value): data {value} {} // constructor

X& operator= (int value) { data = value; return *this; } // mutator

int value () const { return data; } // accessor

// ...

};

The accessor is declared const and returns by value. In other words, the caller receives a copy of the information, not the information itself, and the class is left unaffected by the access.

The mutator, on the other hand, is declared non-const because the class representation (the data member) will need to be modified.

Constructors are not mutators because constructors create information, they do not mutate existing information.

User Avatar

Wiki User

7y ago

What else can I help you with?