Advertisement
MBrendecke

Vector3D - CPP

Jun 29th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. // Vector3D.h
  2. #ifndef __Vector3D
  3. #define __Vector3D
  4.  
  5. class Vector3D
  6. {
  7. private:
  8.     double x, y, z;
  9.  
  10. public:
  11.     Vector3D(double x = 0, double y = 0, double z = 0)
  12.     {
  13.         this->x = x;
  14.         this->y = y;
  15.         this->z = z;
  16.     }
  17.  
  18.     double getX() { return x; }
  19.     void setX(double value) { x = value; }
  20.  
  21.     double getY() { return y; }
  22.     void setY(double value) { y = value; }
  23.  
  24.     double getZ() { return z; }
  25.     void setZ(double value) { z = value; }
  26.  
  27.     friend Vector3D operator+(const Vector3D &v1, const Vector3D &v2);
  28.  
  29.     Vector3D& operator+=(const Vector3D &v) {
  30.         this->x += v.x;
  31.         this->y += v.y;
  32.         this->z += v.z;
  33.  
  34.         return *this;
  35.     }
  36. };
  37.  
  38. inline Vector3D operator+(const Vector3D &v1, const Vector3D &v2) {
  39.     return Vector3D(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
  40. }
  41.  
  42. #endif // !__Vector3D
  43.  
  44. // Vector3D.cpp
  45. #include <stdlib.h>
  46. #include "Vector3D.h"
  47.  
  48. using namespace std;
  49.  
  50. int main() {
  51.     Vector3D vec1(1, 2, 4);
  52.     Vector3D vec2(3, 5, 7);
  53.  
  54.     Vector3D vec3 = vec1 + vec2;
  55.  
  56.     system("pause");
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement