#include class vec2d { public: double x; double y; vec2d() : x(0), y(0) { std::cout << "default constructor." << std::endl; }; vec2d(double a, double b) : x(a), y(b) { std::cout << "constructor with a=" << a << " b=" << b << std::endl; }; vec2d(const vec2d & v) : x(v.x), y(v.y){ std::cout << "copy constructor with v.x=" << v.x << " v.y=" << v.y << std::endl; }; vec2d & operator = (/* *this is the default first operand */ const vec2d & v) { x = v.x; y = v.y; std::cout << "assignment operator with v.x=" << v.x << " v.y=" << v.y << std::endl; return *this; }; vec2d operator - (const vec2d & v2) { return vec2d(x - v2.x, y - v2.y); } operator int () { std::cout << "operator int." << std::endl; return 2; } }; std::ostream & operator << (std::ostream & output, const vec2d & v) { output << "(" << v.x << "," << v.y << ")"; return output; } vec2d operator - (const vec2d & v) { return vec2d(-v.x, -v.y); } vec2d operator - (const vec2d & v1, const vec2d & v2) { return vec2d(v1.x-v2.x, v1.y-v2.y); } int main() { vec2d v1; // invokes default constructor vec2d v2(10, 3.2); // invokes the second constructor vec2d v3 = v2; // invokes the copy constructor vec2d v4(v3); // also invokes the copy constructor v1 = -v4; std::cout << "v1 = " << v1 << std::endl; std::cout << "v4 = " << v4 << std::endl; double d = v1; std::cout << d << std::endl; }; bool orthogonal(vec2d u, vec2d v) { // u and v are also copy-constructed return (u.x*v.x + u.y*v.y == 0); }