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:
++ , -- , + , - , / , * , >> , <<
#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;
}
Comments
Leave a comment