1

so I've been having trouble with operator overloading. So I have a program that has an object called Weight that has 2 attributes, pounds and ounces. I figured out all the other operators, but the increment one has been giving me trouble. I tried to do it this way, but for some reason, it doesn't want to work.

Here are the declarations in the header file (including the 2 variables):

    void operator++();
    void operator--();
private:
    int pounds;
    int ounces;

And the member functions:

void Weight::operator++() {
    pounds + 1;
    ounces + 15;
}
void Weight::operator--() {
    pounds - 1;
    ounces - 15;
}

Anything helps!

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • Recommended reading: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Mar 15 '19 at 03:27
  • 1
    This has nothing to do with overloading increment operators at all. This is a very basic question about how to add something to a variable. – tkausl Mar 15 '19 at 03:28
  • What should the increment be? Right now you are incrementing by almost 2 pounds. If you increment by points, easy. Just increment `pounds` and leave `ounces` alone.If you are incrementing by ounces, increment `ounces` and if it hits 16, reset `ounces` to 0 and increment `pounds`. Note: if I'm wrong on 16 ounces per pound, ah well. I live in a metric country. – user4581301 Mar 15 '19 at 03:31
  • @tkausl the main problem has nothing to do with the overloading, but Tilak has the wrong return type, so sooner or the incorrect behaviour is going to bite. `std::cout << x++;` will go boom, for example. – user4581301 Mar 15 '19 at 03:33
  • 1
    Your first mistake was in thinking that C++ requires proper fractions. It doesn't. Everything will be easier if you store and compute using the total number of ounces. Only for display purposes do you need to break 100 oz into 6 lb 4 oz. The rest of the time, there might as well not even be a pounds unit. – Ben Voigt Mar 15 '19 at 06:43

1 Answers1

1

The posted code suffers from two problems.

  1. Unclear about that should happen when you increment or decrement a Weight objects. Should its value go up/down by one ounce or one pound.

  2. The expresssions pounds + 1, ounces + 15, etc. don't change anything in the object. They compute a value and the resultant is discarded.

Assuming the inrement operator changes the value by one ounce, you'll have to use:

void Weight::operator++() {
    ounces++;

    // If ounces becomes 16, we have to increment pounds by one and set ounces to zero.
    if ( ounces == 16 )
    {
        pounds++;
        ounces = 0;
    }

    // That can also be achieved by using.
    // pounds += (ounces / 16);
    // ounces =  (ounces % 16);
}

Also, the cannonical practice for overloading the ++ operator is to return a reference to the object. Hence, you should use:

Weight& Weight::operator++() {
    ounces++;

    // If ounces becomes 16, we have to increment pounds by one and set ounces to zero.
    if ( ounces == 16 )
    {
        pounds++;
        ounces = 0;
    }

    // That can also be achieved by using.
    // pounds += (ounces / 16);
    // ounces =  (ounces % 16);

    return *this;
}

You'll have to update the operator-- function similarly.

R Sahu
  • 204,454
  • 14
  • 159
  • 270