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
#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;
}
Comments
Leave a comment