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

Need a program for C++ programming in visual studio. Number of Days. Design a cl

ID: 675119 • Letter: N

Question

Need a program for C++ programming in visual studio.

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

Source code for above program is below:

#include<iostream>
using namespace std;

class NumDays
{
   int days;
   int hours;
public:
   void setDays(int day)
   {
       days = day;
   }
   void setHours(int hour)
   {
       hours = hour;
   }
   int getDays()
   {
       return days;
   }
   int getHours()
   {
       return hours;
   }
   NumDays(int hour1)
   {
       hours = hour1;
       days = hour1/8;
   }
   ~NumDays(){}
   NumDays operator+(const NumDays& b)
   {
       NumDays day12;
       day12.days = this->days + b.days;
   day12.hours = this->hours + b.hours;
return day12;
}

NumDays operator-(const NumDays& b)
{
       NumDays day13;
      day13.days = this->days - b.days;
day13.hours = this->hours - b.hours;
return day13;
}

   NumDays& operator++()
{
days++;
hours++;
return *this;
}
   NumDays operator++(int)
{
NumDays temp = *this;
++*this;
return temp;
}

   NumDays()
   {
       days = 1;
       hours = 8;
   }
NumDays& operator--()
{
days--;
hours--;
return *this;
}

NumDays operator--(int)
{
NumDays temp = *this;
--*this;
return temp;
}

void display()
{
   cout<<"Days are: "<<days<<endl;
   cout<<"Hours are: "<<hours<<endl;
}
};


int main()
{
   NumDays n1(8),n2,n3;
   n1.display();
   return 1;
}

Output is:

Days are : 1

Hours are : 8

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