\CPP\Classes
smpl_vector.cpp
// Programmer: Josh Santomieri // http://www.santsys.com // Example of simple 2 point vector class with operator overides #include#include template class CVect { public: CVect(); CVect(T x, T y, T z); ~CVect(); void SetVect(T x, T y, T z); void SetVectX(T x); void SetVectY(T y); void SetVectZ(T z); void PrintVect(); double Distance(CVect V1); CVect operator+(CVect &V1); CVect operator-(CVect &V1); CVect operator*(CVect &V1); CVect operator/(CVect &V1); int operator==(CVect &V1); int operator!=(CVect &V1); private: T x; T y; T z; }; template CVect ::CVect() { } template CVect ::CVect(T x, T y, T z) { CVect::x = x; CVect::y = y; CVect::z = z; } template CVect ::~CVect() { } template void CVect ::SetVect(T x = 0, T y = 0, T z = 0) { CVect::x = x; CVect::y = y; CVect::z = z; } template void CVect ::PrintVect() { cout << "(" << CVect::x << ", " << CVect::y << ", " << CVect::z << ")" < void CVect ::SetVectX(T x) { CVect::x = x; } template void CVect ::SetVectY(T y) { CVect::y = y; } template void CVect ::SetVectZ(T z) { CVect::z = z; } template double CVect ::Distance(CVect V1) { double dDist = 0.0; dDist = (sqrt((pow((CVect::x - V1.x), 2) + pow((CVect::y - V1.y), 2)) + pow((CVect::z - V1.z), 2))); return dDist; } template CVect CVect ::operator+(CVect &V1) { CVect temp; temp.x = CVect::x + V1.x; temp.y = CVect::y + V1.y; temp.z = CVect::z + V1.z; return temp; } template CVect CVect ::operator-(CVect &V1) { CVect temp; temp.x = CVect::x - V1.x; temp.y = CVect::y - V1.y; temp.z = CVect::z - V1.z; return temp; } template CVect CVect ::operator*(CVect &V1) { CVect temp; temp.x = CVect::x * V1.x; temp.y = CVect::y * V1.y; temp.z = CVect::z * V1.z; return temp; } template CVect CVect ::operator/(CVect &V1) { CVect temp; if((V1.x == 0) || (V1.y == 0) || (V1.z == 0)) return NULL; else { temp.x = CVect::x / V1.x; temp.y = CVect::y / V1.y; temp.z = CVect::z / V1.z; } return temp; } template int CVect ::operator==(CVect &V1) { return ((CVect::x == V1.x) && (CVect::y == V1.y) && (CVect::z == V1.z)); } template int CVect ::operator!=(CVect &V1) { return ((CVect::x != V1.x) || (CVect::y != V1.y) || (CVect::z == V1.z)); } int main(int argc, char **argv) { CVect cv(1,2,3); CVect cv1(3,4,5); CVect cvd(1.1, 1.2, 1.3); CVect cvd1(1.1, 2.2, 3.3); cvd.PrintVect(); cv.PrintVect(); cv1.PrintVect(); cv = cv + cv1; cv.PrintVect(); cvd = cvd + cvd1; cvd.PrintVect(); cout << "distance (cv, cv1): " << cv.Distance(cv1) << endl; return 0; }