Question

Design a class called NumDays. The class’ purpose is to store a value that represents a...

Design a class called NumDays. The class’ 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.

Write a program in C++ to demonstrate the NumDays class: create several objects, use their member functions and overloaded operators. Make sure to display (print) the before and after results.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <string>

using namespace std;

class NumDays

{

private :

int days;

int hrs;

public:

NumDays(int hrs)

{

this->hrs=hrs;

}

int getHrs()

{

return hrs;

}

void setHrs(int hrs)

{

this->hrs=hrs;

}

float getDays()

{

return hrs/8.0;

}

NumDays operator+(NumDays n)

{

NumDays nd(hrs+n.getHrs());

return nd;

}

NumDays operator-(NumDays n)

{

NumDays nd(hrs-n.getHrs());

return nd;

}

// Post-increment Operator

NumDays operator++()

{

hrs++;

return *this;

}

// Post-decrement Operator

NumDays operator--()

{

hrs--;

return *this;

}

// Pre-increment Operator

NumDays operator++(int)

{

NumDays old(*this);

++(*this);

return old;

}

// Pre-decrement Operator

NumDays operator--(int)

{

NumDays old(*this);

--(*this);

return old;

}

};

int main ()

{

NumDays n1(8),n2(12),n3(18);

cout<<"After converting "<<n1.getHrs()<<" hrs to days is "<<n1.getDays()<<endl;

cout<<"After converting "<<n2.getHrs()<<" hrs to days is "<<n2.getDays()<<endl;

cout<<"After converting "<<n3.getHrs()<<" hrs to days is "<<n3.getDays()<<endl;

cout<<"No of Hrs in n1 Before PostIncrement "<<n1.getHrs()<<endl;

n1++;

cout<<"No of Hrs in n1 After PostIncrement "<<n1.getHrs()<<endl;

cout<<"No of Hrs in n2 Before PostDecrement "<<n2.getHrs()<<endl;

n2--;

cout<<"No of Hrs in n2 After PostDecrement "<<n2.getHrs()<<endl;

cout<<"No of Hrs in n2 Before PreIncrement "<<n2.getHrs()<<endl;

++n2;

cout<<"No of Hrs in n2 After PreIncrement "<<n2.getHrs()<<endl;

cout<<"No of Hrs in n3 Before PreDecrement "<<n3.getHrs()<<endl;

--n3;

cout<<"No of Hrs in n3 After PreDecrement "<<n3.getHrs()<<endl;

NumDays n4=n1+n2;

cout<<" No of Hrs after adding n1 and n2 is "<<n4.getHrs()<<endl;

cout<<" No of Days in n4 is "<<n4.getDays()<<endl;

NumDays n5=n3-n1;

cout<<" No of Hrs after subtracting n1 from n3 is "<<n5.getHrs()<<endl;

cout<<" No of Days in n5 is "<<n5.getDays()<<endl;

return 0;

}

__________________

Output:

___________Thank You

Add a comment
Know the answer?
Add Answer to:
Design a class called NumDays. The class’ purpose is to store a value that represents a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to...

    Using simple, college level c++ programming 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...

  • Design a class named NumDays, to store a value that represents a number of hours and...

    Design a class named NumDays, to store a value that represents a number of hours and convert it to a number of days. For example, 24 hours would be converted to 1 day, 36 hours would be converted to 1.5 days, and 54 hours would be converted to 2.25 days. This class has two private member variables with data type double: one is named as hours; another is named as days. This class has a constructor that accepts a value...

  • Using C++, this assignment uses two classes which utilize the concept of aggregation. One class will...

    Using C++, this assignment uses two classes which utilize the concept of aggregation. One class will be called TimeOff. This class makes use of another class called NumDays. Then you will create a driver (main) that will handle the personnel records for a company and generate a report. TimeOff class will keep track of an employee’s sick leave, vacation, and unpaid time off. It will have appropriate constructors and member functions for storing and retrieving data in any of the...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

  • Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the...

    Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This class should have two private data members, m_city and m_state, that are strings for storing the city name and state abbreviation for some Address b. Define and implement public getter and setter member functions for each of the two data members above. Important: since these member functions deal with objects in this case strings), ensure that your setters...

  • C++ Question Your class should support the following operations on Fraction objects: • Construction of a...

    C++ Question Your class should support the following operations on Fraction objects: • Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors. You should check to make sure that the denominator is...

  • C++ 3. Day of the Year Modification Modify the DayOfYear class, written in an earlier Programming...

    C++ 3. Day of the Year Modification Modify the DayOfYear class, written in an earlier Programming Challenge, to add a constructor that takes two parameters: a string representing a month and an integer in the range 0 through 31 representing the day of the month. The constructor should then initialize the integer member of the class to represent the day specified by the month and day of month parameters. The constructor should terminate the program with an appropriate error message...

  • Java   Project -  Queue ADT Problem Statement and Assignment: Design an inventory class that stores the...

    Java   Project -  Queue ADT Problem Statement and Assignment: Design an inventory class that stores the following members serialNum: an integer that holds a part’s serial number manufactDate: a member that holds the date the part was manufactured lotNum: an integer that holds the part’s lot number The class should have appropriate member functions (interfaces) for storing data into, and retrieving data from, these members.   Then, design a program that uses the queue class.  The type of the queue...

  • C++ Create NumDays Class to store a value that represents number of hours worked and number...

    C++ Create NumDays Class to store a value that represents number of hours worked and number of days worked. Must ensure that the two representations are always consistent. Create a TimeOff Class to track an employee’s sick leave and vacation time. It should consist of the following members: maxSickDays, sickTaken, maxVacation, and vacTaken. These members should be of type NumDays. An employee, after one year of service, receives at the beginning of the year 160 hrs(20 days) of vacation time...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT