Answer to Question #178725 in C++ for Aditya

Question #178725

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


1
Expert's answer
2021-04-06T15:31:59-0400
#include <iostream>
using namespace std;

class Number {
private:
    double value;

public:
    Number(double value);
    Number();
    ~Number();

    double getValue() const;
    Number operator+(const Number& other) const;
    Number operator-(const Number& other) const;
    Number operator*(const Number& other) const;
    Number operator/(const Number& other) const;
};

std::ostream& operator<<(std::ostream& out, const Number& number);


Number::Number(double value) {
    this->value = value;
}

Number::Number() {
    this->value = 0;
}

Number::~Number() {}

double Number::getValue() const {
    return this->value;
}

Number Number::operator+(const Number& other) const {
    Number diff;

    diff.value = this->value - other.getValue();

    return diff;
}

Number Number::operator-(const Number& other) const {
    Number div;

    div.value = this->value / other.getValue();

    return div;
}

Number Number::operator*(const Number& other) const {
    Number sum;

    sum.value = this->value + other.getValue();

    return sum;
}

Number Number::operator/(const Number& other) const {
    Number prod;

    prod.value = this->value * other.getValue();

    return prod;
}

std::ostream& operator<<(std::ostream& out, const Number& other) {
    return out << other.getValue();
}

int main() {
    Number ten(10);
    Number two(2);

    cout << ten + two << endl;
    cout << ten - two << endl;
    cout << ten * two << endl;
    cout << ten / two << 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