SHARE
TWEET

23

a guest Jan 21st, 2020 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <algorithm>
  2. #include <iostream>
  3.  
  4. class cNumber {
  5. protected:
  6.     int _value;
  7. public:
  8.     cNumber(int v = 0) :
  9.         _value{ v } { }
  10.  
  11.     cNumber operator+(const cNumber& other) {
  12.         cNumber number(other.value() + this->value());
  13.         return number;
  14.     }
  15.  
  16.     friend std::ostream& operator<<(std::ostream& o, cNumber& n) {
  17.         o << n.value();
  18.         return o;
  19.     }
  20.  
  21.     virtual int value() const {
  22.         return _value;
  23.     }
  24.  
  25. };
  26.  
  27. class cAbsNumber : public cNumber {
  28. public:
  29.     cAbsNumber(int i):
  30.         cNumber(i) { }
  31.  
  32.     int value() const override {
  33.         if (cNumber::value() < 0)
  34.             return -cNumber::value();
  35.         return cNumber::value();
  36.     }
  37. };
  38.  
  39. int main() {
  40.     cNumber a(-123);
  41.     cNumber b(7);
  42.  
  43.     cNumber c = (a + b);
  44.  
  45.     cAbsNumber d(-100);
  46.     cAbsNumber e(123);
  47.  
  48.     std::cout << c << "\n";
  49.     std::cout << d << "\n";
  50.     std::cout << e << "\n";
  51. }#include <algorithm>
  52. #include <iostream>
  53.  
  54. class cNumber {
  55. protected:
  56.     int _value;
  57. public:
  58.     cNumber(int v = 0) :
  59.         _value{ v } { }
  60.  
  61.     cNumber operator+(const cNumber& other) {
  62.         cNumber number(other.value() + this->value());
  63.         return number;
  64.     }
  65.  
  66.     friend std::ostream& operator<<(std::ostream& o, cNumber& n) {
  67.         o << n.value();
  68.         return o;
  69.     }
  70.  
  71.     virtual int value() const {
  72.         return _value;
  73.     }
  74.  
  75. };
  76.  
  77. class cAbsNumber : public cNumber {
  78. public:
  79.     cAbsNumber(int i):
  80.         cNumber(i) { }
  81.  
  82.     int value() const override {
  83.         if (cNumber::value() < 0)
  84.             return -cNumber::value();
  85.         return cNumber::value();
  86.     }
  87. };
  88.  
  89. int main() {
  90.     cNumber a(-123);
  91.     cNumber b(7);
  92.  
  93.     cNumber c = (a + b);
  94.  
  95.     cAbsNumber d(-100);
  96.     cAbsNumber e(123);
  97.  
  98.     std::cout << c << "\n";
  99.     std::cout << d << "\n";
  100.     std::cout << e << "\n";
  101. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top