C++ Getters and Setters Last Updated : 27 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, getters and setters are part of data encapsulation used to protect our data, particularly when creating classes. These are public methods that are used to access and modify the private or protected members of a class. In this article, we will learn how to use getters and setters in C++. Getters in C++ In C++, getters, also known as accessors (as they access the value), are the public member functions that are used to fetch private member's values. In general, the getter starts with the word “get” followed by the variable name. Syntax to Define Getter dataType getVariableName() const { return variableName; }Setters in C++ Setters, also known as mutators (as they update the value), are also the public member functions that set the value of a private member variable. In general, the setter starts with the word “set” followed by the variable name. Syntax to Define Setter void setVariableName(dataType newValue) { variableName = newValue; }C++ Program to Demonstrate the Use of Getter and Setter.The below example demonstrates how we can use getters and setters in C++. C++ // C++ program to demonstrate the use of getters and setters #include <iostream> using namespace std; // Define Class Employee class Employee { private: // Define Private member salary int salary; public: // Setter void setSalary(int s) { salary = s; } // Getter int getSalary() { return salary; } }; int main() { // create object of class employee Employee myObj; // set the salary myObj.setSalary(10000); // get the salary and print it cout << "Salary is: " << myObj.getSalary(); return 0; } OutputSalary is: 10000Explanation: In the above program, we have accessed the private member "salary" of class employee using the setter setSalary() method and getter getSalary() method. Comment More infoAdvertise with us Next Article C++ Getters and Setters S sonijaiog3d Follow Improve Article Tags : C++ Programs C++ cpp-class CPP-OOPs CPP Examples +1 More Practice Tags : CPP Similar Reads How to Write Getter and Setter Methods in C++? In C++, classes provide a way to define custom data types that can encapsulate data and methods related to that data. Getter and setter methods are commonly used to access and modify private member variables of the class allowing for the controlled access and modification of data. In this article, w 2 min read Arrow Operator vs. Dot Operator in C++ In C++, we use the arrow operator (->) and the dot operator (.) to access members of classes, structures, and unions. Although they sound similar but they are used in different contexts and have distinct behaviours. In this article, we will learn the key differences between the arrow operator and 3 min read C++ Exercises - C++ Practice Set with Solutions Do you want to improve your command on C++ language? Explore our vast library of C++ exercise questions, which are specifically designed for beginners as well as for advanced programmers. We provide a large selection of coding exercises that cover every important topic, including classes, objects, a 15+ min read 7 Essential C++ Concepts for Every Developer C++ is a powerful, high-performance programming language used in a wide range of applications, from game development to systems programming. To master C++, it's crucial to understand some key concepts that form the foundation of this language. In this article, we will learn seven such essential C++ 7 min read Double Address Operator in C++ In C++, the double address operator (&&) is used to denote an rvalue reference, which is a type of reference that can bind to temporary objects. In this article, we will learn how to use the double address operator in C++. Double Reference Operator in C++ To understand the double address ope 3 min read Like