ljamison
ljamison

Reputation: 115

C++ - overloading operator+ taking one argument

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;
}

Upvotes: 2

Views: 1007

Answers (1)

M.M
M.M

Reputation: 141618

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
}

Upvotes: 6

Related Questions