// // 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 // // define name for type of vector elements to ease transition // to template class typedef int T; class Vector { public: // ---- constructors ---- // post: x = init_x, y = init_y, z = init_z Vector(T init_x = 0, T init_y = 0, T 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 T get_x(void) const { return x; } // post: return value is y coordinate T get_y(void) const { return y; } // post: return value is z coordinate T get_z(void) const { return z; } private: T 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); // dot product // postcondition: return value is dot product of v1, v2 T dotProduct(const Vector & v1, const Vector & v2); // // ---- 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 cout << "dot product of v1 and v2 = " << dotProduct(v1, v2) << endl; return EXIT_SUCCESS; } // ---- function implementations (see declarations for comments) ---- // ---- "helper" functions ---- // post: return value is x + y T add(T x, T y) { return x + y; } // post: return value is x - y T subtract(T x, T y) { return x - y; } // post: return value is x * y T multiply(T x, T y) { return x * y; } // post: return value is produced by combining corresponding // elements of v1, v2 using oper Vector pairwiseOperator(const Vector & v1, const Vector & v2, T oper(const T, const T)) { return Vector(oper(v1.get_x(), v2.get_x()), oper(v1.get_y(), v2.get_y()), oper(v1.get_z(), v2.get_z())); } // post: return value is produced by "reducing" elements of // v1 using oper T reduce(const Vector & v1, T oper(const T, const T)) { return (oper( oper(v1.get_x(), v1.get_y()), v1.get_z())); } // ---- functions declared earlier ---- Vector operator+ (const Vector & v1, const Vector & v2) { return pairwiseOperator(v1, v2, add); } Vector operator- (const Vector & v1, const Vector & v2) { return pairwiseOperator(v1, v2, subtract); } ostream & operator<< (ostream & out, const Vector & v) { out << "(" << v.get_x() << ", " << v.get_y() << ", " << v.get_z() << ")"; return out; } T dotProduct(const Vector & v1, const Vector & v2) { return (reduce (pairwiseOperator(v1, v2, multiply), add)); }