There is a secret organization and they
want to create a secret language for the
calculation to make the data secure.
Now the manager of the organization
asked his IT employee to make the
program which will function as follows:
a) * will subtract the numbers b) - will
divide the numbers c) / will add the
numbers d) + will multiply the numbers.
Use operator overloading.
#include <iostream>
using namespace std;
class Real {
double value;
public:
Real(double x=0) { value = x; }
void print(ostream& os) { os << value; }
Real operator*(Real x) {
return Real(value - x.value);
}
Real operator-(Real x) {
return Real(value / x.value);
}
Real operator/(Real x) {
return Real(value + x.value);
}
Real operator+(Real x) {
return Real(value * x.value);
}
};
ostream& operator<<(ostream& os, Real x) {
x.print(os);
return os;
}
int main() {
Real X(2), Y(5);
cout << X << " + " << Y << " = " << X + Y << endl;
cout << X << " - " << Y << " = " << X - Y << endl;
cout << X << " * " << Y << " = " << X * Y << endl;
cout << X << " / " << Y << " = " << X / Y << endl;
cout << "Have a nice day!" << endl;
return 0;
}
Comments
Leave a comment