avr39ripe

cppPoinToPoint3DAndBoolConversion

Jun 24th, 2021 (edited)
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Point3D;
  4.  
  5. class Point
  6. {
  7.     int x;
  8.     int y;
  9. public:
  10.     Point() = default;
  11.     Point(int pX, int pY) : x{ pX }, y{ pY } {}
  12.     Point& setX(int pX) { x = pX; return *this; }
  13.     Point& setY(int pY) { y = pY; return *this; }
  14.  
  15.     void showPoint() const
  16.     {
  17.         std::cout << '(' << x << ',' << y << ')';
  18.     }
  19.  
  20.     explicit operator bool() const { return x and y; }
  21.     explicit operator Point3D() const;
  22. };
  23.  
  24. class Point3D
  25. {
  26.     int x;
  27.     int y;
  28.     int z;
  29. public:
  30.     Point3D() = default;
  31.     Point3D(int pX, int pY, int pZ) : x{ pX }, y{ pY }, z{ pZ } {}
  32.     Point3D& setX(int pX) { x = pX; return *this; }
  33.     Point3D& setY(int pY) { y = pY; return *this; }
  34.     Point3D& setZ(int pZ) { z = pZ; return *this; }
  35.  
  36.     void showPoint() const
  37.     {
  38.         std::cout << '(' << x << ',' << y << ',' << z << ')';
  39.     }
  40.  
  41.     explicit operator bool() const { return x and y and z; }
  42.     explicit operator Point() const;
  43. };
  44.  
  45. Point::operator Point3D() const { return { x,y,0 }; }
  46. Point3D::operator Point() const { return { x, y }; }
  47.  
  48. int main()
  49. {
  50.     const int pointsCount{ 3 };
  51.     Point points[pointsCount]{ {0,0}, {28,29}, {0,26} };
  52.  
  53.     bool isZero{};
  54.  
  55.     for (auto point{ points }, pointsEnd{ points + pointsCount }; point != pointsEnd; ++point)
  56.     {
  57.         //isZero = (bool)(*point);
  58.         if (!*point)
  59.         {
  60.             point->showPoint();
  61.             std::cout << '\n';
  62.         }
  63.         else
  64.         {
  65.             std::cout << "Zero Point detected!\n";
  66.         }
  67.     }
  68.  
  69.     return 0;
  70. }
Add Comment
Please, Sign In to add comment