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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#include <iostream>
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;
}
|