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.
#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;
}
Comments
Leave a comment