Clock

this is my mid term project. I think Ive got it down, but I was hoping some more experienced eyes could look over my code and offer any suggestions because I really need to get a good grade. It works as far as I can tell (i get no errors when compiled) but i tend to make a lot of stupid mistakes. even if you dont look over all of, any part you do look at for me would be helpful. thank you in advance.

the problem is:

a) Extend the definition of the class clockType by overloading the post-increment operator function as a member of the class clockType

b) write the definition of the function to overload the post nicrement operator for the class clockType as defined in part a


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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#ifndef H_Clock
#define H_Clock

#include<iostream>

using namespace std;

class clockType
{
	friend ostream& operator << ( ostream&, const clockType& );
	
	friend istream& operator >> ( istream&, clockType& );

public:
	void setTime( int, int, int );
	void getTime( int&, int&, int& ) const;

	clockType operator++();

	clockType operator++( int );

	bool operator==( const clockType& ) const;
	bool operator!=( const clockType& ) const;
	bool operator<=( const clockType& ) const;
	bool operator<( const clockType& ) const;
	bool operator>=( const clockType& ) const;
	bool operator>( const clockType& ) const;

	clockType( int = 0, int = 0, int = 0 );

	int hr;
	int min;
	int sec;
};

#endif

ostream& operator << ( ostream& osObject, const clockType& timeOut)
{

	if ( timeOut.hr < 10 )
		osObject << "0";
	osObject << timeOut.hr << ":";

	if ( timeOut.min < 10 )
		osObject << "0";
	osObject << timeOut.min << ":";

	if ( timeOut.sec < 10 )
		osObject << "0";
	osObject << timeOut.sec << ":";

	return osObject;

}

istream& operator >> ( istream& isObject, clockType& timeIn )
{
	char ch;

	isObject >> timeIn.hr;
	isObject.get( ch );
	isObject >> timeIn.min;
	isObject.get( ch );
	isObject >> timeIn.sec;

	return isObject;

}

void clockType::setTime( int hours, int minutes, int seconds )
{
	if( 0 <= hours && hours <24 )
		hr = hours;
	else 
		hr = 0;
	if( 0 <= minutes && minutes <60 )
		min = minutes;
	else
		min = 0;
	if( 0 <= seconds && seconds <60 )
		sec = seconds;
	else 
		sec = 0;
}

void clockType::getTime( int& hours, int& minutes, int& seconds ) const
{
	hours = hr;
	minutes = min;
	seconds = sec;
}

clockType clockType::operator++()
{
	sec++;

	if( sec > 59 )
	{
		sec = 0;
		min++;

		if( min > 59 )
		{
			min = 0;
			hr++;

			if( hr > 23 )
				hr =  0;
		}
	}
	return*this;
}

clockType clockType::operator++( int )
{
	clockType clock( *this );

	sec++;

	if( sec > 59 )
	{
		sec = 0;
		min++;

		if( min > 59 )
		{
			min = 0;
			hr++;

			if( hr > 23 )
				hr =  0;
		}
	}
	return clock;
}

bool clockType::operator==( const clockType& otherClock ) const
{
	return( ( hr == otherClock.hr ) && ( min == otherClock.min ) && ( sec == otherClock.sec ) );
}

bool clockType::operator!=( const clockType& otherClock ) const
{
	return( ( hr == otherClock.hr ) || ( min == otherClock.min ) || ( sec == otherClock.sec ) );
}

bool clockType::operator<=( const clockType& otherClock ) const
{
	return( ( hr < otherClock.hr ) || 
		  ( ( hr == otherClock.hr ) && ( min < otherClock.min ) ) ||
		  ( ( hr == otherClock.hr ) && ( min == otherClock.min ) && 
		  ( sec <= otherClock.sec ) ) );
}

bool clockType::operator<( const clockType& otherClock ) const
{
	return( ( hr < otherClock.hr ) || 
		  ( ( hr == otherClock.hr ) && ( min < otherClock.min ) ) ||
		  ( ( hr == otherClock.hr ) && ( min == otherClock.min ) && 
		  ( sec < otherClock.sec ) ) );
}

