Solution:NIIT/GNIIT Sonugiri0032@gmail.com

Wednesday, January 06, 2016

Getter and Setter in C++

Getter and Setter in C++


Structure of the Problem Requirements 

Getter and setter are used when we define our own data type which also known as abstract data types. In abstract data types when we declare some functions or variables as private which are not accessible from out side of that class then these function are used to access them . Remember through this we just get the value of that variables and can't change them.

Source Code

#include<iostream>
#include <cstdlib>
using namespace std;
class LEP 
{
 string name;
   int  roll_no;
   public:
   void set_name (string);
   void set_roll_no(int);
   string get_name();
   int get_roll_no();
   void display();
};
void LEP::set_name(string n)
{
 name = n;
}
void LEP::set_roll_no(int r)
{
 roll_no = r;
}
string LEP::get_name()
{
 return name;
}
int LEP::get_roll_no()
{
 return roll_no;
}
void LEP::display ()
{
 cout<<" The Name of the Student is : "<<get_name() ;
 cout<<endl;
 cout<<" The Roll # the Student is :" <<get_roll_no();
 cout<<endl;
}
int main ()
{
 LEP L;
 string name;
 int roll;
 cout<<" Enter Your Name :";
 cin>>name;
 cout<<" Enter Your Roll # :";
 cin>>roll;
 L.set_name(name);
 L.set_roll_no(roll);
 L.display();
 }
Share:

0 comments:

GNIITSOLUTION GNIIT SOLUTION. Powered by Blogger.

Translate

Blog Archive

Unordered List