Advertisement
avr39ripe

PV913This

May 20th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Test
  4. {
  5.     static int common;
  6.     int x;
  7.     int* ptr;
  8.     static const int size{42};
  9.  
  10. public:
  11.    
  12.     Test() : Test(0) { std::cout << "Default constructor for Test\n"; };
  13.     Test(int pX, int pSize = 433) : x{ pX }, ptr{ new int[10] }{++common; std::cout << "Test constructed for " << this << '\n' << "This is #" << common << " instance of Test\n"; }
  14.     ~Test() { delete[] ptr; std::cout << "Test destructed for " << this << '\n' << "This was #" << common-- << " instance of Test\n"; };
  15.     void setX(int x) { this->x = x; };
  16.     int getX() { return x; };
  17.     int getSize() { return size; };
  18.     bool isEqual(Test* const obj) { return (obj == this); };
  19.     Test& add(int num) { x += num; return *this; };
  20.     Test& sub(int num) { x -= num; return *this; };
  21.     Test& mul(int num) { x *= num; return *this; };
  22.     Test& div(int num) { x /= num; return *this; };
  23.     static int getObjCount(){ return common; };
  24.     static void sayName() { std::cout << "I'm a Test class!\n"; };
  25. };
  26.  
  27. //int getXTest(Test* const obj) { return obj->x; };
  28.  
  29. int Test::common{0};
  30.  
  31. int cnt()
  32. {
  33.     static int cntNum{ 55 };
  34.     return ++cntNum;
  35. }
  36.  
  37.  
  38. //Test* const this; //1
  39. //const Test* this; //2
  40.  
  41. int main()
  42. {
  43.  
  44.     //for (int i{ 0 }; i < 5; ++i)
  45.     //{
  46.     //  std::cout << cnt() << '\n';
  47.     //};
  48.  
  49.     //std::cout << Test::common << '\n';
  50.     //Test::common = 888;
  51.     //std::cout << Test::common << '\n';
  52.     //std::cout << "There are " << Test::getObjCount() << " objects of Test class \n";
  53.     //std::cout << "\n\n";
  54.     Test anotherTest{ 555, 55 };
  55.     //std::cout << "\n\n";
  56.     //std::cout << "There are " << anotherTest.getObjCount() << " objects of Test class \n";
  57.    
  58.     Test newTest{ 555 };
  59.     //std::cout << "\n\n";
  60.     //std::cout << "There are " << myTest.getObjCount() << " objects of Test class \n";
  61.  
  62.     //std::cout << "We got mytest up and running!\n";
  63.     //myTest.setX(42);
  64.     //std::cout << myTest.getX() << '\n';
  65.     //std::cout << myTest.getX() << '\n';
  66.     //std::cout << anotherTest.getX() << '\n';
  67.  
  68.     //std::cout << myTest.isEqual(&myTest) << '\n'; //1
  69.     //std::cout << myTest.isEqual(&anotherTest) << '\n'; //0
  70.     //std::cout << myTest.isEqual(&newTest) << '\n'; //0
  71.  
  72.     //std::cout << anotherTest.isEqual(&anotherTest) << '\n'; //1
  73.     //std::cout << anotherTest.isEqual(&myTest) << '\n'; //0
  74.     //std::cout << anotherTest.isEqual(&newTest) << '\n'; //0
  75.  
  76.     //std::cout << myTest.getSize() << '\n';
  77.     //std::cout << anotherTest.getSize() << '\n';
  78.  
  79.     Test myTest{ 555 };
  80.  
  81.     std::cout << myTest.getX() << '\n';
  82.     myTest.sub(555).add(10).add(2).mul(2); // x == 24;
  83.     //myTest.sub(555);  // x == 0;
  84.     myTest.add(10); // x == 10;
  85.     //myTest.add(2); // x == 12;
  86.     //myTest.mul(2); // x == 24;
  87.     std::cout << myTest.getX() << '\n'; //24
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement