First off, this is for academic purposes. I'm extremely frustrated, and I have even been working this out with a friend for half the day.
We're working with both overloaded operators and friend classes and this lab has made me feel like a complete idiot. I'll post the important code below but the whole project will be in a zip file at the bottom of the post. I'm sure it's full of errors and it's now refusing to save some of my code (it keeps throwing an exception).
This is the header file code for my primary class
class NumDays
{
private:
int hours;
double days;
void calcDays(int);
public:
NumDays (int);
void setHours (int);
double getWork();
NumDays operator+ (const NumDays &);
NumDays operator- (const NumDays &);
NumDays operator++ ();
void operator<< (const NumDays &);
friend class Overtime(const NumDays &);
};
It doesn't seem to like my attempt to pass in a NumDays object into the Overtime class constructor, but from my understanding, this is how it should work because the overtime objects are constructed with each NumDays object
The second issue is with the overloaded operators, I'm having less trouble with wrapping my head around this and more trouble with why it has to be implemented so specifically.
This is the code for the overloaded operators
/*overloading the + operator***************************************************/
NumDays NumDays::operator+ (const NumDays &right)
{
NumDays temp;
temp.hours = hours + right.hours;
return temp;
}
/*overloading the -operator***************************************************/
NumDays NumDays::operator- (const NumDays &right)
{
NumDays temp;
temp.hours = hours - right.hours;
return temp;
}
/*overloading the ++ operator**************************************************/
NumDays NumDays::operator++ ()
{
++hours;
return *this;
}
/*overloading the << operator**************************************************/
void NumDays::operator<< (const NumDays &objOput)
{
cout << objOput.getWork << " days have been worked by this work";
}
It's definitely not a fan of the << overloading, but I basically copied code directly from my book to try and do this.
Original code link was broken, here is a permanent link to it. http://www.mediafire.com/file/j4q3fln9a8p98ll/dayCounter.zip
Also somewhat fixed my code. Doesn't work perfectly, but it does compile and mostly work. http://www.mediafire.com/file/g5m21drbuab8tso/Lab5workCounter.zip
NumDays
has no default constructor, so you can’t make one and then set its hours
member in the operators.NumDays&
from operator++()
. (This is “merely” a good idea; it doesn’t require any other code changes here.)operator<<
as a class member, since it’s supposed to take a std::ostream&
first argument.()
on your method calls.