avr39ripe

cppconstructorsBasics

Jul 8th, 2021 (edited)
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. class Point
  4. {
  5. public:
  6.     int x;
  7.     int y;
  8. public:
  9.     //Point() : x{42}, y{33}
  10.     Point() : Point{42,33}
  11.     {
  12.         std::cout << "Default constructor for Point started!\n";
  13.         //x = 42;
  14.         //y = 33;
  15.     }
  16.  
  17.     Point(int pX, int pY) : x{ pX }, y{pY}
  18.     {
  19.         std::cout << "Parametrized X Y constructor for Point started!\n";
  20.         /*x = pX;
  21.         y = pY;*/
  22.     }
  23.  
  24.     /*Point(int pX, bool all = false) : x{ pX }, y{all ? pX : 0}*/
  25.     Point(int pX, bool all = false) : Point{ pX, all ? pX : 0 }
  26.     {
  27.         std::cout << "Parametrized pX constructor for Point started!\n";
  28.         //x = pX;
  29.         //if (all) { y = pX; }
  30.         //else { y = 0; }
  31.     }
  32.  
  33.     void setX(int pX) { x = pX; }
  34.     void setY(int pY) { y = pY; }
  35.  
  36.     int getX() { return x; }
  37.     int getY() { return y; }
  38. };
  39.  
  40.  
  41. class Circle
  42. {
  43.     Point centre;
  44.     int R;
  45. public:
  46.     Circle() : centre{ 10,10 }, R{42}
  47.     {
  48.         std::cout << "Default constructor for Circle started!\n";
  49.         /*centre.x = 10;
  50.         centre.y = 10;*/
  51.         //R = 42;
  52.     }
  53.  
  54.     void info()
  55.     {
  56.         std::cout << "Circle info: R = " << R << ' ' << " center x = " << centre.x << " y = " << centre.y << '\n';
  57.     }
  58. };
  59.  
  60. int main()
  61. {
  62.     Circle c1;
  63.  
  64.     c1.info();
  65.  
  66.     Point p1; //p1.x p1.y
  67.     p1.setX(1);
  68.     Point p2{22, true}; //p2.x p2.y
  69.     //p2.setX(2);
  70.     Point p3{33,555};
  71.     //p3.setX(3);
  72.  
  73.     //p1.setX(3);
  74.     //p1.setY(5);
  75.  
  76.     std::cout << p1.getX() << ' ' << p1.getY() << '\n';
  77.     std::cout << p2.getX() << ' ' << p2.getY() << '\n';
  78.     std::cout << p3.getX() << ' ' << p3.getY() << '\n';
  79.  
  80.  
  81.     return 0;
  82. }
Add Comment
Please, Sign In to add comment