bool clockType::operator>=( const clockType& otherClock ) const
{
	return( ( hr > otherClock.hr ) || 
		  ( ( hr == otherClock.hr ) && ( min > otherClock.min ) ) ||
		  ( ( hr == otherClock.hr ) && ( min == otherClock.min ) && 
		  ( sec >= otherClock.sec ) ) );
}

bool clockType::operator>( const clockType& otherClock ) const
{
	return( ( hr > otherClock.hr ) || 
		  ( ( hr == otherClock.hr ) && ( min > otherClock.min ) ) ||
		  ( ( hr == otherClock.hr ) && ( min == otherClock.min ) && 
		  ( sec > otherClock.sec ) ) );
}

clockType::clockType( int hours, int minutes, int seconds )
{
	setTime( hours, minutes, seconds );
}

int main()
{
	cout << "\n\n\tA Program to check the functionality"
		 << "\n\tof the class clockType" << endl;

	clockType myClock( 23, 45, 12 );
	clockType yourClock;

	char choice;

	cout << "\n\tMy Clock: " << myClock;

	cout << "\n\tEnter the hours, minutes and seconds of your clock";
	cin >> yourClock;

	do 
	{
		cout << "\n\n\t\t\tM E N U";
		cout << "\n\n\t Operation >\t: > ";
		cout << "\n\tOperation <\t: < ";
		cout << "\n\tOperation ==\t: == ";
		cout << "\n\tOperation !=\t: !";
		cout << "\n\tOperation >=\t: 1";
		cout << "\n\tOperation <=\t: 2";
		cout << "\n\tPre-Increment\t: 3";
		cout << "\n\tPost-Increment\t: 4";
		cout << "\n\tQuit\t: Q";

		cout << "\n\n\tPlease enter your choice: ";
			cin >> choice;

		if ( choice > 91 )
			choice -= 32;

		switch( choice )
		{
		case '>' : cout << "\n\tMy Clock > your clock is ";
			if ( myClock > yourClock )
				cout << "true";
			else
				cout << "false";
			break;

		case '<' : cout << "\n\tMy Clock < your clock is ";
			if( myClock < yourClock )
				cout << "true";
			else
				cout << "false";
			break;

		case '=' : cout << "\n\tMy Clock ==  your clock is";
			if( myClock == yourClock )
				cout << "True";
			else
				cout << "false";
			break;
		
		case '!' : cout << "\n\tMy Clock != your clock is";
			if( myClock != yourClock )
				cout << "true";
			else
				cout << "false";
			break;

		case '1' : cout << "\n\tMy Clock >= your clock is";
			if( myClock >= yourClock )
				cout << "true";
			else
				cout << "false";
			break;

		case '2' : cout << "\n\tMy Clock <= your clock is";
			if( myClock <= yourClock)
				cout << "true";
			else
				cout << "false";
			break;

		case '3' : cout << ++yourClock;
			cout << "-- Pre-incremented";
				break;

		case '4' : cout << yourClock++;
			cout << "- Post-incremented";
				break;
		}

	}while (choice != 'Q'); 

	cout << "\n\n\t";
	system( "pause" );
	return 0;
}
Looks pretty good.

One caution: You chose to implement the class using the int type which can contain negative values and it's certainly possible to enter negative values in your program via the overloaded operator>>.

It doesn't matter in your program, but typically the prefix operator++ would return a reference and not a copy.

I think I would be inclined to implement the postfix operator++ like so:

1
2
3
4
5
6
7
8
clockType clockType::operator++( int )
{
    clockType clock( *this );

    this->operator++() ;

    return clock;
}


so that any future changes to the implementation of the class would only require an update in the prefix operator++, and there isn't so much code duplication. The same goes for defining the relational operators in terms of other relational operators.
for your ++ operator: instead of repeating the code put it in it's own function and call it from both operators. line 117 stays of course where it is.

your operator!= is wrong. Here's a little trick:
1
2
3
4
bool clockType::operator!=( const clockType& otherClock ) const
{
	return ( ! operator==(otherClock) );
}
The same applies to the other comparison operators
Last edited on
thank you!
Topic archived. No new replies allowed.