Answer to Question #327480 in C++ for Vamsi

Question #327480

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.

1
Expert's answer
2022-04-11T16:50:19-0400
#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;
}

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