Homework problem. The #1 requirement by the teacher is we can't adjust the main (driver) function. Must build our class around it. Having a hard time with handling the NumDays Four Instantiated object. Also not sure if I did the ostream and istream functions correctly. Please help before I loose my hair.
#include <iostream.h>
#include <stdlib.h>
class NumDays
{
private:
float hours;
float days;
void calcDays(void) { days = hours / 8; }
public:
NumDays(float h) { hours = h; }
NumDays();
float GetDays();
NumDays operator+(const NumDays &);
NumDays operator-(const NumDays &);
NumDays operator++(void);
NumDays operator++(int);
NumDays operator--(void);
NumDays operator--(int);
friend ostream &operator<<(ostream &, NumDays &);
friend istream &operator>>(istream &, NumDays &);
};
float NumDays::GetDays(void)
{
calcDays();
return days;
}
NumDays NumDays:perator+(const NumDays &right)
{
NumDays temp;
temp.hours = hours + right.hours;
temp.calcDays();
return temp;
}
NumDays NumDays:perator-(const NumDays &right)
{
NumDays temp;
temp.hours = hours - right.hours;
temp.calcDays();
return temp;
}
NumDays NumDays:perator++(void)
{
++hours;
calcDays();
return *this;
}
NumDays NumDays:perator++(int)
{
NumDays temp(hours);
hours++;
calcDays();
return temp;
}
NumDays NumDays:perator--(void)
{
--hours;
calcDays();
return *this;
}
NumDays NumDays:perator--(int)
{
NumDays temp(hours);
hours--;
calcDays();
return temp;
}
ostream &operator<<(ostream &strm, NumDays &obj)
{
strm << obj.hours << " hour(s), " << obj.days << " day(s) ";
return strm;
}
istream &operator>>(istream &strm, NumDays &obj)
{
cout << "One: ";
strm >> obj.hours;
cout << " Two: ";
strm >> obj.hours;
cout << "One + Two: ";
strm >> obj.days;
return strm;
}
//Either copy your source code here or
// #include "Your header file here"
// 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 object called One: ";
cin >> Hours1;
cout << "Enter the number of hours for 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 << " hour(s)" << endl;
cout << "One - Two: " << One - Two << " hour(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;
system("PAUSE");
}