Design a class called NumDays. The class\'s purpose is to store a value that rep
ID: 3938639 • 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 to 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 also have the following overloaded 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 increment the number of hours stored in the object. When incremented, the number of days should be automatically recalculated. - Prefix and postfix decrement operators. These operators should decrement the number of hours stored in the object. When decremented, the number of days should be automatically recalculated.Explanation / Answer
#include<iostream>
using namespace std;
class numdays
{
int no_wrk_hrs;
float no_wrk_days;
public:
void result();
numdays(int x) //constructor
{
//8 hrs is equal to 1 day
cout<<"enter the tot hours:";
no_wrk_hrs=x;
no_wrk_days=no_wrk_hrs/8;
cout<<" "<<"the tot days worked:"<<no_wrk_days;
}
void result() //function showing result
{
cout<<" "<<the tot hours:"<<s1.no_wrk_hrs;
cout<<" "<<"the tot day:"<<s1.no_wrk_days;
cout<<" "<<the tot hours:"<<s2.no_wrk_hrs;
cout<<" "<<"the tot day:"<<s2.no_wrk_days;
}
int operator ++()
{
int add;
add = s1.no_wrk_hrs + s2.no_wrk_hrs;
return add;
//cout<<" "<<"the obj1 and obj2 hrs are added"<<add;
}
int operator --()
{
int sub;
sub = s1.no_wrk_hrs - s2.no_wrk_hrs;
if(sub<0)
{
sub=s2.no_wrk_hrs - s1.no_wrk_hrs;
return sub;
//cout<<" "<<"the obj1 and obj2 hrs are subtracted"<<sub;
}
else
{
return sub;
cout<<" "<<"the obj1 and obj2 hrs are subtracted"<<sub;
}
}
}s1,s2; // obj declared
int main()
{
cout<<" enter the tot hours:";
cin>>s1.no_wrk_hrs;
s1(no_wrk_hrs);
s1.result();
cout<<" enter the tot hours:";
cin>>s2.no_wrk_hrs;
s2(no_wrk_hrs);
s2.result();
operator ++();
operator --();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.