How to Write Getter and Setter Methods in C++? Last Updated : 27 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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, we will learn how to create a class with getter and setter methods in C++. Getter and Setter Methods for a Class in C++To create a class with getter and setter methods in C++, we define the class with private data members and public methods to get (read) and set (write) these data members. Example: Consider a class Student with private data member age, and public getter and setter methods for these data members. // Getter methods DataType getAge() const { return age; }// Setter methods void setAge(DataType newValue) { age = newValue; }C++ Program to Create a Class with Getter and Setter Methods C++ // C++ Program to show how to Create a Class with Getter and // Setter Methods #include <iostream> using namespace std; class GFG { private: int length; int width; public: // Constructor GFG(int len, int wid) { length = len; width = wid; } // Getter methods int getLength() const { return length; } int getWidth() const { return width; } // Setter methods void setLength(int len) { length = len; } void setWidth(int wid) { width = wid; } }; // Driver Code int main() { // Create an object of Rectangle class GFG rect(5, 3); cout << "Length: " << rect.getLength() << endl; cout << "Width: " << rect.getWidth() << endl; // Use setter methods to modify member variables rect.setLength(7); rect.setWidth(4); // Display updated values cout << "Updated Length: " << rect.getLength() << endl; cout << "Updated Width: " << rect.getWidth() << endl; return 0; } OutputLength: 5 Width: 3 Updated Length: 7 Updated Width: 4 Comment More infoAdvertise with us Next Article How to Write Getter and Setter Methods in C++? V vikas2gqb5 Follow Improve Article Tags : C++ Programs C++ cpp-class CPP-OOPs CPP Examples +1 More Practice Tags : CPP Similar Reads C++ Getters and Setters 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++. Gett 2 min read How to Set, Clear, and Toggle a Single Bit in C++? In bit manipulation, setting a bit, clearing a bit, and toggling a single bit are basic operations. We can easily carry out these operations using the bitwise operators in C++. In this article, we will see how to set, clear, and toggle operations on the Kth bit. Example Input: N = 15, K = 0Output: S 3 min read How to Access Vector Methods from Pointer to Vector in C++? In C++, we can create a pointer that points to the object of vector type. In this article, we will learn how to access member functions of std::vector from a pointer to the vector in C++. Example Input:vec = {1, 2, 3, 4}vector<int>* ptr = vecOutput:ptr->at(2): 3Accessing std::vector Methods 2 min read How to Create a Class with Private and Public Members in C++? In C++, the classes are blueprints for creating objects with specific properties and methods that provide a feature of access specifiers to the user through which they can control the access of the data members present in a class. In this article, we will learn how to create a class with private and 3 min read How to Add an Element to a Set in C++? In C++ STL, a set is a container that stores unique elements in a sorted order. In this article, we will learn how to add an element to a set in C++ STL. Example: Input: mySet = {1, 2, 4, 5, 8} Element to add: 3 Output: mySet = {1, 2, 3, 4, 5, 8}Add an Element to a Set in C++To add a specific elemen 2 min read Like