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. Part 2: Design a class named TimeOff. The purpose of the class is to track an employee’s sick leave, vacation, and unpaid time off. It should have, as members,

Respuesta :

Answer:

Check the explanation

Explanation:

#include <iostream>

#include <string>

using namespace std;

//class declaration

class NumDays{

private:

   double hours;

   double days;

public:

   //constructor

   NumDays(double h = 0){

       hours = h;

       days = h/(8.00);

   }

   //getter functions

   double getHours(){

       return hours;

   }

   double getDays(){

       return days;

   }

   //setter functions

   void setHours(double h){

       hours = h;

       days = h/(8.00);

   }

   void setDays(double d){

       days = d;

       hours = d*(8.00);

   }

   //overload + operator

   double operator+ (const NumDays &right){

       return hours+right.hours;

   }

   //overload - operator

   double operator- (const NumDays &right){

       //check if subtraction will give negative value

       if(hours < right.hours){

           cout << "ERROR! Cannot subtract! Now terminating!\n";

           exit(0);

       }

       return hours-right.hours;

   }

   //overload prefix ++ operator

   NumDays operator++(){

       //pre-increment hours member

       ++hours;

       //update days member

       days = hours/(8.00);

       //return modified calling object

       return *this;

   }

   //overload postfix ++ operator

   NumDays operator++(int){

       //post-increment hours member

       hours++;

       //update days member

       days = hours/(8.00);

       //return modified calling object

       return *this;

   }

   //overload prefix -- operator

   NumDays operator--(){

       //pre-decrement hours member

       --hours;

       //update days member

       days = hours/(8.00);

       //return modified calling object

       return *this;

   }

   //overload postfix -- operator

   NumDays operator--(int){

       //post-decrement hours member

       hours--;

       //update days member

       days = hours/(8.00);

       //return modified calling object

       return *this;

   }

};

int main()

{

   //create first object

   cout << "Creating object with 12 hours...\n";

   NumDays obj1(12);

   cout << obj1.getHours() << " hours = " <<obj1.getDays() << " days.\n";

   //create second object

   cout << "\nCreating object with 18 hours...\n";

   NumDays obj2(18);

   cout << obj2.getHours() << " hours = " <<obj2.getDays() << " days.\n";

   //test overloaded + operator

   cout << endl << "Adding hours... " << obj1 + obj2 << " hours.\n";

   //test overloaded - operator

   cout << endl << "Subtracting hours... " << obj2 - obj1 << " hours.\n\n";

   //test overloaded ++ operators

   cout << "Pre- and post-incrementing first object...\n";

   ++obj1;

   cout << obj1.getHours() << " hours = " <<obj1.getDays() << " days.\n";

   obj1++;

   cout << obj1.getHours() << " hours = " <<obj1.getDays() << " days.\n";

   //test overloaded -- operators

   cout << "\nPre- and post-decrementing second object...\n";

   --obj2;

   cout << obj2.getHours() << " hours = " <<obj2.getDays() << " days.\n";

   obj2--;

   cout << obj2.getHours() << " hours = " <<obj2.getDays() << " days.\n";

   return 0;

}

Answer:

Sew explaination foe code

Explanation:

Code below:

#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()<<"\n";

obj3=obj+obj2;

cout<<"'obj3=obj+obj2'=> Day:"<<obj3.getDay()<<"##Hour:"<<obj3.getHour()<<"\n";

obj4=obj3++;

cout<<"'obj4=obj3++' => Day:"<<obj4.getDay()<<"##Hour:"<<obj4.getHour()<<"\n";

obj4=++obj3;

cout<<"'obj4=++obj3' => Day:"<<obj4.getDay()<<"##Hour:"<<obj4.getHour()<<"\n";

obj4=obj3--;

cout<<"'obj4=obj3--' => Day:"<<obj4.getDay()<<"##Hour:"<<obj4.getHour()<<"\n";

obj4=--obj3;

cout<<"'obj4=--obj3' => Day:"<<obj4.getDay()<<"##Hour:"<<obj4.getHour()<<"\n";

};