I'm creating a program that simply show the effect of operator overloading, and I'm having a problem with *=. Code:
Code:
#include <iostream>
using namespace std;
class Overload {
int i;
public:
Overload() {
i = 0;
}
Overload(int x) {
i = x;
}
Overload(const Overload &ol) {
i = ol.i;
}
int getI() {
return i;
}
Overload operator+(int x) {
Overload temp;
temp.i = i + x;
return temp;
}
Overload operator+(Overload ol) {
Overload temp;
temp.i = i + ol.i;
return temp;
}
Overload operator+=(int x) {
i += x;
return *this;
}
Overload operator+=(Overload ol) {
i += ol.i;
return *this;
}
Overload operator++(int x) {
i++;
return *this;
}
Overload operator-(int x) {
Overload temp;
temp.i = i - x;
return temp;
}
Overload operator-(Overload ol) {
Overload temp;
temp.i = i - ol.i;
return temp;
}
Overload operator-=(int x) {
i -= x;
return *this;
}
Overload operator-=(Overload ol) {
i -= ol.i;
return *this;
}
Overload operator--(int x) {
i--;
return *this;
}
Overload operator*(int x) {
Overload temp;
temp.i = i * x;
return temp;
}
Overload operator*(Overload ol) {
Overload temp;
temp.i = i * ol.i;
return temp;
}
Overload operator*=(int x) {
i *= x;
return *this;
}
Overload operator*=(Overload ol) {
i *= ol.i;
return *this;
}
};
int main() {
int y = 59, y2 = 100;
Overload ob1, ob2(10), ob3(ob2);
cout << "ob1's i: " << ob1.getI() << endl;
cout << "ob2's i: " << ob2.getI() << endl;
cout << "ob3's i: " << ob3.getI() << endl << endl;
ob1 = ob1 + 76;
ob2 = ob2 + y;
ob3 = ob3 + ob2;
cout << "ob1's i after adding (operator+) 76: " << ob1.getI() << endl;
cout << "ob2's i after adding (operator+) y, with the value of " << y << ": " << ob2.getI() << endl;
cout << "ob3's i after adding (operator+) ob2: " << ob3.getI() << endl << endl;
ob1 += 25;
ob2 += y2;
ob3 += ob2;
cout << "ob1's i after adding (operator+=) 25: " << ob1.getI() << endl;
cout << "ob2's i after adding (operator+=) y2, with the value of " << y2 << ": " << ob2.getI() << endl;
cout << "ob3's i after adding (operator+=) ob2: " << ob3.getI() << endl << endl;
ob1++;
ob2++;
ob3++;
cout << "ob1's i after adding (operator++) 1: " << ob1.getI() << endl;
cout << "ob2's i after adding (operator++) 1: " << ob2.getI() << endl;
cout << "ob3's i after adding (operator++) 1: " << ob3.getI() << endl << endl;
ob1 = ob1 - 76;
ob2 = ob2 - y;
ob3 = ob3 - ob2;
cout << "ob1's i after subtracting (operator-) 76: " << ob1.getI() << endl;
cout << "ob2's i after subtracting (operator-) y, with the value of " << y << ": " << ob2.getI() << endl;
cout << "ob3's i after subtracting (operator-) ob2: " << ob3.getI() << endl << endl;
ob1 -= 25;
ob2 -= y2;
ob3 -= ob2;
cout << "ob1's i after subtracting (operator-=) 25: " << ob1.getI() << endl;
cout << "ob2's i after subtracting (operator-=) y2, with the value of " << y2 << ": " << ob2.getI() << endl;
cout << "ob3's i after subtracting (operator-=) ob2: " << ob3.getI() << endl << endl;
ob1--;
ob2--;
ob3--;
cout << "ob1's i after subtracting (operator--) 1: " << ob1.getI() << endl;
cout << "ob2's i after subtracting (operator--) 1: " << ob2.getI() << endl;
cout << "ob3's i after subtracting (operator--) 1: " << ob3.getI() << endl << endl;
ob1 = ob1 * 76;
ob2 = ob2 * y;
ob3 = ob3 * ob2;
cout << "ob1's i after multiplying (operator*) by 76: " << ob1.getI() << endl;
cout << "ob2's i after multiplying (operator*) by y, with the value of " << y << ": " << ob2.getI() << endl;
cout << "ob3's i after multiplying (operator*) by ob2: " << ob3.getI() << endl << endl;
ob1 *= 25;
ob2 *= y2;
ob3 *= ob2;
cout << "ob1's i after multiplying (operator*=) by 25: " << ob1.getI() << endl;
cout << "ob2's i after multiplying (operator*=) by y2, with the value of " << y2 << ": " << ob2.getI() << endl;
cout << "ob3's i after multiplying (operator*=) by ob2: " << ob3.getI() << endl << endl;
return 0;
}
I can't place my finger on the problem. ob3 is supposed to have its current value (i) multiplied by the i field in ob2. ob2 is 59000, and the current value of ob3 is 74340. Multiplied together, it equals 4,386,060,000. After operator*= is called, ob3.i has 91,092,704 in it. At first I thought it was this:
ob3 *= ob2;
Which would mean the problem would be the overloaded *= with an Overload argument. But i replaced it with
ob3 = ob3 * ob2;
And it yielded the same result. Can anyone see the problem, its driving me crazy. Thanks !