### NumDays Class

Essentially I was creating a class called NumDays where I convert the number of hours into a day since the problem stated there are 8 hours in a day.
So I believe there is some logical error somewhere in the math I am not sure where.
Output:
The # of hours you have is: 12
The # of days you have is: 1.5
The # of hours you have is: 14
The # of days you have is: 1.75
The # of hours you have is: 26
The # of days you have is: 3.25
The # of hours you have is: 26
The # of days you have is: 3.25
The # of hours you have is: 13
The # of days you have is: 1.5
The # of hours you have is: 14
The # of days you have is: 1.5
The # of hours you have is: 13
The # of days you have is: 1.75
The # of hours you have is: 12
The # of days you have is: 1.75

Put the code you need help with here.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173`` `````` #include using namespace std; // This class is called NumDays class NumDays { private: double days; double hours; public: void setHours(double); void setDays(double); double getHours(); double getDays(); NumDays(); NumDays(double); void Print(); // This returns the sum of two object hours NumDays operator + (const NumDays dayConvert) { return hours + dayConvert.hours; } // This returns the difference of the object hours NumDays operator - (const NumDays dayConvert) { return hours + dayConvert.hours; } // This returns the number of days incremented NumDays operator ++ (int increaseDays) { NumDays temp; temp.hours = ++hours; temp.days = hours / 8; return temp; } // This returns the number of days incremented NumDays operator++ () { NumDays temp; temp.hours = ++hours; temp.days = hours / 8; return temp; } // This returns the number of days decremented NumDays operator -- (int decreaseDays) { NumDays temp; temp.hours = --hours; temp.days = hours / 8; return temp; } // This returns the number of days decremented NumDays operator -- () { NumDays temp; temp.hours = --hours; temp.days = hours / 8; return temp; } }; // Set up default constructor NumDays::NumDays() { days = 0; hours = 0; } // Set up non-default constructor NumDays::NumDays(double h) { hours = h; days = h / 8; } // Converting hours to day void NumDays::setDays(double h) { hours = h; days = hours / 8; } // Storing hours with a temp variable void NumDays::setHours(double h) { hours = h; } // Return the # of hours double NumDays::getHours() { return hours; } // Return the # of days double NumDays::getDays() { return days; } // Display the following statements void NumDays::Print() { cout << "The # of hours you have: " << getHours() << endl; cout << "The # of days you now have: " << getDays() << endl; } // Main test driver int main() { // Create an object for class NumDays hoursToDay(12); // Display the info from the user hoursToDay.Print(); // Second class object NumDays secondHours; secondHours.setHours(10); secondHours.setDays(14.0); // Display the info from the user secondHours.Print(); // Taking the sum of two class objects NumDays thirdHours; thirdHours = hoursToDay + secondHours; // Display the sum thirdHours.Print(); // Taking the difference of two class objects NumDays fourthHours; fourthHours = hoursToDay - secondHours; // Display the difference fourthHours.Print(); // Incrementing before the class object returns ++hoursToDay; // Display hoursToDay++ hoursToDay.Print(); // Incrementing after the class object returns hoursToDay++; // Display hoursToDay++ hoursToDay.Print(); // Decrementing before the second class object returns --secondHours; // Display --secondHours secondHours.Print(); // Decrementing before the second class object returns secondHours--; // Display the info from the user secondHours.Print(); system("PAUSE"); return 0; }``````
Last edited on

 Please learn to use code tags, they make reading and commenting on source code MUCH easier. How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/ There are other tags available. How to use tags: http://www.cplusplus.com/articles/z13hAqkS/ HINT: you can edit your post and add code tags. Some formatting & indentation would not hurt either
