Advertisement
smatskevich

Seminar5

Mar 13th, 2023
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. uint Base = 10;
  5.  
  6. template <typename T>
  7. class SmartPointer {
  8.  public:
  9.   explicit SmartPointer(T* to_hold) : _p(to_hold) { std::cout << "Common sp ctor" << std::endl; }
  10.   ~SmartPointer() { delete _p; }
  11.  
  12.  private:
  13.   T* _p;
  14. };
  15.  
  16. template <>
  17. class SmartPointer<int> {
  18.  public:
  19.   explicit SmartPointer(int* to_hold) : _p(to_hold) { std::cout << "Spec sp ctor" << std::endl; }
  20.   ~SmartPointer() { std::cout << "ooo, int " << *_p << " is to be crashed."; delete _p; }
  21.  
  22.  private:
  23.   int* _p;
  24. };
  25.  
  26. int main1() {
  27.   SmartPointer<double> pd(new double(3.5));
  28.   SmartPointer<int> pi(new int(44));
  29.   return 0;
  30. }
  31.  
  32. template <int D>
  33. class BigInteger {
  34.  public:
  35.   BigInteger() = default;
  36.   BigInteger(BigInteger<D - 1> low, uint up) : _low(low), _up(up) {}
  37.  
  38.  
  39.   std::string ToString() const {
  40.     std::string answer;
  41.     answer.append(std::to_string(_up));
  42.     answer.append(_low.ToString());
  43.     return answer;
  44.   }
  45.  
  46.   BigInteger<D> operator-() const {
  47.     return {-_low, _up};
  48.   }
  49.  
  50. //  template <int G>
  51. //  BigInteger<D> operator+(const BigInteger<G>& right) const;
  52.  
  53.  
  54.  private:
  55.   BigInteger<D - 1> _low;
  56.   uint _up = 0;
  57. };
  58.  
  59. template <>
  60. class BigInteger<1> {
  61.  public:
  62.   BigInteger() = default;
  63.   BigInteger(uint digit, bool is_positive) : _digit(digit), _is_positive(is_positive) {}
  64.  
  65.   std::string ToString() const { return std::to_string(_digit); }
  66.  
  67.   BigInteger<1> operator-() const {
  68.     return {_digit, !_is_positive};
  69.   }
  70.  
  71.  private:
  72.   uint _digit = 0;
  73.   bool _is_positive = true;
  74. };
  75.  
  76. /*class BigIntegerWithSign {
  77.  private:
  78.   BigInteger<D> bi;
  79.   bool sign;
  80. };
  81. */
  82. int main() {
  83.   BigInteger<5> bi1;
  84. //  BigInteger<5> bi2;
  85. //  bi1 + bi2;
  86.   BigInteger<5> bi2(-bi1);
  87.  
  88.   std::cout << bi2.ToString() << std::endl;
  89.   return 0;
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement