Advertisement
bogolyubskiyalexey

Untitled

Feb 26th, 2021
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. class Type {
  6. public:
  7.     Type(int v) : value(v) {
  8.         std::cout << "Type(): " << value << std::endl;
  9.         pointer = new int(10);
  10.     }
  11.     Type(const std::string& a, const std::vector<int>& b)
  12.         : name(a)
  13.         , data(b)
  14.     {}
  15.     Type(const Type& other)
  16.         : name(other.name)
  17.         , data(other.data)
  18.     {}
  19.     Type(Type&& other)
  20.         : name(std::move(other.name))
  21.         , data(std::move(other.data))
  22.     {}
  23.     ~Type() {
  24.         std::cout << "~Type()" << value << std::endl;
  25.         delete pointer;
  26.     }
  27.  
  28.     Type& operator= (const Type& other) {
  29.         std::cout << "operator=(const Type&) " << value << std::endl;
  30.         name = other.name;
  31.         data = other.data;
  32.         return *this;
  33.     }
  34.     Type& operator= (Type&& other) {
  35.         std::cout << "operator=(Type&&): " << value << std::endl;
  36.         name = std::move(other.name);
  37.         data = std::move(other.data);
  38.         return *this;
  39.     }
  40.  
  41. public:
  42.     std::string name;
  43.     std::vector<int> data;
  44.     int value = -1;
  45.     int* pointer = nullptr;
  46. };
  47.  
  48. Type func() {
  49.     Type p(1);
  50.     p.name = "12wefsdfsdfsefesfwefwefefewsfsdfsdf";
  51.     p.data = {123,122,2222,2222};
  52.     return p;
  53. }
  54.  
  55. class Rational {
  56. public:
  57.     Rational(int p, int q) {
  58.         if (q == 0) {
  59.             throw std::invalid_argument("q must be not zero");
  60.         }
  61.         p_ = p;
  62.         q_ = q;
  63.     }
  64.  
  65.     bool set_q(int q) {
  66.         if (q == 0) {
  67.             return false;
  68.         }
  69.         q_ = q;
  70.     }
  71.  
  72. private:
  73.     int p_;
  74.     int q_;
  75. };
  76.  
  77. void f() {
  78.     try {
  79.         Rational a(10,0);
  80.         std::cout << "im here" << std::endl;
  81.     } catch(std::runtime_error& e) {
  82.         std::cout << "something wrong: " << e.what() << std::endl;
  83.     }
  84. }
  85.  
  86. int main() {
  87.     try {
  88.         f();
  89.     } catch(...) {
  90.         std::cout << "any exception" << std::endl;
  91.     }
  92.    
  93.     std::cout << "agter init" << std::endl;
  94.  
  95.  
  96.     int value;
  97.     std::cin >> value;
  98.     if (value < 1000) {
  99.         throw std::invalid_argument("input value less then 1000");
  100.     }
  101.     std::cout << "ok" << std::endl;
  102.  
  103.     Type* pt = nullptr;
  104.     std::cout << pt << std::endl;
  105.     std::cout << pt->name << std::endl;
  106.     std::cout << "done" << std::endl;
  107.     pt = new Type("sdfsdfsdf", {123, 443, 34333});
  108.  
  109.  
  110.     int *a = new int;
  111.     int* ptr = new int(10);
  112.  
  113.     std::cout << *a << std::endl;
  114.     delete a;
  115.     a = nullptr;
  116.     delete ptr;
  117.     delete pt;
  118.     delete a; // delete nullptr; valid
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement