Answer to Question #329792 in C++ for Rival

Question #329792

Objective: 

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.


1
Expert's answer
2022-04-17T13:53:52-0400
#include <iostream>

using namespace std;

class NumDays
{
	float days;
	int hours;
public:
	NumDays(int _hours):hours(_hours)
	{
		days = (float)hours / 24;
	}


	NumDays():days(0),hours(0){}
	void Set()
	{
		int h;
		int ds;
		cout << "\nPlease, enter days: ";
		cin>> ds;
		cout << "Please, enter hours ";
		cin >> h;
		days = ds+ (float)h / 24;
		hours =ds*24+ h;
	}
	int operator+ (NumDays& n)
	{
		return (hours + n.hours);
	}
	int operator- (NumDays& n)
	{
		return (hours - n.hours);
	}
	void Display()
	{
		cout << "\nDays are " << days
			<< "\nHours are " << hours<<endl;
	}
};


int main()
{
	NumDays n(130);
	n.Display();
	NumDays m;
	m.Set();
	m.Display();
	cout << (n + m) << endl;
	cout << (n - m) << endl;
}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS