Getters and Setters In C++

Getters and setters are methods used in C++ classes to access and modify the private or protected attributes (data members) of objects. They are part of a technique called data encapsulation, which provides controlled access to the attributes, allowing you to maintain the integrity and security of the data.

Getters (Accessors)

A getter is a method that provides read-only access to an object's attributes. It allows external code to retrieve the values of private or protected attributes without directly accessing them.

class Student { private: std::string name; int age; public: // Getter methods std::string getName() { return name; } int getAge() { return age; } };

In this example, the getName and getAge methods are getters that allow external code to retrieve the name and age attributes of a Student object.

Setters (Mutators)

A setter is a method that provides a way to modify the values of an object's attributes. It allows controlled modification of private or protected attributes, often including validation logic.

class BankAccount { private: double balance; public: // Setter method void setBalance(double newBalance) { if (newBalance >= 0) { balance = newBalance; } } };

In this example, the setBalance method is a setter that allows external code to set the balance attribute of a BankAccount object, but it includes logic to ensure that the balance is non-negative.

Usage of Getters and Setters

Getters and setters are used to encapsulate access to class attributes, providing a controlled interface to external code while maintaining the data's integrity. They help enforce data validation and can be particularly useful when additional logic is needed during attribute access or modification.

int main() { Student student; student.getName(); // Access the name attribute using the getter student.getAge(); // Access the age attribute using the getter BankAccount account; account.setBalance(1000); // Modify the balance attribute using the setter }

Getters and setters are common in C++ classes to ensure that attributes are accessed and modified in a controlled and validated manner, enhancing data encapsulation and code maintainability.

Full Source
#include <iostream> #include <string> class Student { private: std::string name; int age; public: // Getter methods std::string getName() { return name; } int getAge() { return age; } }; class BankAccount { private: double balance; public: // Setter method void setBalance(double newBalance) { if (newBalance >= 0) { balance = newBalance; } } }; int main() { // Using the Student class Student student; student.getName(); // Access the name attribute using the getter student.getAge(); // Access the age attribute using the getter // Using the BankAccount class BankAccount account; account.setBalance(1000); // Modify the balance attribute using the setter return 0; }

Conclusion

Getters and setters are methods used in C++ classes to control access to private or protected attributes. Getters allow read-only access to these attributes, while setters enable controlled modification, often including validation logic, providing encapsulation and data integrity in object-oriented programming.