#include // with the following line, we are defining "Scalar" to be a synonym of basic type float typedef float Scalar; // a class for vectors class Vector { public: Scalar x, y, z; // the Cartesian coords /* constructors */ Vector(): x(0), y(0), z(0){} Vector(Scalar a, Scalar b, Scalar c): x(a), y(b), z(c) {} /* declaration of methods */ Vector sum(const Vector &other) const; // another way to write down the vector sum Vector operator+(const Vector &other) const; Scalar dot(const Vector &other) const; }; /* implementation of methods */ Vector Vector::sum(const Vector &other) const { return Vector(x+other.x, y+other.y, z+other.z); } Vector Vector::operator+(const Vector &other) const { return Vector(x+other.x, y+other.y, z+other.z); } Scalar Vector::dot(const Vector &other) const { return (x*other.x) + (y*other.y) + (z*other.z); } /* global functions on vectors and scalars */ bool isEqual(Scalar v1, Scalar v2) { return (v1-v2)*(v1-v2) < 0.001; } bool isEqual(Vector v1, Vector v2) { return isEqual(v1.x, v2.x) && isEqual(v1.y, v2.y) && isEqual(v1.z, v2.z); } void unitTestSum() { /* this test that vector-sum is indeed commutative (sanity check) */ Vector a(5,-6,9.5), b(2,3,5); assert(isEqual( a+b , b+a )); } int main() { // just some examples of usage of class Vectors Vector v, w(2,3,5); v.x = 2; v = v.sum(w); v = v.operator+(w); v = v + w; Scalar k = v.dot(v); unitTestSum(); return 0; }