#include <iostream>
usingnamespace std;
class T
{
int one,two;
public:
T(int one = 2, int two = 1)
{
cout << one << "/" << two << endl;
}
intoperator+(T s)
{
int x = (one/two) + (s.one/s.two);
cout << x;
return x;
}
};
int main()
{
T one(7,5);
T two;
int i = one + two;
return 0;
}
The problem is you never initialize the 'one' and 'two' members of your T class.
This is hard to see because you gave 3 different things the same name, so I'm going to rename your vars for this example. Note that the below code is 100% identical to yours, apart from the changed names:
#include <iostream>
usingnamespace std;
class T
{
int one,two;
public:
T(int a = 2, int b = 1)
{
// note that you are printing parameters 'a' and 'b' here, and not your
// member vars 'one' and 'two'.
cout << a << "/" << b << endl;
// also note that you do not initialize 'one' and 'two' to anything... so
// those vars are filled with random garbage.
}
intoperator+(T s)
{
// note again, since 'one' and 'two' never were initialized, they have garbage.
// so here, 'x' is also going to be garbage
int x = (one/two) + (s.one/s.two);
cout << x;
return x;
}
};
int main()
{
T foo(7,5);
T bar;
int i = foo + bar;
return 0;
}