1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
#include <iostream> using namespace std; class test { private: int x; int y; public: test(); // constructors cant be private ~test(); test operator+(int); }; test::test() { cout << "constructing...." << endl; x = 0; y = 0; } test::~test() { cout << "Destructing in...." << endl; } test test::operator+(int p) { int newtest; newtest = x + p.x; newtest = y + p.y; return newtest; } int main () { test constructor; test t1,t2,t3; int a,b; cout << "enter numbers: "; cin >> a >> b; char chooseoperator; cout << "Choose a operator: "; cin >> chooseoperator; if(chooseoperator == '+') { t3 = t1.x + t2.y; cout << " t3" << endl; } return 0; }
123456789
test test::operator+(const test& rhs) { test newtest; newtest.x = x + rhs.x; newtest.y = y + rhs.y; return newtest; }