Setter and Getter Function - Learn in the easy way

Happy Learning...

ads

Post Top Ad

Setter and Getter Function

Share This
Program to set the first and second values of the two objects to display the values and add numbers.

#include<iostream>
#include<conio.h>
using namespace std;
class myFirstClass
{
private:
  int num1;
  int num2;
public:
  void setValue(int a,int b) //setter
  {
  num1=a;
  num2=b;
}
int getValue1()
{
  return num1;
}
int getValue2()
{  
  return num2;
}

int addFirstNum(myFirstClass obj)
{
  int num1Total;
  num1Total=num1+obj.num1;
  return num1Total;
}
int addSecondNum(myFirstClass obj)
{
  int num2Total;
  num2Total=num2+obj.num2;
  return num2Total;
}
};
int main()

  myFirstClass obj1;
  myFirstClass obj2;
  int a,b,c,d;
  cout<<"------------------Setterss---------------------------\n";
  cout<<"Enter the first value for the first object:\n\n";
  cin>>a;
  cout<<"Enter the second value for the first object:\n\n";
  cin>>b;
  cout<<"Enter the first value for the second object:\n\n";
  cin>>c;
  cout<<"Enter the second value for the second object:\n\n";
  cin>>d;

  obj1.setValue(a,b);
  obj2.setValue(c,d);
  cout<<"------------------Getterss---------------------------\n";
  cout<<"\n\nFirst value of first object obtained by getter:\n\n"<<obj1.getValue1();
  cout<<"\n\nSecond value of first object obtained by getter:\n\n"<<obj1.getValue2();
  cout<<"\n\nFirst value of second object obtained by getter:\n\n"<<obj2.getValue1();
  cout<<"\n\nSecond value of second object obtained by getter:\n\n"<<obj2.getValue2();
  //now for addition:
  cout<<"----------------Addition Result-----------------------\n";
  cout<<"Result after addition of first values of the two objects is:"<<obj1.addFirstNum(obj2);
  cout<<"\n\nResult after addition of second values of the two objects is:"<<obj1.addSecondNum(obj2);
  getch();

}

No comments:

Post a Comment

Tell me What can I do for you.....Comment, please

Post Bottom Ad

Pages