Answer to Question #176940 in C++ for aliza

Question #176940

Write a C++ program to create a calculator for the number by creating a class of numbers overloading all operators in it.

Operators to use in the program are: 

++ , -- , + , - , / , * , >> , << 

1
Expert's answer
2021-03-30T14:36:12-0400
#include <iostream>
#include <math.h>


using namespace std;


class Calculator {

  double a, b;

public:

  void Get()
  {
    cout << "Enter First Number: ";
    cin >> a;
    cout << "Enter Second Number: ";
    cin >> b;
  }

  double Sum()
  {
    return a + b;
  }

  double Diff()
  {
    return a - b;
  }

  double Mul()
  {
    return a * b;
  }

  double Div()
  {
    if (b == 0)
    {
      cout << "ERROR: !!!!!Divison By Zero!!!!!" << endl;
      return INFINITY;
    }
    else
      return a / b;
  }

};

int main()
{
  int choice;
  Calculator calculator;

  cout << "Enter 1 to sum 2 Numbers"
    << "\nEnter 2 to difference 2 Numbers"
    << "\nEnter 3 to multiply 2 Numbers"
    << "\nEnter 4 to divide 2 Numbers"
    << "\nEnter 0 To Exit"
    << "\n";
  do {
    cout << "\nChoose the operation: ";
    cin >> choice;
    switch (choice) {
    case 1:
      calculator.Get();
      cout << "Result: " << calculator.Sum() << endl;
      break;
    case 2:
      calculator.Get();
      cout << "Result: " << calculator.Diff() << endl;
      break;
    case 3:
      calculator.Get();
      cout << "Result: " << calculator.Mul() << endl;
      break;
    case 4:
      calculator.Get();
      cout << "Result: " << calculator.Div() << endl;
      break;
    }
  } while (choice >= 1 && choice <= 4);

  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS