// Vector3D.h
#ifndef __Vector3D
#define __Vector3D
class Vector3D
{
private:
double x, y, z;
public:
Vector3D(double x = 0, double y = 0, double z = 0)
{
this->x = x;
this->y = y;
this->z = z;
}
double getX() { return x; }
void setX(double value) { x = value; }
double getY() { return y; }
void setY(double value) { y = value; }
double getZ() { return z; }
void setZ(double value) { z = value; }
friend Vector3D operator+(const Vector3D &v1, const Vector3D &v2);
Vector3D& operator+=(const Vector3D &v) {
this->x += v.x;
this->y += v.y;
this->z += v.z;
return *this;
}
};
inline Vector3D operator+(const Vector3D &v1, const Vector3D &v2) {
return Vector3D(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}
#endif // !__Vector3D
// Vector3D.cpp
#include <stdlib.h>
#include "Vector3D.h"
using namespace std;
int main() {
Vector3D vec1(1, 2, 4);
Vector3D vec2(3, 5, 7);
Vector3D vec3 = vec1 + vec2;
system("pause");
return 0;
}
Comments