Monday, 6 February 2017

Calculator using Class and object


#include<iostream>
using namespace
std;
class calculator {
public:
 calculator(int a, int b) {
  first = a;
  second = b;
 }
 int  add() {
  return first + second;
 }
 int sub() {
  return first - second;
 }
 int mul()
 {
  return first * second;
 }
 int div()
 {
  return first / second;
   
 }
private:
 int first;
 int second;
};

void main()
{

 int f_n;
 int s_n;
 char choice;
 cout << "Enter the first number " << endl;
 cin >> f_n;
 cout << "Enter the operation " << endl;
 cin >> choice;
 cout << "Enter the second number " << endl;
 cin >> s_n;
 calculator obj1(f_n, s_n);
 switch (choice)
 {
 case '+':
  cout << obj1.add() << endl;
  break;
 case '-':
  cout << obj1.sub() << endl;
  break;
 case '*':
  cout << obj1.mul() << endl;
  break;
 case '/':
  cout << obj1.div() << endl;
  break;
 }
 system("pause");
}

No comments:

Post a Comment