For some reason the value of divide()
still prints as an int
even though I declared it as a double
.
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
class Calculator{
private:
int a = 0;
int b = 0;
public:
Calculator(int n1, int n2) : a(n1), b(n2) {}
Calculator() = default;
int getN1() const{
return a;
}
int getN2() const{
return b;
}
int add() const{
return a + b;
}
int subtract() const{
return a - b;
}
int multiply() const{
return a * b;
}
double divide() const{
return a / b;
}
void showDetails() const{
cout << " " << endl;
cout << "Computed:" << endl;
cout << "Sum = " << add() << endl;
cout << "Difference = " << subtract() << endl;
cout << "Product = " << multiply() << endl;
cout << "Quotient = " << setprecision(2) << fixed << divide() << endl;
}
};
int main() {
int n1;
int n2;
cout << "Enter Num1: ";
cin >> n1;
cout << "Enter Num2: ";
cin >> n2;
Calculator x(n1, n2);
x.showDetails();
return 0;
}
It worked when I declared the initial values as double
but I was tasked to assign the initial values as int
.