Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need c++ programming with output for: Number of Days. Design a class called Nu

ID: 3801164 • Letter: I

Question

I need c++ programming with output for:

Number of Days. Design a class called NumDays. The class’s purpose is to store a value that will convert the number of worked hours 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 converted to 2.25 days. The class must have a constructor that accepts a number of hours. There must also be member function to set and get the hours and days. The class should have 2 data members, hours and days.The class will also overload several operators:

the addition operator. This operator will add the hours of the two objects and return a new instance of the NumDays with its hour’s data member set to the sum of the other two objects.

the subtraction operator will also be overloaded which will subtract the two objects and return a new instance of the NumDays class.

the prefix and postfix increment operator. These operators should increment the number of hours stored in the object. It will return an instance of the NumDays object.

the prefix and postfix decrement operator. These operators should decrement the number of hours stored in the object. It will return an instance of the NumDays object.

Note that when the number of hours changes, the number of days should always be updated. The user of this class should be able to use the object in a statement like C = A + B; where A, B and C are instances of the NumDays class. Main must show that the class and all the operators work correctly.

Explanation / Answer

#include <iostream>
#include <iomanip>
using namespace std;

class NumDays
{
private:
double days;
double hours;
public:
NumDays() {}
NumDays(double h)
{
hours = h;
days = hours/8;
}

double getDays() {return days;}
double getHours() {return hours;}
double setHours(double h) {hours = h; days = hours/8;}
double setDays(double d) {days = d; hours = days*8;}

NumDays operator + (NumDays &obj) {
double h = hours + obj.getHours();
NumDays res = NumDays(h);
return res;
}

NumDays operator - (NumDays &obj) {
double h = hours - obj.getHours();
if (h < 0) h = 0;
NumDays res = NumDays(h);
return res;
}

// prefix ++
NumDays operator++ () {
++hours;
setHours(hours);
NumDays res = NumDays(hours);
return res;
}

// overloaded postfix ++ operator
NumDays operator++( int ) {
// save the orignal value
NumDays nd(hours);
// increment this object
hours++;
setHours(hours);
// return old original value
return nd;
}

// prefix --
NumDays operator-- () {
--hours;

if (hours < 0) hours = 0;
setHours(hours);
NumDays res = NumDays(hours);
return res;
}

// overloaded postfix -- operator
NumDays operator--( int ) {
// save the orignal value
NumDays nd(hours);
// increment this object
hours--;
if (hours < 0) hours = 0;
setHours(hours);

// return old original value
return nd;
}
};

int main()
{
NumDays A = NumDays(8);

cout << "A: ";
cout << "Hours: " << A.getHours() << " Days: " << A.getDays() << endl;

NumDays B = NumDays(18);

cout << "B: ";
cout << "Hours: " << B.getHours() << " Days: " << B.getDays() << endl;

NumDays C;

// add
C = A+B;
cout << "C = A+B: ";
cout << "Hours: " << C.getHours() << " Days: " << C.getDays() << endl;

// subtract
C = B - A;
cout << "C = B-A: ";
cout << "Hours: " << C.getHours() << " Days: " << C.getDays() << endl;


// prefix increment
C = ++A;
cout << "C = ++A: ";
cout << "A: ";
cout << "Hours: " << A.getHours() << " Days: " << A.getDays() << endl;
cout << "C: ";
cout << "Hours: " << C.getHours() << " Days: " << C.getDays() << endl;


// postfix increment
C = A++;
cout << "C = A++: "<< endl;
cout << "A: ";
cout << "Hours: " << A.getHours() << " Days: " << A.getDays() << endl;
cout << "C: ";
cout << "Hours: " << C.getHours() << " Days: " << C.getDays() << endl;

// prefix decrement
C = --A;
cout << "C = --A: ";
cout << "A: ";
cout << "Hours: " << A.getHours() << " Days: " << A.getDays() << endl;
cout << "C: ";
cout << "Hours: " << C.getHours() << " Days: " << C.getDays() << endl;

// postfix decrement
C = A--;
cout << "C = A--: ";
cout << "A: ";
cout << "Hours: " << A.getHours() << " Days: " << A.getDays() << endl;
cout << "C: ";
cout << "Hours: " << C.getHours() << " Days: " << C.getDays() << endl;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote