daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Jun 24th, 2018 48 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4.  
  5. class House {
  6.  public:
  7.   House &operator=(const House &house) = default;
  8.   House(const House &house) = default;
  9.   House(): id_habitants_(nullptr), num_habitants_() {}
  10.   explicit House(size_t num_habitants) {
  11.     if (num_habitants > 0) {
  12.       num_habitants_ = num_habitants;
  13.       id_habitants_ = new int[num_habitants_];
  14.       if (id_habitants_ != nullptr) {
  15.         for (size_t id = 0; id < num_habitants_; ++id) {
  16.           id_habitants_[id] = 1;
  17.         }
  18.       }
  19.     }
  20.   }
  21.   void Print() {
  22.     if (id_habitants_ != nullptr) {
  23.       for (size_t id = 0; id < num_habitants_; ++id) {
  24.         std::cout << id_habitants_[id] << ' ';
  25.       }
  26.       std::cout << std::endl;
  27.     } else {
  28.       std::cout << "<empty>" << std::endl;
  29.     }
  30.   }
  31.   ~House() {
  32.     if (id_habitants_ != nullptr) {
  33.       delete [] id_habitants_;
  34.     }
  35.     num_habitants_ = 0;
  36.   }
  37.  private:
  38.   int *id_habitants_;
  39.   size_t num_habitants_;
  40. };
  41.  
  42. int main() {
  43.   std::cout << "Testing unique_ptr.n" << std::endl;
  44.  
  45.   std::cout << "Using a dumb House class..." << std::endl;
  46.   std::cout << "Creating House h1 with 3 habitants..." << std::endl;
  47.   House h1(3);
  48.   std::cout << "IDs of h1's 3 habitants:" << std::endl;
  49.   h1.Print();
  50.   std::cout << "Creating House h2 with 0 habitants..." << std::endl;
  51.   House h2;
  52.   std::cout << "IDs of h2's 0 habitants:" << std::endl;
  53.   h2.Print();
  54.   std::cout << "Default-assigning h1 to h2..." << std::endl;
  55.   h2 = h1;
  56.   std::cout << "IDs of h2's new 3 habitants:" << std::endl;
  57.   h2.Print();
  58.   std::cout << "Destroying h1..." << std::endl;
  59.   h1.~House();
  60.   std::cout << "IDs of h2's new 3 habitants:" << std::endl;
  61.   h2.Print();
  62. }
  63.    
  64. class SmartHouse {
  65.  public:
  66.   SmartHouse &operator=(const SmartHouse &shouse) = default;
  67.   SmartHouse(const SmartHouse &shouse) = default;
  68.   SmartHouse(): id_habitants_(nullptr), num_habitants_() {}
  69.  
  70.   explicit SmartHouse(size_t num_habitants) {
  71.     if (num_habitants > 0) {
  72.       num_habitants_ = num_habitants;
  73.       id_habitants_ = std::unique_ptr<int[]>(new int[num_habitants_]);
  74.       if (id_habitants_) {
  75.         for (size_t id = 0; id < num_habitants_; ++id) {
  76.           id_habitants_[id] = 1;
  77.         }
  78.       }
  79.     }
  80.   }
  81.   void Print() {
  82.     if (id_habitants_) {
  83.       for (size_t id = 0; id < num_habitants_; ++id) {
  84.         std::cout << id_habitants_[id] << ' ';
  85.       }
  86.       std::cout << std::endl;
  87.     } else {
  88.       std::cout << "<empty>" << std::endl;
  89.     }
  90.   }
  91.   ~SmartHouse() {
  92.     num_habitants_ = 0;
  93.   }
  94.  private:
  95.   std::unique_ptr<int[]> id_habitants_;
  96.   size_t num_habitants_;
  97. };
  98.    
  99. SmartHouse sh1(3);
  100. SmartHouse sh2;
  101. sh2 = sh1;
  102.    
  103. class SmartHouse {
  104.   SmartHouse &operator=(SmartHouse &&SmartHouse) = default;
  105. }
  106. ...
  107. SmartHouse sh1(3);
  108. SmartHouse sh2;
  109. sh2 = std::move(sh1);
  110. sh1.~SmartHouse();
  111. sh2.Print();
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