Created
March 13, 2021 15:01
-
-
Save grayed/1ddb2884d10c7b499889252e7817c994 to your computer and use it in GitHub Desktop.
C++ class task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Number { | |
public: | |
virtual void print(std::ostream& os) const = 0; | |
virtual Number* sum(const Number& other) const = 0; | |
}; | |
std::ostream& operator <<(std::ostream& os, const Number& n) { n.print(os); return os; } | |
class NaturalNumber : public Number | |
{ | |
int n; | |
public: | |
NaturalNumber (int n_) : n(n_) {} | |
int getValue() const { return n; } | |
virtual void print(std::ostream& os) const { std::cerr << "NaturalNumber::print()"; os << n; } | |
virtual Number* sum(const Number& other) const; | |
}; | |
class Signed { | |
public: | |
virtual void switchSign() = 0; | |
}; | |
class IntegerNumber : public Number, public Signed | |
{ | |
int n; | |
public: | |
IntegerNumber(int n_) : n(n_) {} | |
int getValue() const { return n; } | |
virtual void print(std::ostream& os) const { std::cerr << "IntegerNumber::print()"; os << n; } | |
virtual void switchSign() { n = -n; } | |
virtual Number* sum(const Number& other) const; | |
}; | |
class RealNumber : public Number, public Signed | |
{ | |
double n; | |
public: | |
RealNumber (double n_) : n(n_) {} | |
double getValue() const { return n; } | |
virtual void print(std::ostream& os) const { std::cerr << "RealNumber::print()"; os << n; } | |
virtual void switchSign() { n = -n; } | |
virtual Number* sum(const Number& other) const; | |
}; | |
Number* IntegerNumber::sum(const Number& other) const { | |
const NaturalNumber* nn = dynamic_cast<const NaturalNumber*>(&other); | |
if (nn != NULL) | |
return new IntegerNumber(n + nn->getValue()); | |
const IntegerNumber* in = dynamic_cast<const IntegerNumber*>(&other); | |
if (in != NULL) | |
return new IntegerNumber(n + in->getValue()); | |
const RealNumber* rn = dynamic_cast<const RealNumber*>(&other); | |
if (in != NULL) | |
return new RealNumber(n + in->getValue()); | |
} | |
Number* RealNumber::sum(const Number& other) const { | |
const NaturalNumber* nn = dynamic_cast<const NaturalNumber*>(&other); | |
if (nn != NULL) | |
return new RealNumber(n + nn->getValue()); | |
const IntegerNumber* in = dynamic_cast<const IntegerNumber*>(&other); | |
if (in != NULL) | |
return new RealNumber(n + in->getValue()); | |
const RealNumber* rn = dynamic_cast<const RealNumber*>(&other); | |
if (in != NULL) | |
return new RealNumber(n + in->getValue()); | |
} | |
IntegerNumber operator +(const IntegerNumber &n1, const NaturalNumber &n2) | |
{ | |
return IntegerNumber(n1.getValue() + n2.getValue()); | |
} | |
IntegerNumber operator +(const NaturalNumber&n1, const IntegerNumber&n2) | |
{ | |
return IntegerNumber(n1.getValue() + n2.getValue()); | |
} | |
// ... | |
int main() | |
{ | |
NaturalNumber nn(12); RealNumber rn(4.2); Signed* s = &rn; s->switchSign(); | |
std::cout << "Natural: " << nn << std::endl; | |
std::cout << "Real: " << rn << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment