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 - 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
-
-
-
-
Why should we hide information?1m 44s
-
(Locked)
Doorways to data: Getters and setters7m
-
(Locked)
Applying the DRY principle6m 56s
-
(Locked)
Challenging const correctness3m 3s
-
(Locked)
Friends3m 9s
-
(Locked)
Challenge: Bank account with audit system1m 33s
-
(Locked)
Solution: Bank account with audit system2m 33s
-
-
-
-
-