From the course: Object-Oriented Programming with C++

Unlock this course with a free trial

Join today to access over 24,400 courses taught by industry experts.

Doorways to data: Getters and setters

Doorways to data: Getters and setters - C++ Tutorial

From the course: Object-Oriented Programming with C++

Doorways to data: Getters and setters

- [Instructor] Let's fix our review class by allowing the controlled modification of its private data members. We'll implement special methods called setters and getters that act as access points to our data. A setter is a method for modifying or setting the value of a private member variable. It usually starts with set, followed by the variable name, like set rating. A setter can include validation logic to ensure the new value is valid before changing the data. A getter is a method for reading or getting the value of a private member variable. It typically starts with get followed by the variable name. For example, if we have a private variable called rating, we create a getter called get rating. The getter can perform additional operations or conversions before returning the value. Now, if all the getter does is return the value and the setter just assigns it directly to the private member variable, we are not adding any real value. In such cases, the member might as well be…

Contents