Advertisement
Guest User

templates

a guest
Nov 24th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. template <class T>
  2. class Operand
  3. {
  4.     T value;
  5. public:
  6.     Operand() {};
  7.     Operand(T initValue)
  8.     {
  9.         value = initValue;
  10.     }
  11.  
  12.     Operand<T> operator-(Operand<T> const &b) const
  13.     {
  14.         Operand<T> integ(*this);
  15.         integ.setValue(integ.getValue() - b.getValue());
  16.         return integ;
  17.     }
  18.    
  19.     Operand<T> operator/(Operand<T> const &b) const
  20.     {
  21.         Operand<T> integ(*this);
  22.         integ.setValue(integ.getValue() / b.getValue());
  23.         return integ;
  24.     }
  25.  
  26.     Operand<T> operator+(Operand<T> const &b) const
  27.     {
  28.         Operand<T> integ(*this);
  29.         integ.setValue(integ.getValue() + b.getValue());
  30.         return integ;
  31.     }
  32.  
  33.     Operand<std::string> operator+(Operand<std::string> const &b) const
  34.     {
  35.         Operand<std::string> str(std::to_string(this->getValue()));
  36.         str.setValue(str.getValue() + b.getValue());
  37.         return str;
  38.     }
  39.  
  40.     Operand<T> operator*(Operand<T> const &b) const
  41.     {
  42.         Operand<T> integ(*this);
  43.         integ.setValue(integ.getValue() * b.getValue());
  44.         return integ;
  45.     }
  46.  
  47.     Operand<std::string> operator*(Operand<std::string> const &b) const
  48.     {
  49.         Operand<std::string> str(b);
  50.         int n = this->getValue();
  51.         std::string strPart = str.getValue();
  52.  
  53.         while (n--)
  54.         {
  55.             str.setValue(str.getValue() + strPart);
  56.         }
  57.  
  58.         return str;
  59.     }
  60.  
  61.     T getValue() const
  62.     {
  63.         return value;
  64.     }
  65.  
  66.     void setValue(T initValue)
  67.     {
  68.         value = initValue;
  69.     }
  70. };
  71.  
  72. template <>
  73. class Operand <std::string>
  74. {
  75.     std::string value;
  76. public:
  77.     Operand() {};
  78.     Operand(std::string initValue)
  79.     {
  80.         value = initValue;
  81.     };
  82.  
  83.     Operand <std::string> operator+(Operand<std::string> const &b) const
  84.     {
  85.         Operand<std::string> str(*this);
  86.         str.setValue(str.getValue() + b.getValue());
  87.         return str;
  88.     }
  89.  
  90.     Operand <std::string> operator+(Operand<int> const &b) const
  91.     {
  92.         Operand<std::string> str(*this);
  93.         str.setValue(str.getValue() + std::to_string(b.getValue()));
  94.         return str;
  95.     }
  96.  
  97.     Operand <std::string> operator*(Operand<int> const &b) const
  98.     {
  99.         Operand<std::string> str(*this);
  100.         int n = b.getValue();
  101.         std::string strPart = str.getValue();
  102.  
  103.         while (n--)
  104.         {
  105.             str.setValue(str.getValue() + strPart);
  106.         }
  107.  
  108.         return str;
  109.     }
  110.  
  111.     std::string getValue() const
  112.     {
  113.         return value;
  114.     }
  115.  
  116.     void setValue(std::string initValue)
  117.     {
  118.         value = initValue;
  119.     }
  120. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement