I need some help with an operator overload problem. At the bottom is my complete output. I can not figure out what is wrong with my output. It shows:
//One + Two: 11.25 day(s)
//One - Two: -1.25 day(s)
The professor's example when entering one as 40 and two as 50 as:
One + Two: 90 day(s)
One - Two: -10 day(s)
What am I missing? The rest of the output for Four is fine. What code am I missing and where?
Thanks!
*********************
#include <iostream.h> // Needed to overload << and >>
class NumDays
{
private:
float hours;
float days;
void calcDays(void) { days = hours / 8; }
public:
NumDays() { }
NumDays(float h) { hours = h; }
float GetDays();
NumDays operator+(const NumDays &); // overloaded +
NumDays operator-(const NumDays &); // overloaded -
NumDays operator++(void); // prefix ++
NumDays operator++(int); // postfix++
NumDays operator--(void); // prefix--
NumDays operator--(int); // postfix--
friend ostream &operator<<(ostream &, NumDays &); // overload the stream extraction operator
};
float NumDays::GetDays(void)
{
calcDays();
return days;
}
NumDays NumDays::operator+(const NumDays &right)
{
NumDays temp;
temp.hours = hours + right.hours;
temp.calcDays();
return temp;
}
NumDays NumDays::operator-(const NumDays &right)
{
NumDays temp;
temp.hours = hours - right.hours;
temp.calcDays();
return temp;
}
NumDays NumDays::operator++(void)
{
++hours;
calcDays();
return *this;
}
NumDays NumDays::operator++(int)
{
NumDays temp(hours);
hours++;
calcDays();
return temp;
}
NumDays NumDays::operator--(void)
{
--hours;
calcDays();
return *this;
}
NumDays NumDays::operator--(int)
{
NumDays temp(hours);
hours--;
calcDays();
return temp;
}
ostream &operator<<(ostream &strm, NumDays &obj)
{
strm << obj.days;
return strm;
}
// 14.1 Driver Program
//Do not alter anything in the main function.
// Instead, alter your class so it compiles using this main function.
void main(void)
{
float Hours1, Hours2;
//Prompt for the data for the first 2 objects
cout << "Enter the number of hours for the the object called One: ";
cin >> Hours1;
cout << "Enter the number of hours for the the object called Two: ";
cin >> Hours2;
//Instantiate One and Two
NumDays One(Hours1), Two(Hours2);
cout << "One: " << One.GetDays() << " day(s)" << endl;
cout << "Two: " << Two.GetDays() << " day(s)" << endl << endl;
//Addition and subtraction operators
cout << "One + Two: " << One + Two << " day(s)" << endl;
cout << "One - Two: " << One - Two << " day(s)" << endl << endl;
//Instantiate Three to the sum of One and Two
NumDays Three(One + Two);
//Instantiate Four
NumDays Four;
//Increment and decrement operators
Four = Three++;
cout << "Four = Three++ " << endl;
cout << " Three: " << Three.GetDays() << " day(s)" << endl;
cout << " Four: " << Four.GetDays() << " day(s)" << endl << endl;
Four = ++Three;
cout << "Four = ++Three: " << endl;
cout << " Three: " << Three.GetDays() << " day(s)" << endl;
cout << " Four: " << Four.GetDays() << " day(s)" << endl << endl;
Four = Three--;
cout << "Four = Three--: " << endl;
cout << " Three: " << Three.GetDays() << " day(s)" << endl;
cout << " Four: " << Four.GetDays() << " day(s)" << endl << endl;
Four = --Three;
cout << "Four = --Three: " << endl;
cout << " Three: " << Three.GetDays() << " day(s)" << endl;
cout << " Four: " << Four.GetDays() << " day(s)" << endl;
}
// My output
//Enter the number of hours for the object called one: 40
//Enter the number of hours for the object called two: 50
//One: 5 day(s)
//Two: 6.25 day(s)
//One + Two: 11.25 day(s)
//One - Two: -1.25 day(s)
// Four = Three++
// Three: 11.375 day(s)
// Four: 11.25 day(s)
//Four = ++Three:
// Three: 11.5 day(s)
// Four: 11.5 day(s)
//Four = Three--:
// Three: 11.375 day(s)
// Four: 11.5 day(s)
//Four = --Three:
// Three: 11.25 day(s)
// Four: 11.25 day(s)