Question

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 for the member variable hours (if not argument is sent, the default value is 0), 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 a new object under class NumDays. For this new object, its member variable hours should be the summary of the two existed objects’ hours members; its member variable days should be based on its member variable hours.

- Subtraction operator. When one NumDays object is subtracted from another, the overloaded -operator should return a new object under class NumDays. For this new object, its member variable hours should be the difference between the two objects’ hours members; its member variable days should be based on its member variable hours. (allow negative value for hours and days, e.g. hours = -48 and days = -2)

Please put NumDays class declaration and member functions’ definitions in one header file named NumDays.h

Define a main function in a source file named HW3.cpp to test the class.

In this main function, define 4 objects under class NumDays. These objects are named as one, two, three, four. When creating these objects, object one’s hours is initialized to 24; object two’s hours is initialized to 12; object three’s hours is initialized to 0 (default value); object four’ s hours is initialized to 0 (default value).

Then add object one and object two, and assign result to object three; subtract object two from object one, and assign result to object four.

At last, display values of days in object one, two, three, four respectively.

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// NumDays.h

#ifndef NUMDAYS_H
#define NUMDAYS_H

class NumDays
{
public:
NumDays(double hrs);
NumDays();

NumDays operator + ( NumDays &rhs );
NumDays operator - ( NumDays &rhs );
double getDays();

private:
// Declaring variables
double hours;
double days;
};
#endif

___________________

// NumDays.cpp

#include <iostream>
using namespace std;
#include "NumDays.h"

NumDays::NumDays(double hrs)
{
this->hours=hrs;
}
NumDays::NumDays()
{
this->hours=0;   
}

NumDays NumDays::operator + ( NumDays &rhs )
{
double hrs=hours+rhs.hours;
NumDays n(hrs);
n.days=n.getDays();
return n;   
}
NumDays NumDays::operator - ( NumDays &rhs )
{
double hrs=this->hours-rhs.hours;
NumDays n(hrs);
n.days=n.getDays();
return n;
}

double NumDays::getDays()
{
return hours/24;
}

________________________

// main.cpp

#include <iostream>
using namespace std;
#include "NumDays.h"

int main()
{
NumDays one(24);
NumDays two(12);
NumDays three(0);
NumDays four(0);
three=one+two;
four=one-two;
cout<<"No Of Days in Object#1"<<endl;
cout<<one.getDays()<<endl;
cout<<"No Of Days in Object#2"<<endl;
cout<<two.getDays()<<endl;
cout<<"No Of Days in Object#3"<<endl;
cout<<three.getDays()<<endl;
cout<<"No Of Days in Object#4"<<endl;
cout<<four.getDays()<<endl;
  
return 0;
}


_________________________

Output:


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Design a class named NumDays, to store a value that represents a number of hours and...
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
  • 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....

  • 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...

  • 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...

  • 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...

  • 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...

  • Write a class declaration named Circle with a private member variable named radius.

     in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159*radius*radius. Add a default constructor to the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign its value to the radius...

  • 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...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • Write a class declaration named Circle with a private member variable named radius.

    CODE IN C++Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius. Add a default constructor the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign radius...

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