Getters and Setters in C++
In the C++ programming language, getter and setter methods are built-in functions of user-defined class members that help C++ users control the private data type member variables of a declared class and provide access to class data members. Getter and setter methods are commonly used in object-oriented programming (OOP) to hide private and protected data type members declared in a user-defined class and to apply encapsulation concepts.

Getter methods (also known as class data member accessors) in a C++ program are such class data extraction methods. Getter methods are used to extract the value of a private class data member from a program.
Setter methods (also known as class mutators) in a C++ program are such class data information update concepts. In C++, setters are used to modify or set the value of private class data members declared in a class.
Why use getters and setters methods in C++ programming?
Encapsulation concept in getters and setters methods.
In C++ OOP programming, C++ users hide the internal data of a user-defined class (e.g., hiding class data member parameters) to prevent access or modification without user permission. By using getters and setters methods in a C++ class, C++ users provide controlled user access to this class data.
Data validation concept in getters and setters methods.
Setter methods allow C++ users to apply validation logic to data types declared in an existing class before modifying them. For example, a setter method in a user-defined class might ensure that an employee’s age is always entered or displayed as a positive number.
Maintainability concept in getters and setters methods.
Getters and setters methods allow C++ users to modify the internal representation of class data without directly impacting external program source code. For example, you can modify or update the internal data type of a user-defined class variable, but as long as the getter and setter parameters in the class are consistent, this has no major impact on the remaining program.
Security concept in getters and setters methods.
Getters and setters methods provide C++ users with more secure control over multiple concepts for accessing or modifying existing class data parameter members. For example, logging within a class, controlling data member access, or calculating data values before returning them to a member within a class.
Basic example of getters and setters method in a C++ program.
So, let’s take a look at how getters and setters work in C++ programming. Here’s a simple example to help you understand this better.
#include <iostream>
#include <string>
class Employee {
private:
std::string emp_name;
int emp_age;
public:
// Here we set the getter method for the ’emp_name’ class field
std::string getEmpName() const {
return emp_name;
}
// Here we set the setter method for the ’emp_name’ class field
void setEmpName(const std::string& updateName) {
emp_name = updateName;
}
// here we set Getter method for ’emp_age’
int getEmpAge() const {
return emp_age;
}
// here we set Setter method for ’emp_age’
void setEmpAge(int updateAge) {
if (updateAge >= 0) { // here we Add a simple validation to prevent negative emp_age
emp_age = updateAge;
} else {
std::cout << “Negative employee emp_age, Age must be a non-negative value.” << std::endl;
}
}
};
int main() {
Employee employee;
// here we Use a setter methods to set employee class values
employee.setEmpName(“Siddhi Deora”);
employee.setEmpAge(19);
// Here we use getter methods to access employee class values
std::cout << “Employee Name is – ” << employee.getEmpName() << std::endl;
std::cout << “Employee Age is – ” << employee.getEmpAge() << std::endl;
// Here we attempt to set an invalid negative number employee age
employee.setEmpAge(-1);
return 0;
}
Explanation of a getters and setters method in C++.
Private class data members.
- Here in the Employee class, the emp_name and emp_age class members are private data fields. This means they cannot be directly accessed from outside the class, and this is a secure class data member extraction method.
Getters Class method.
- The getter method in the Employee class, getEmpName(), provides the value of the private emp_name member.
- Whereas getEmpAge() provides the value of the private emp_age member.
Setters Class method.
- In the setter method, setEmpName(const std::string& updateName) sets the emp_name value.
- Whereas setEmpAge(int updateAge) helps set the emp_age value, we have added an easy employee age validation feature to ensure that emp_age is not displayed as a negative number.

