Having trouble with operator*=()

This is a discussion on Having trouble with operator*=() within the C++ Programming forums, part of the General Programming Boards category; I'm creating a program that simply show the effect of operator overloading, and I'm having a problem with *=. Code: ...

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    Having trouble with operator*=()

    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 !
    Do not make direct eye contact with me.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    4,386,060,000 is a pretty big number for a 4 byte integer.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    But changing int to long doesn't change a thing .

    EDIT: However double works . Thanks, but do you know why long wouldnt work though?
    Last edited by Lurker; 10-25-2003 at 07:53 PM.
    Do not make direct eye contact with me.

  4. #4
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    its called overflow. mr wizard is right in stating that 4.3 million is a lot for an integer.

    a normal (signed int) can store between approx. -2 million to +2 million.

    an unsigned int can only store from 0 to about 4.2 million.

    when you multiply 2 ints together and they go above the maximum int value, the int loops around and starts all over from its smallest value ( -2 mill for an int, 0 for an unsigned int ) and continues counting.

    overflow is easier to understand with a smaller variable ( and besides, i dont know the exact values for an int right off the top of my head ).

    a char is one byte and therefore can store 256 different values ( 0 - 255 for an unsigned, -127 to 128 for a signed ).

    if your signed char equaled 127 and you added 3 to it, your char would now equal -126. the computer adds 1 which makes it 128 ( the maximum for a signed char ) and then when it adds the second one it loops around and becomes -127. incrimenting it one last time makes -127 into -126.

    my guess is, this is most likely what is happening to your program.



    [edit]

    to answer your second question:

    the reason why a long doesnt work is that on newer compilers (i know for a fact its this way in MSVC++.NET) a long is also 4 bytes. in other words, longs are pretty useless.
    Last edited by ...; 10-25-2003 at 11:17 PM.
    I came up with a cool phrase to put down here, but i forgot it...

  5. #5
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Alrighty, thanks a lot - it was driving me crazy .
    Do not make direct eye contact with me.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    a normal (unsigned int) can store between approx. -2 million to +2 million.

    a signed int can only store from 0 to about 4.2 million.
    No biggie, but I think you've got that the wrong way round.

  7. #7
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by The Dog
    No biggie, but I think you've got that the wrong way round.
    I thought so.
    Do not make direct eye contact with me.

  8. #8
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    oops, my mistake

    yeah, thats what i meant
    I came up with a cool phrase to put down here, but i forgot it...

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    161
    Also, it's near 2 and 4 billion (for signed and unsigned, respectively)-- not million.

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by ...
    a normal (signed int) can store between approx. -2 million to +2 million.

    an unsigned int can only store from 0 to about 4.2 million.
    It's worth pointing out that the actual range is platform specific, it will depend on the OS and the compiler that you're using. To be strictly portable you can only rely on signed integers handling values from -32767 to 32767 (or 0 to 65535 for unsigned), but I suspect most of the people here don't have to worry about portability to that extent.

    Originally posted by ...
    when you multiply 2 ints together and they go above the maximum int value, the int loops around and starts all over from its smallest value ( -2 mill for an int, 0 for an unsigned int ) and continues counting.
    I believe this is guaranteed for unsigned integers, where an overflow will effectively result in modulo [max value+1] being applied, but for signed integers I believe overflow results in undefined behaviour. For some (perhaps many or even all) compilers this may result in wrap around to the most negative value, but IIRC the language offers no such guarantee.

    Originally posted by ...
    a char is one byte and therefore can store 256 different values ( 0 - 255 for an unsigned, -127 to 128 for a signed ).

    if your signed char equaled 127 and you added 3 to it, your char would now equal -126. the computer adds 1 which makes it 128 ( the maximum for a signed char ) and then when it adds the second one it loops around and becomes -127. incrimenting it one last time makes -127 into -126.
    Typically 8 bit chars store -128 to 127, I've never heard of a compiler that uses -127 to 128 (as the most significant bit is usually the sign bit). Also, although -128 to 127 is typical, char is only guaranteed to be able to hold -127 to 127 as far as the language is defined. Once again, this will be of concern if you want strict portability but not for many here.

  11. #11
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    at least you werent salem

    i was kinda waiting for salem to rip my post apart, because i realized after i wrote it that there were a few errors in it, and he always seems to be lurking around every corner waiting to prove me wrong.

    (hey, i wrote it in like two minutes, give me a break)
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 11:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2003, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21