Design a class called NumDays. The class\'s purpose is to store a value that rep
ID: 3544320 • Letter: D
Question
Design a class called NumDays. The class's purpose is to store a value that represents a number of work hours and convert it into a number of days. for example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to 2.25 days. The class should have a constructor that accepts a number of hours, as well as member functions for storing and retrieving the hours and days. The class should have the following overloading operators:
+ addition operator: When two NumDays objects are added together, the overloaded + operator should return the sum of the two objects hours members.
-subtraction operator: When one NumDays object is subtracted from another, the overloaded- operator should return the difference of the two objects hours members.
++ prefix and postfix increment operators. These operators should incremenet the number of hours stored in that object. When incremented, the number of days should automatically be recalculated.
-- Prefix and postfix decrement operators. These operators should decrement the number of hours stored in that object. When decremented, the number of days should automatically be recalculated.
Explanation / Answer
// Go to: http://ideone.com/iXv1kZ for code checking
#include <iostream>
using namespace std;
class NumDays{
int hours;
float day;
public:
NumDays()
{
hours=0;
day=0.0;
};
NumDays(int h)
{
hours=h;
day=float(h/8.0);
};
int getHour()
{
return hours;
}
float getDay()
{
return day;
}
NumDays operator +(NumDays obj)
{
int h=getHour()+obj.getHour();
NumDays temp(h);
return temp;
}
NumDays operator -(NumDays obj)
{
int h=getHour()-obj.getHour();
NumDays temp(h);
return temp;
}
const NumDays& operator++() //prefix
{
++hours;
day=float(hours/8.0);
return *this;
}
const NumDays& operator--() //prefix
{
--hours;
day=float(hours/8.0);
return *this;
}
const NumDays operator++(int) //postfix
{
NumDays temp(*this);
++hours;
day=float(hours/8.0);
return temp;
}
const NumDays operator--(int) //postfix
{
NumDays temp(*this);
--hours;
day=float(hours/8.0);
return temp;
}
};
int main()
{
NumDays obj(2),obj2(10),obj3,obj4;
obj3=obj2-obj;
cout<<"'obj3=obj2-obj'=> Day:"<<obj3.getDay()<<"##Hour:"<<obj3.getHour()<<" ";
obj3=obj+obj2;
cout<<"'obj3=obj+obj2'=> Day:"<<obj3.getDay()<<"##Hour:"<<obj3.getHour()<<" ";
obj4=obj3++;
cout<<"'obj4=obj3++' => Day:"<<obj4.getDay()<<"##Hour:"<<obj4.getHour()<<" ";
obj4=++obj3;
cout<<"'obj4=++obj3' => Day:"<<obj4.getDay()<<"##Hour:"<<obj4.getHour()<<" ";
obj4=obj3--;
cout<<"'obj4=obj3--' => Day:"<<obj4.getDay()<<"##Hour:"<<obj4.getHour()<<" ";
obj4=--obj3;
cout<<"'obj4=--obj3' => Day:"<<obj4.getDay()<<"##Hour:"<<obj4.getHour()<<" ";
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.