Search This Blog

Wednesday, 26 February 2014

setter and getter function in C++

/*
Setter means to set the values for the private member in a class.
Getter means to access them. 
*/


#include<iostream.h>
#include<conio.h>

class Student
{
  int roll;
  char *ch;

  public:
       void SetRoll(int roll)
       {
         this->roll=roll;
       }
       void SetName(char name[])
       {
         ch=name;
       }
       int GetRoll()
       {
         return roll;
       }
       char* GetName()
       {
         return ch;
       }
};

void main()
{
  Student s;
  s.SetRoll(21);
  s.SetName("sanjay kumar singh");
  cout<<"Roll no is :"<<s.GetRoll()<<" and Name is :"<<s.GetName();
  getch();
}

Output


No comments:

Popular Posts