Question about making a .h file for my assignment

So I have code that is a clock basically. In all of our assignments we need to name it pass(1-8).cpp This one is pass8.cpp. Part of the directions include this

"You should put the class prototype in Clock.h, and implement all the Clock member functions in Clock.cpp. In a third file, main.cpp, write the following code."

Then he says after that that we will compile it with g++ pass8.cpp Clock.cpp

I don't see what the point in the pass8.cpp file is if everything is already in the other three files. Can someone help me out with this as I don't have too much experience with .h files.


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

using namespace std;

class Clock
{
private:
	int mHours; //(range 0-23)
	int mMinutes; //(range 0-59)
	int mSeconds; //(range 0-59)
public:
	Clock();
	Clock(int);
	Clock(int, int);
	Clock(int, int, int);
	int getHours()  
		{return mHours;}
	int getMinutes()  
		{return mMinutes;}
	int getSeconds()  
		{return mSeconds;}
	void setHours(int hourValue) {
		if ( hourValue >= 0 && hourValue <23) { 
			mHours = hourValue;
		} else if(hourValue<0) { 
			mHours = 0;
		} else if(hourValue>12) {
			mHours = 12;
		}
	}
	void setMinutes(int minuteValue) {
		if ( minuteValue >= 0 && minuteValue < 60) {
			mMinutes = minuteValue;
		} else {
			mMinutes = 0;
		}	
	}
	void setSeconds(int secondValue) {
		if ( secondValue >= 0 && secondValue < 60) {
			mSeconds = secondValue;
		} else {
			mSeconds = 0;
		}
	}
	bool isMorning();
	bool isAfternoon();
	bool isEvening();
	void tick();
	friend ostream &operator<<(ostream &, Clock&);	
};
	ostream &operator<<(ostream &out, Clock &time){
		string display;	
		if(time.mHours >12)
		{	time.mHours = time.mHours - 12;
			display = "PM";
		}else{
			display = "AM";
		}

		
		out << setfill('0') << setw(2)<<time.mHours;
		out << ":"<<setfill('0')<<setw(2)<< time.mMinutes;
		out << ":"<<setfill('0')<<setw(2)<< time.mSeconds
		<< " "<<display << endl; 
		return out;
}
	Clock::Clock() {
		setHours(12);
		setMinutes(0);
		setSeconds(0);
		}
	Clock::Clock(int hourValue) 
		{setHours(hourValue);}
	Clock::Clock(int hourValue, int minuteValue) 
		{setHours(hourValue);
		setMinutes(minuteValue);}
	Clock::Clock(int hourValue, int minuteValue, int secondValue) 
		{setHours(hourValue);
		setMinutes(minuteValue);
		setSeconds(secondValue);}
	

	bool Clock::isMorning() {
		if ( mHours < 12) {
			return true;
		}else{
			return false;
		}
	}
	bool Clock::isAfternoon() {
		if ( mHours >= 12 && mHours < 18) {
			return true;
		}else{ 
			return false;
		}
	}
	bool Clock::isEvening() {
		if ( mHours < 18) {
			return false;
		}else{
			return true;
		}
	}
	void Clock::tick() {
		if (mSeconds < 60)
			mSeconds = mSeconds + 1;
		else 
			mSeconds = 0;
		if (mMinutes < 60 && mSeconds == 0)
			mMinutes = mMinutes + 1;
		else 
			mMinutes = 0;
		if (mHours < 24 && mMinutes == 0 && mSeconds == 0)
			mHours = mHours + 1;
		else 
			mHours = 0;
		
}
void printTimeMessage(Clock a) {
		
		cout << "The current time is "<< a << endl;
}

int main() {
	
	int hour;
	int minute;
	int second;
	

	cout << "What is the hour?" << endl;
	cin >> hour;
	
	cout << "What is the minute?" << endl;
	cin >> minute;

	cout << "What is the second?" << endl;
	cin >> second;

        Clock a(hour,minute,second);

printTimeMessage(a);

        a.setHours(hour+2);
        a.setMinutes(minute+30);
        a.setSeconds(second+15);
	
	cout << "Back to the Future!"; 
	printTimeMessage(a);

	a.tick();
	printTimeMessage(a);
        a.tick();
	printTimeMessage(a);
        a.tick();
	printTimeMessage(a);
        a.tick();
	printTimeMessage(a);
        a.tick();
	printTimeMessage(a);
        a.tick();
	printTimeMessage(a);
        a.tick();
	printTimeMessage(a);
        a.tick();
	printTimeMessage(a);
        a.tick();
	printTimeMessage(a);
        a.tick();
	printTimeMessage(a);

return 0;
}
	
The idea here is that you want to do something like
1
2
3
4
5
6
7
8
9
10
11
12
// Clock.h

// #include guard -- prevents your header file from being #included multiple times
#ifndef CLOCK_H_INCLUDED_ORSOMEOTHERUNIQUENAMEOFYOURCHOICE
#define CLOCK_H_INCLUDED_ORSOMEOTHERUNIQUENAMEOFYOURCHOICE

class Clock
{
    // Member variable/function declarations here
};

#endif 

1
2
3
4
5
6
7
8
9
10
// Clock.cpp

#include "Clock.h"
// Whatever other #includes you might need

// Member function definitions here, e.g.
Clock::Clock()
{
    // etc.
}

1
2
3
4
5
6
7
8
9
// main.cpp

#include "Clock.h"
// Whatever other #includes you need

int main()
{
    // Use Clock here
}

That's how you would split everything into multiple files.
Last edited on
But what about the main.cpp that he wants?
Topic archived. No new replies allowed.