There are issues with pre/post increment/decrement. They all have to adjust the value of the object (which isn't being done). The pre inc/dec return a ref to the object after the inc/dec whereas post inc/dec return the original value. Shouldn't setDays()/setHours() set days/hours and calculate the hours/days? Possibly something like:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143`` ``````#include // This class is called NumDays class NumDays { private: const static size_t HoursDay {8}; double days {}; double hours {}; public: void setHours(double); void setDays(double); double getHours() const; double getDays() const; NumDays(); NumDays(double); void Print() const; // This returns the sum of two object hours NumDays operator+(const NumDays& dayConvert) const { return hours + dayConvert.hours; } // This returns the difference of the object hours NumDays operator-(const NumDays dayConvert) const { return hours - dayConvert.hours; } // Prefix increment NumDays& operator++() { days = ++hours / HoursDay; return *this; } // Postfix increment NumDays operator++(int) { auto old {*this}; operator++(); return old; } // Prefix decrement NumDays& operator--() { days = --hours / HoursDay; return *this; } // Postfix decrement NumDays& operator--(int) { auto old {*this}; operator--(); return old; } }; // Set up default constructor NumDays::NumDays() {} // Set up non-default constructor NumDays::NumDays(double h) : hours(h), days(h / HoursDay) {} // Set days void NumDays::setDays(double d) { hours = d * HoursDay; days = d; } // Set hours void NumDays::setHours(double h) { hours = h; days = h / HoursDay; } // Return the # of hours double NumDays::getHours() const { return hours; } // Return the # of days double NumDays::getDays() const { return days; } // Display the following statements void NumDays::Print() const { std::cout << "The # of hours you have: " << getHours() << '\n'; std::cout << "The # of days you now have: " << getDays() << '\n'; } // Main test driver int main() { // Create an object for class NumDays hoursToDay(12); // Display the info from the user hoursToDay.Print(); // 12, 1.5 // Second class object NumDays secondHours; //secondHours.setHours(10); secondHours.setDays(1.25); // Display the info from the user secondHours.Print(); // 10, 1.25 // Taking the sum of two class objects NumDays thirdHours {hoursToDay + secondHours}; // Display the sum thirdHours.Print(); // 22, 2.75 // Taking the difference of two class objects NumDays fourthHours {hoursToDay - secondHours}; // Display the difference fourthHours.Print(); // 2, 0.25 // Incrementing before the class object returns ++hoursToDay; // Display hoursToDay++ hoursToDay.Print(); // 13, 1.625 // Incrementing after the class object returns hoursToDay++; // Display hoursToDay++ hoursToDay.Print(); // 14, 1.75 // Decrementing before the second class object returns --secondHours; // Display --secondHours secondHours.Print(); // 9, 1.125 // Decrementing before the second class object returns secondHours--; // Display the info from the user secondHours.Print(); // 8, 1 }``````

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170`` ``````#include using namespace std; // This class is called NumDays class NumDays { private: double days; double hours; public: void setHours(double); void setDays(double); double getHours(); double getDays(); NumDays(); NumDays(double); void Print(); // This returns the sum of two object hours NumDays operator + (const NumDays dayConvert) { return hours + dayConvert.hours; } // This returns the difference of the object hours NumDays operator - (const NumDays dayConvert) { return hours + dayConvert.hours; } // This returns the number of days incremented NumDays operator ++ (int increaseDays) { NumDays temp; temp.hours = ++hours; temp.days = hours / 8; return temp; } // This returns the number of days incremented NumDays operator++ () { NumDays temp; temp.hours = ++hours; temp.days = hours / 8; return temp; } // This returns the number of days decremented NumDays operator -- (int decreaseDays) { NumDays temp; temp.hours = --hours; temp.days = hours / 8; return temp; } // This returns the number of days decremented NumDays operator -- () { NumDays temp; temp.hours = --hours; temp.days = hours / 8; return temp; } }; // Set up default constructor NumDays::NumDays() { days = 0; hours = 0; } // Set up non-default constructor NumDays::NumDays(double h) { hours = h; days = h / 8; } // Converting hours to day void NumDays::setDays(double h) { hours = h; days = hours / 8; } // Storing hours with a temp variable void NumDays::setHours(double h) { hours = h; } // Return the # of hours double NumDays::getHours() { return hours; } // Return the # of days double NumDays::getDays() { return days; } // Display the following statements void NumDays::Print() { cout << "The # of hours you have: " << getHours() << endl; cout << "The # of days you now have: " << getDays() << endl; } // Main test driver int main() { // Testing NumDays constructor. NumDays hoursToDay(12); cout << "Now printing hoursToDay, which should be 12:" << endl; hoursToDay.Print(); // Testing .setHours() method. NumDays secondHours; secondHours.setHours(10); cout << "The result of secondHours.setHours is:" << endl; secondHours.Print(); // Testing .setDays() method. secondHours.setDays(14.0); cout << "The result of secondHours.setDays is:" << endl; secondHours.Print(); // Taking the sum of two NumDays class objects NumDays thirdHours; thirdHours = hoursToDay + secondHours; thirdHours.Print(); // Taking the difference of two NumDays class objects NumDays fourthHours; fourthHours = hoursToDay - secondHours; fourthHours.Print(); // Testing the prefix ++ operator. NumDays prefixResult = ++hoursToDay; cout << "The result for ++hoursToDay is: " << endl; prefixResult.Print(); cout << "hoursToDay is now : " << endl; hoursToDay.Print(); // Test the postfix ++ operator. NumDays postfixResult = hoursToDay++; cout << "The result for hoursToDay++ is: " << endl; postfixResult.Print(); hoursToDay.Print(); // Test teh prefix -- operator NumDays prefixTemp = --secondHours; cout << "The result for --secondHours is: " << endl; prefixTemp.Print(); secondHours.Print(); // Decrementing before the second class object returns NumDays postfixTemp = secondHours--; cout << "The result for secondHours-- is: " << endl; postfixTemp.Print(); secondHours.Print(); system("PAUSE"); return 0; }``````

Hi @seeplus what do you think?
Last edited on
The pre-inc/dec operators are still not as expected by convention. pre-inc/dec return a ref to the object and post-inc/dec return the value before the operation. If you inc/dec the hours (or set them), then shouldn't you also adjust days - and vice/versa? Member functions that don't change values of member variables should be marked as const.
Topic archived. No new replies allowed.