jeudi 26 février 2015

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



Aucun commentaire:

Enregistrer un commentaire