20120930

Getter and Setter Methods in C++

How to Use Getter and Setter Methods in C++
In C++ Getter and Setter Methods are used to set the value of data members/variable and return the value of the data member. Its a very common technique in object oriented programming for setting and returning the values of variable.
Example Getter and Setter Methods in C++
#include<iostream>
using namespace std;

class getsetDemo
{
   private:
       int age;
   public:
       void getData()
          {
            return age;
          }
      void  setData(int a)
             {
               age=a;
              }
int main()
{
getsetDemo gs1(10) ;
cout<<"Age is"<<setData();
return 0;
}


Getter and Setter Methods makes the program more readable for the programmers who uses a previously written program. For changing the value of a variable he does not need to search for the variables. Simply search the setter method and edit the variable value which much convenient.
Previous                             Home                               Next

No comments:

Post a Comment