// // class for 3D vectors of ints, plus test main // // input: none. // output: results of using some of class's functions. // #include #include // has EXIT_SUCCESS using namespace std; // // class for 3D vectors of ints // class Vector { public: // ---- constructors ---- // post: x = init_x, y = init_y, z = init_z Vector(int init_x = 0, int init_y = 0, int init_z = 0) { x = init_x; y = init_y; z = init_z; } // ---- member functions ---- // functions to read private variables // post: return value is x coordinate int get_x(void) const { return x; } // post: return value is y coordinate int get_y(void) const { return y; } // post: return value is z coordinate int get_z(void) const { return z; } private: int x, y, z; }; // ---- nonmember functions ---- // overloaded "+" // post: return value is sum of v1 and v2 Vector operator+ (const Vector & v1, const Vector & v2); // overloaded "-" // post: return value is difference of v1 and v2 Vector operator- (const Vector & v1, const Vector & v2); // overloaded "<<" // post: v printed to out, in the form (x, y, z) ostream & operator<< (ostream & out, const Vector & v); // // ---- main program to test a few things ---- // int main(void) { Vector v1(1, 2, 3); // note syntax for declaring/initializing Vector v2(2, 4, 6); cout << "v1 = " << v1 << endl; cout << "v2 = " << v2 << endl; Vector vtemp = v1 + v2; cout << "v1 + v2 = " << vtemp << endl; cout << "v1 - v2 = " << v1 - v2 << endl; // note compute-and-print return EXIT_SUCCESS; } // ---- function implementations (see declarations for comments) ---- Vector operator+ (const Vector & v1, const Vector & v2) { return Vector(v1.get_x() + v2.get_x(), v1.get_y() + v2.get_y(), v1.get_z() + v2.get_z()); } Vector operator- (const Vector & v1, const Vector & v2) { return Vector(v1.get_x() - v2.get_x(), v1.get_y() - v2.get_y(), v1.get_z() - v2.get_z()); } ostream & operator<< (ostream & out, const Vector & v) { out << "(" << v.get_x() << ", " << v.get_y() << ", " << v.get_z() << ")"; return out; }