fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Vector3D;
  5.  
  6. class Vector2D {
  7. public:
  8. Vector2D(float x, float y) : x(x), y(y) { }
  9.  
  10. void print() { cout << "(" << x << ", " << y << ")" << endl; }
  11.  
  12. float getX() { return x; }
  13. float getY() { return y; }
  14.  
  15. operator Vector3D();
  16.  
  17. private:
  18. float x;
  19. float y;
  20. };
  21.  
  22. class Vector3D {
  23. public:
  24. Vector3D(float x, float y, float z) : x(x), y(y), z(z) { }
  25.  
  26. void print() { cout << "(" << x << ", " << y << ", " << z << ")" << std::endl; }
  27.  
  28. operator Vector2D() {
  29. return Vector2D(x,y);
  30. }
  31.  
  32. float getX() { return x; }
  33. float getY() { return y; }
  34. float getZ() { return z; }
  35.  
  36. private:
  37. float x;
  38. float y;
  39. float z;
  40. };
  41.  
  42. Vector2D::operator Vector3D()
  43. {
  44. return Vector3D(x, y, 0.0);
  45. }
  46.  
  47. int main()
  48. {
  49. Vector2D v2(1, 2);
  50. Vector3D v3(v2);
  51. v3.print();
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
(1, 2, 0)