Getters and Setters in C++
Hey, guys today we are going to learn about Getters and Setters in C++. Getters are Setters are mostly used with classes. They are used to access and set the values of private members of classes respectively. Before discussing getters and setters in detail lets first discuss classes and access specifiers.
Abstraction using classes in C++
Abstraction is hiding the irrelevant information and providing only the essential information of the data to the outside world(user). This is achieved by using classes. Classes have different access specifiers which tell about the accessibility of different data members. There are 3 access specifiers:
public: These data members are accessible both inside and outside the class.
private: Private data members are not accessible outside the class and are not inheritable. They are available only inside the body of the class.
protected: Protected members are not accessible outside the class but are inheritable in nature i.e. they are accessible to the derived/base class.
Thus we know that private and protected members are inaccessible outside a class but sometimes we are required to give access to private and protected data members. This can be done with the help of getters and setters.
Getter (Accessor Function)
Getter is commonly known as accessor function. Getter is a special function that allows us to access the data members of the class(even private and protected members). Its syntax is
datatype function_name() { return datamember; }
Let’s understand with an example. Let’s consider a class student with Name, Rollno, Percentage as private data members and getRollNO() and constructor student() as public function as shown below:
#include <iostream> using namespace std; class student { private: string Name; int Rollno; float Percentage; public: student(string name,int rno,float perc)//A constructor { Name=name; Rollno=rno; Percentage=perc; } int getRollNO() { return Rollno; } }; int main() { student a("User",21,98.78); cout<<a.getRollNO();//public members can be accessed using '.' return 0; }
Output:
21
In the above example, an object ‘a’ of student class is created. Its Rollno(Private member) is retrieved using getter ‘getRollNo’. Please note that it is not essential to use’get’ in the accessor function(getter) name. Thus any name can be used.
Setter (Mutator Function)
Setter is commonly known as the mutator function. It is a special function that allows us to set(assign) values to a data member of an object of a class. It can also change the previous value assigned to a data member. However, the data members of an object of the class are usually assigned values by creating a special function called the constructor. Usually, setters are used along with conditions which are checked before assigning the value of data member. Its syntax is
void setter_name(datatype value) { datamember=value; }
Let us understand with the help of the given example
#include <iostream> using namespace std; class student { private: string Name; int Rollno; float Percentage; public: void setRollNO(int rno)// Setter for RollNO { if(rno>0 && rno<=60) Rollno=rno; else cout<<"Rollno should be between 1 and 60\n"; } //constructor to initialise Name and Percentage student(string name,float perc ) { Name=name; Percentage=perc; } int getdetails() { cout<<Name<<endl; cout<<Rollno<<endl; cout<<Percentage<<endl; } }; int main() { student a("User",98.78); a.setRollNO(90); a.setRollNO(21); a.getdetails(); return 0; }
Output:
Rollno should be between 1 and 60 User 21 98.78
In this example, a class student is created with Name, Rollno, Percentage as private data members and setRollNo() and constructor student() and getdetails() as public members. After creating an object, we tried to assign the value of Rollno as 90 then as 21. Also like getter, it is not essential to use ‘set’ in the name of setters. Thus any name an be given to setter
In conclusion, we can say that getter is used to access data members(even protected and private members) of an object of a class. Similarly, the setter is used to assign values to data members(even protected and private members) of an object of a class.
Also, refer :
Leave a Reply