/*Orion Lawlor's Simple C++ Examples, olawlor@acm.org http://charm.cs.uiuc.edu/users/olawlor/ref/examples/cpp/overload_addition.cpp Shows how to overload a class foo's addition operator (+). */ #include class foo { private: double val; public: foo(double x):val(x) {} foo operator+(const foo &other) { std::cout<<"Called foo's operator+() with " <val<<","<val+other.val); } }; int main() { foo x(1.2),y(2.3),z(10.1); foo sum=x+y; foo sum2=x+y+z; return 0; } /*<@> <@> ******** Program output: ******** <@> Called foo's operator+() with 1.2,2.3 <@> Called foo's operator+() with 1.2,2.3 <@> Called foo's operator+() with 3.5,10.1 <@> */