Question

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

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

solution:

#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:

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\ClassNumDaysPreincrementPostIncrementOperato.. After converting 8 hrs to days is 1

please give me thumb up

Add a comment
Know the answer?
Add Answer to:
Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to...
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....

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

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

  • c++ program Implement a class called Person with the following members: 1a. ????, a private variable...

    c++ program Implement a class called Person with the following members: 1a. ????, a private variable of type ?????? 1b. ???, a private variable of type ??? 1c. Default constructor to set name to "" and age to 0 1d. Non-default constructor which accepts two parameters for name and age 1e. A copy constructor 1f. The post-increment operator 1g. The pre-decrement operator 1h. The insertion and extraction stream operators >>and <<

  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

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

  • 12. Consider C++ class. Which one of the following choices is NOT correct? A. Class instances...

    12. Consider C++ class. Which one of the following choices is NOT correct? A. Class instances can be static, stack dynamic, or heap dynamic. B. If static or stack dynamic, they are referenced directly with value variables. C. If stack dynamic, they are referenced through pointers. D. Stack dynamic instances of classes are always created by the elaboration of an object declaration. E. The lifetime of such a class instance ends when the end of the scope of its declaration...

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