Design a class called NumDays whose purpose is to store a value that represents
ID: 3631992 • Letter: D
Question
Design a class called NumDays whose purpose is to store a value that represents a #of work hours and convert it to a number of days. ex .8 hours would be 1 day, 12 hrs = 1.5 days. the class should have a constructor that accepts a #of hours as well as member functions for storing and retrieving the hours and days. SHould have overloaded operators. "+" when 2 NumDays are added the sum of the objects hours should be returned "-" when 2 NumDays are subtracted the diff should be returned "++" increments the #of hours stored in the object "--"#number of hours should be decreasedExplanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
class NumDays
{private:
double hours;
double days;
public:
NumDays()
{hours=0;
days=0;
}
NumDays(double h)
{hours=h;
days=h/8;
}
void setHours(double h)
{hours=h;
days=h/8;
}
void setDays(double d)
{days=d;
hours=d*8;
}
double getHours()
{return hours;
}
double getDays()
{return days;
}
NumDays NumDays::operator+ (NumDays &n)
{NumDays t;
t.setHours(hours+n.getHours());
return t;
}
NumDays NumDays::operator- (NumDays &n)
{ NumDays t;
t.setHours(hours-n.getHours());
return t;
}
NumDays NumDays::operator++ ()
{hours++;
days=hours/8;
return *this;
}
NumDays NumDays::operator-- ()
{hours--;
days=hours/8;
return *this;
}
NumDays NumDays::operator++ (int)
{NumDays t(hours);
hours++;
days=hours/8;
return t;
}
NumDays NumDays::operator-- (int)
{NumDays t(hours);
hours--;
days=hours/8;
return t;
}
};
int main()
{
NumDays a(10),b(20),c;
cout<<"Initially ";
cout<<"a="<<a.getDays()<<endl;
cout<<"b="<<b.getDays()<<endl;
c=a+b;
cout<<"after c=a+b: c="<<c.getDays()<<endl;
c=a-b;
cout<<"after c=a-b: c="<<c.getDays()<<endl;
c=a++;
cout<<"after c=a++: a="<<a.getDays()<<", c="<<c.getDays()<<endl;
c=++a;
cout<<"after c=++a: a="<<a.getDays()<<", c="<<c.getDays()<<endl;
c=a--;
cout<<"after c=a--: a="<<a.getDays()<<", c="<<c.getDays()<<endl;
c=--a;
cout<<"after c=--a: a="<<a.getDays()<<", c="<<c.getDays()<<endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class NumDays
{private:
double hours;
double days;
public:
NumDays()
{hours=0;
days=0;
}
NumDays(double h)
{hours=h;
days=h/8;
}
void setHours(double h)
{hours=h;
days=h/8;
}
void setDays(double d)
{days=d;
hours=d*8;
}
double getHours()
{return hours;
}
double getDays()
{return days;
}
NumDays operator+ (NumDays &);
NumDays operator- (NumDays &);
NumDays operator++ (); //Prefix
NumDays operator++ (int); //Postfix
NumDays operator-- (); //prefix
NumDays operator-- (int); //Postfix
};
NumDays NumDays::operator+ (NumDays &n)
{NumDays t;
t.setHours(hours+n.getHours());
return t;
}
NumDays NumDays::operator- (NumDays &n)
{ NumDays t;
t.setHours(hours-n.getHours());
return t;
}
NumDays NumDays::operator++ ()
{hours++;
days=hours/8;
return *this;
}
NumDays NumDays::operator-- ()
{hours--;
days=hours/8;
return *this;
}
NumDays NumDays::operator++ (int)
{NumDays t(hours);
hours++;
days=hours/8;
return t;
}
NumDays NumDays::operator-- (int)
{NumDays t(hours);
hours--;
days=hours/8;
return t;
}
int main()
{
NumDays a(10),b(20),c;
cout<<"Initially ";
cout<<"a="<<a.getDays()<<endl;
cout<<"b="<<b.getDays()<<endl;
c=a+b;
cout<<"after c=a+b: c="<<c.getDays()<<endl;
c=a-b;
cout<<"after c=a-b: c="<<c.getDays()<<endl;
c=a++;
cout<<"after c=a++: a="<<a.getDays()<<", c="<<c.getDays()<<endl;
c=++a;
cout<<"after c=++a: a="<<a.getDays()<<", c="<<c.getDays()<<endl;
c=a--;
cout<<"after c=a--: a="<<a.getDays()<<", c="<<c.getDays()<<endl;
c=--a;
cout<<"after c=--a: a="<<a.getDays()<<", c="<<c.getDays()<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.