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.
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;
}
Last edited on
It would be helpful for us to help you if you would

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:

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
#include <iostream>

// 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
}

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
#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()
{
	// 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.