2

Working on an assignment and have almost everything finished except I can't seem to lay out the operator+ function of the class. Guidance/direction would be very welcomed as I can't seem to determine what I'm doing wrong.

#include <iostream>

using namespace std;

class numDays {

private: // member variables
    int hours;
    double days;

public: // member functions

    numDays(int h = 0) {
        hours = h;
        days = h / 8;
    }

    void setHours(int s) {
        hours = s;
        days = s / 8;
    }

    double getDays() {
        return days;
    }

    numDays operator+(numDays& obj) {
        // what to put here?
    }

    numDays operator- (numDays& obj) { // Overloaded subtraction
        // and here?
    }

    numDays operator++ () { // Overloaded postfix increment
        hours++;
        days = hours / 8.0;
        numDays temp_obj(hours);
        return temp_obj;
    }

    numDays operator++ (int) { // Overloaded prefix increment
        numDays temp_obj(hours);
        hours++;
        days = hours / 8.0;
        return temp_obj;
    }

    numDays operator-- () { // Overloaded postfix decrement
        hours--;
        days = hours / 8.0;
        numDays temp_obj(hours);
        return temp_obj;
    }

    numDays operator-- (int) { // Overloaded prefix decrement
        numDays temp_obj(hours);
        hours--;
        days = hours / 8.0;
        return temp_obj;
    }
};


int main() {
    // insert code here...
    numDays one(25), two(15), three, four;

    // Display one and two.
    cout << "One: " << one.getDays() << endl;
    cout << "Two: " << two.getDays() << endl;

    // Add one and two, assign result to three.
    three = one + two;

    // Display three.
    cout << "Three: " << three.getDays() << endl;

    // Postfix increment...
    four = three++;
    cout << "Four = Three++: " << four.getDays() << endl;

    // Prefix increment...
    four = ++three;
    cout << "Four = ++Three: " << four.getDays() << endl;

    // Postfix decrement...
    four = three--;
    cout << "Four = Three--: " << four.getDays() << endl;

    // Prefix increment...
    four = --three;
    cout << "Four = --Three: " << four.getDays() << endl;

    return 0;
}
amdn
  • 11,314
  • 33
  • 45
ljamison
  • 115
  • 1
  • 10

1 Answers1

6

You need to make a temp_obj and return it, just like you do in postfix operator++, however you will update the members of temp_obj instead of updating anything in this.

In fact you could make it a const member function so that the compiler will detect if you accidentally update this. Most people even use non-member operator overloading for operator+ to make it a symmetric relation.


Unrelated, but:

  • days = h / 8; is integer division (remainder is discarded)
  • your code appears to be maintaining days and hours in parallel, which is fragile. It seems that only hours should be a member variable, and the getDays function (which should be const) can compute it on the fly.
  • As pointed out by Dan Allen, the prefix operator++ and operator-- (which are the ones without the dummy argument) should not be returning by value - those functions should update members of this and return a reference to *this.

Pro tip: to avoid code duplication you can do the increment operators like this (assuming you want them both to be member functions):

numDays & operator++()          // prefix
{
    // your logic here, e.g. hours++;
    return *this;
}

numDays operator++(int dummy)   // postfix
{
     return ++numDays(*this);   // invokes prefix operator++ we already wrote
}
amdn
  • 11,314
  • 33
  • 45
M.M
  • 138,810
  • 21
  • 208
  • 365
  • 2
    To add to Matt's point. Some places you use hours/8 and others hours/8.0 you will encounter weird inconsistencies as is. Also your comments are wrong. `numDays operator++ (int)` is the post-fix operator. The code looks right. The comment is wrong. – Persixty Feb 26 '15 at 23:07
  • When you say "non-member operator overloading for `operator+`" are you talking about `friend numDays operator + (const numDays&)`? – Jonny Henly Feb 26 '15 at 23:13
  • @JonnyHenly no; outside the class definition `numDays operator+( numDays arg1, numDays arg2)`. `friend` functions are non-members however it is simpler to not use `friend` (IMHO friend functions are way too overused in study material). There are examples in [this thread](http://stackoverflow.com/questions/4421706/operator-overloading). – M.M Feb 26 '15 at 23:24
  • @MattMcNabb oh gotcha. Thank you for the clarification and the example link. I would +2 your answer. – Jonny Henly Feb 26 '15 at 23:28
  • @MattMcNabb I'm in the unfortunate position that my professor did a horrendous job explaining overloading to us so kindly pardon any stupid questions (I'm a first semester student in a 100-level Comp Sci class). When you say to update the members of `temp_obj` instead of `this` are you referring to `hours` and `days`? If so, would it be something along the lines of `numDays operator+ (numDays& obj) { numDays temp_obj(hours); temp_obj = hours + obj.getDays(); return temp_obj;` – ljamison Feb 26 '15 at 23:48
  • 1
    You mean `temp_obj.hours += obj.hours;` in the middle, however this wouldn't update `days`. In fact you can do it all in one go: `return numDays(hours + obj.hours);` because you have a constructor that accepts a number of hours. – M.M Feb 26 '15 at 23:54
  • It worked!!! Thank you sooo much. That return call is 1000x easier than the professor had tried to explain it! – ljamison Feb 27 '15 at 01:15