- Notifications
You must be signed in to change notification settings - Fork 0
Accessors
Martin Jean-Christio edited this page Oct 25, 2022 · 1 revision
In practice, all attributes of a class are private. Getters are the interface between the user and private attributes, to be sure that the values are always correct.
getter:
- The accessor member function gets the value of a private data member of the class.
- This method is also known as
getor Getter method.
setter:
- 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
setor Setter method.
#ifndef SAMPLE_CLASS_H # define SAMPLE_CLASS_H class Sample { public: Sample(void); ~Sample(void); intgetFoo(void) const; void setFoo(int v); private: int_foo; }; #endifSample::Sample(void) { std::cout << "Constructor called" << std::endl; this->setFoo(0); std::cout << "this->getFoo() = " << this->getFoo() << std::endl; return; } Sample::~Sample(void) { std::cout << "Destructor called" << std::endl; return; } // grant only read-only access to the attribute intSample::getFoo(void) const { // return a copy of the _foo attribute return this->_foo; } // this way allows the user to change the _foo attribute voidSample::setFoo(int v) { // don't allow _foo to have a negative value if (v >= 0) this->_foo = v; return; }