Advertisement
Miseryk

C++ Constructor delegation

Sep 8th, 2021
1,659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class A
  4. {
  5.     int x, y, z;
  6.    
  7. public:
  8.     A()
  9.     {
  10.         this->x = 0;
  11.         this->y = 0;
  12.         this->z = 0;
  13.     }
  14.    
  15.     A(int xx) : A()
  16.     {
  17.         this->x = xx;
  18.     }
  19.    
  20.     A(int xx, int yy) : A(xx)
  21.     {
  22.         this->y = yy;
  23.     }
  24.    
  25.     A(int xx, int yy, int zz) : A(xx, yy)
  26.     {
  27.         this->z = zz;
  28.     }
  29.    
  30.     void show()
  31.     {
  32.         std::cout << "x: " << this->x << std::endl;
  33.         std::cout << "y: " << this->y << std::endl;
  34.         std::cout << "z: " << this->z << std::endl << std::endl;
  35.     }
  36. };
  37.  
  38. int main()
  39. {
  40.     A test;
  41.     test.show();
  42.    
  43.     A test2(1);
  44.     test2.show();
  45.    
  46.     A test3(1, 2);
  47.     test3.show();
  48.    
  49.     A test4(1, 2, 3);
  50.     test4.show();
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement