Getters and Setters in C++: A Simple Guide with Example
C++ Tips
Introduction:
In object-oriented programming, encapsulation is one of the key principles. Encapsulation allows us to keep the internal workings of a class hidden from the outside world, exposing only the necessary information. One powerful way to achieve this in C++ is by using Getters and Setters — methods that allow controlled access to the private members of a class.
In this post, we’ll explore how Getters and Setters work in C++, and we’ll walk through a simple example of managing an air conditioning system with an ACControl
class.
Why Use Getters and Setters?
In C++, class members can be declared as private
, which means they are hidden from direct access outside of the class. This is great for protecting the integrity of your data, but what if you still need to access or modify those private members? That’s where Getters and Setters come into play.
- Getter: A method that retrieves (or gets) the value of a private variable.
- Setter: A method that modifies (or sets) the value of a private variable.
These methods provide a controlled way to interact with an object’s internal state, enforcing any rules or logic before changing values.
Example: ACControl
Class
Let’s take an example of an air conditioning system. The ACControl
class will have private members to store the current temperature, a base temperature, and the current mode of the air conditioner (like "Cooling" or "Heating"). Using Getters and Setters, we’ll allow safe modification of these values.
#include <iostream>
#include <string>
class ACControl {
private:
double _temp = 25.0;
double _baseTemp = 25.0;
std::string _acMode = "Normal";
public:
// Getter for AC Mode
std::string getACMode() {
return _acMode;
}
// Getter for Temperature
double getTemp() {
return _temp;
}
// Setter for Temperature
void setTemp(double temp) {
_temp = temp;
// Update the AC mode based on temperature
if (_temp == _baseTemp) {
_acMode = "Normal";
} else if (_temp > _baseTemp) {
_acMode = "Heating";
} else {
_acMode = "Cooling";
}
}
};
Explanation:
- Private Members:
We have three private variables:_temp
for current temperature,_baseTemp
for the default temperature (25°C), and_acMode
for storing the air conditioner’s current mode. - Getters:
getACMode()
andgetTemp()
allow external code to access the values of_acMode
and_temp
without changing them. - Setter:
setTemp()
allows us to change the temperature. When the temperature is set, we automatically update the air conditioning mode:
- If the temperature is higher than the base, the mode switches to “Cooling”.
- If lower, it switches to “Heating”.
- If equal to the base, it stays in “Normal” mode.
Using the ACControl
Class:
Now, let’s see how we can use these Getters and Setters in action:
int main() {
ACControl AC1;
// Initial state
std::cout << "Room Temperature: " << AC1.getTemp() << std::endl;
std::cout << "AC Mode: " << AC1.getACMode() << std::endl;
// Change the temperature
AC1.setTemp(30.0);
std::cout << "Room Temperature: " << AC1.getTemp() << std::endl;
std::cout << "AC Mode: " << AC1.getACMode() << std::endl;
return 0;
}
Room Temperature: 25
AC Mode: Normal
Room Temperature: 30
AC Mode: Cooling
Conclusion:
Getters and Setters are simple yet powerful tools for encapsulating your class’s internal state while still allowing controlled access and modification. By using Getters and Setters, you maintain control over how your object’s data is accessed and updated, ensuring data integrity and encapsulation in C++.