Setter and Getter functions in C++

Submitted by: 
Language: 

Setter and Getter functions in C++

In this part you will learn:
1. How to make an object of class in C++
2. What are Setters
3. What are getters
4. To use setters and getters
5. Basic C++ syntax

How to make an object of class in C++?
Whenever we want to make an object of the class we first define its class and then in the main of the program we write name of the class followed by the name of the object we want to make.
e.g to make an object we write the code as
class anyclass{
//data members and functions
};
int main
{ anyclass myobj;
}
In the above code we have created an object of the class ‘anyclass’ whose name is ‘myobj’

What are Setters?
Specific functions which are used to set or assign the values to the data members of objects of some class are called setters. There could be any name of the function which set the value of data member but usually it is called setter. The code below in this tutorial will have setter which will make things more clearer.

What are Getters?
Getters are the functions that retrieve the values of data members of some object so that these values can be used in the main of the program. As in C++ each function can return only one variable so here we need to have the getters equal to number of the data members so that each getter returns the value of one data member. The code below in this tutorial will have getters which will make things more clearer.

Program:

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;

These are the basic header files which are required for C++ programming.

  1. class myFirstClass
  2. {
  3. private:
  4. int num1;
  5. int num2;
  6. public:
  7. void setValue(int a,int b) //setter
  8. {
  9. num1=a;
  10. num2=b;
  11. }

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.
In the next part 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).

  1. int getValue1()
  2. {
  3. return num1;
  4. }
  5. int getValue2()
  6. {
  7. return num2;
  8. }
  9.  
  10. int addFirstNum(myFirstClass obj)
  11. {
  12. int num1Total;
  13. num1Total=num1+obj.num1;
  14. return num1Total;
  15. }
  16. int addSecondNum(myFirstClass obj)
  17. {
  18. int num2Total;
  19. num2Total=num2+obj.num2;
  20. return num2Total;
  21. }
  22. };

In this part of code functions int getValue1() and int getValue2() are the getters, as already told before the getters are equal to the number of data members so here we have two getters and two data members as well. getValue1() returns the value of first data member ‘num1’ while the getValue2() function returns the value of second data member ‘num2 ’ and it must be noted here that as the both getters return the integer type values so there return type is int.
In the last two function of the class ‘int addFirstNum(myfirstclass obj)’ and
‘int addSecondNum(myfirstclass obj)’we basically are adding the corresponding data members of the two objects. Here this must be kept in mind that the object through which the function is called(the object after which dot operator ‘.’is used) has the name of attributes same as defined in class while the object which is delivered to the function as argument uses dot operator to call its data member as shown in above example. The result of addition is stored in another integer type variable which is then returned.

  1. void main()
  2. {
  3. myFirstClass obj1;
  4. myFirstClass obj2;
  5. int a,b,c,d;
  6. cout<<"------------------Setterss---------------------------\n";
  7. cout<<"Enter the first value for the first object:\n\n";
  8. cin>>a;
  9. cout<<"Enter the second value for the first object:\n\n";
  10. cin>>b;
  11. cout<<"Enter the first value for the second object:\n\n";
  12. cin>>c;
  13. cout<<"Enter the second value for the second object:\n\n";
  14. cin>>d;
  15.  
  16. obj1.setValue(a,b);
  17. obj2.setValue(c,d);
  18. cout<<"------------------Getterss---------------------------\n";
  19. cout<<"\n\nFirst value of first object obtained by getter:\n\n"<<obj1.getValue1();
  20. cout<<"\n\nSecond value of first object obtained by getter:\n\n"<<obj1.getValue2();
  21. cout<<"\n\nFirst value of second object obtained by getter:\n\n"<<obj2.getValue1();
  22. cout<<"\n\nSecond value of second object obtained by getter:\n\n"<<obj2.getValue2();
  23. //now for addition:
  24. cout<<"----------------Addition Result-----------------------\n";
  25. cout<<"Result after addition of first values of the two objects is:"<<obj1.addFirstNum(obj2);
  26. cout<<"\n\nResult after addition of second values of the two objects is:"<<obj1.addSecondNum(obj2);
  27. getch();
  28. }

In the main of the function we first made two objects named ‘obj1’ and ‘obj2’ of the class ‘myFirstClass’ and in the next lines by using setValue function we are assigning the values to the data members of each object which are input by the user. Then we are displaying the result on screen by using the getValue(also called getter) functions.
In the last part we are displaying the result after addition of the corresponding data members of the two objects for which we are using functions addFirstNumand addSecondNum. Here the object through which function is called always comes before the dot operator and after the dot operator the name of function is written, here the function is passed the obj2 so that their data members be added.

Output:
Output


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.