Getter and Setter in C++
Getter and Setter:
Getters and setters are used to efficiently protect your data. This is a technique used greatly when creating classes.
For each instance variable, a getter method returns its value while a setter method sets or updates its value.
The getter method returns the value of the attribute. The setter method takes a parameter and assigns it to the attribute.
The getters and setters are usually public and the variables are made.
Why use getter and setter:
Getters and setters makes your code more manageable and more future proof.
If you ever want to change the class, and you want to drop the field, you can emulate it and your application code is not broken.
You will be helpful in large projects. Consuming class would be more homogeneous.
☝ Getters and Setters are a common technique used in object oriented programming. It is good practice to use these Getters and Setters instead of changing the variables directly.
Note:
There could be any name of the function which set the value of data member but usually it is called setter.
Let's take a simple example:
- #include<iostream>
- #include<conio.h>
- using namespace std;
- class MyFirstClass
- {
- private:
- int num1;
- int num2;
- public:
- void setValue(int a,int b) //setter
- {
- num1=a;
- num2=b;
- }
In the above code we have made a class named ‘MyFirstClass’, its private data members are num1 and num 2 which have type of integers.
It also contains member functions which are under the public access specifier of the class. It should be noted here that data members are always kept private because an important thing kept in mind while working in object oriented programming is that it hides the implementation of the class by hiding the details(also called encapsulation of data) so otherwise class and structure will have no difference.
Then we have the setter function named ‘setValue’ of the class that takes in two input arguments and assign each argument to the data member of the class hence setting the values. It must be noted here that the setters never return a value so we the return type of setters is always void(meaning empty).
Now let's make getter:
- int getValue1()
- {
- return num1;
- }
- int getValue2()
- {
- return num2;
- }
- };
Here The getValue functions return values of type integers.
Now for our main function:
- int main()
- {
- MyFirstClass obj1;
- MyFirstClass obj2;
- int c,d,e,f;
- // Taking user input
- cout<<"Enter values of obj1" <<endl;
- // let the values are 3 and 5
- cin>>c>>d;
- //Calling setValue function to say value of obj1
- obj1.setValue(c,d);
- // Now for obj2
- cout <<"Enter values of obj2" <<endl;
- // let the values are 7 and 8
- cin>>e>>f;
- obj2.setValue(e,f);
- // Now printing their values
- cout <<obj1.getValue1()<<endl;
- cout <<obj1.getValue2()<<endl;
- cout <<obj2.getValue1()<<endl;
- cout <<obj2.getValue2()<<endl;
- return 0;
- }
Output:
Enter values of obj1:
Enter values of obj2:
3
5
7
8
Keep it up.
ReplyDelete☺️
gud😊
ReplyDelete