Setters And Getters

Setters and Getters

In this part you will learn: 1. What are setters and getters? 2. Why we need setters and getters? 3. Program using setters and getters 4. Showing output What are Setters and Getters? As the name suggests, setters are functions used to set values whereas getters are used to get values. Setters and getters functions are usually public data members of the class. Setters and getters are used for private or protected member variables. As we know that me cannot directly access the private or protected member variables of a class, so we use indirect access method using setters and getters. For example we want to access a member variable namely “age”, we will use the function getage() and setage(int a) to get the value stored in member variable “age” and to set the value of “age” which is passed as parameter in variable a respectively. We will discuss a complete example of setters and getters at the end of this topic. Why we need Setters and Getters? While writing huge programs, programmers restrict themselves from directly changing or accessing the values of member variables of an object because it creates a lot of mess finding and removing the error in that case. As a solution we use setters and getters functions which are part of that class and can be used outside the class providing indirect access to private member variables of the class. Program using setters and getters Basic Step:
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
These two are the most common and basic lines of a c++ program. The first one iostream is used to grant us most of the basic functions of C++ like Input and Output functions. The second one conio.h means console input output, the black screen we see is the console. Using namespace std; provides us an ease of using all the library functions without specifying that this function belongs to standard library. Without including it, we have to write ‘std’ with all the built-in functions. Class
  1. class info
  2. {
  3. public:
  4. int id;
  5. protected:
  6. int contact;
  7. private:
  8. int age;
  9.  
  10. public:
  11. int getcontact(){
  12. return contact;
  13. }
  14. void setcontact(int c){
  15. contact = c;
  16. }
  17. int getage(){
  18. return age;
  19. }
  20. void setage(int a){
  21. age = a;
  22. }
  23. };
The above given code is the definition of class “info” which has 3 member variables id, contact and age under access specifiers public, protected and private respectively. Since member variables contact and age are protected and private respectively, so they cannot be accessed directly. For this purpose we have made their setters and getters under public access. In the definition of setters and getters, we have simply returned the value stored in it in get function and passed the value as parameter we want to store in the member variable and assigned the parameter to it. Note: Since these setters and getters are part of class definition so they have direct access to even the private and protected members of the class. Setter functions have usually “void” as their return type as they don’t return anything. Now we will write the main of the program to see its working.
  1. <strong>Main</strong>
  2. int main()
  3. {
  4. info muzzi; //initializing a variable muzzi of type info
  5.  
  6. muzzi.id = 4156; //allowed because it is public member variable
  7. muzzi.setcontact(1234); //indirectly accessing protected member "contact"
  8. muzzi.setage(20); //indirectly accessing private member "age"
  9.  
  10. cout << muzzi.id<<endl; //allowed because it is public member function
  11. cout << muzzi.getcontact()<<endl; //indirectly getting the value stored in "contact"
  12. cout << muzzi.getage()<<endl; //indirectly getting the value stored in "age"
  13.  
  14. system("pause");
  15. }
The above code explains the usage of setters and getters. After assigning value to member id of the object muzzi, we see that we have used “muzzi.setcontact(1234);” instead of “muzzi.contact=1234” because contact is a protected member of class info and it cannot be accessed directly same is the case while setting age. After that, while printing the value on console, id gets directly accessed because it is a public member. But in case of contact and age we have used their respective getters to get the value stored in them. Output picture viewer

Add new comment