123456789101112131415161718192021
#include <iostream> class T{ public: int a; double d; T(){} T(int aa, double dd) : a(aa), d(dd) {} }; T operator+ (const T &a1, const T &a2){ return T(a1.a+a2.a , a1.d+a2.d); } int main () { T p(1,3.17); T q(7,0.16); T r= p+q; std::cout << r.a << " " << r.d << "\n"; return 0; }
1234567891011121314151617181920212223242526272829303132
#include <iostream> class T{ public: int a; double d; T(){} T(int aa, double dd) : a(aa), d(dd) {} }; T operator+ (const T &a1, const T &a2){ return T(a1.a+a2.a , a1.d+a2.d); } void operator+= (T &t1, const T &t2){ t1.a += t2.a; t1.d += t2.d; } int main () { T p(1,3.17); T q(7,0.16); T r= p+q; std::cout << r.a << " " << r.d << "\n"; p+=r; std::cout << p.a << " " << p.d << "\n"; return 0; }