Classes: Alarm Clock

Nov 12, 2017 at 2:33am
This is what I have so far... I need to: In the main function, write statements to call setAlarmClock to set the time to all zeros, and call getAlarmClock to get an object of AlarmClock and display all the time (i.e. hours, minutes, seconds, alarmHours, alarmMinutes, alarmSeconds) of the object.

I'm mostly struggling with the getAlarmClock function.

Any tips are greatly appreciated. Thank you.


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
#include <iostream>
using namespace std;

class Clock
{
private:
	int hours;
	int minutes;
	int seconds;
public:
	//Constructor #1
	Clock()
	{
		hours = 00;
		minutes = 00;
		seconds = 00;
	}
	
	//Constructor #2
	Clock(int hr, int min, int sec)
	{
		hours = hr;
		minutes = min;
		seconds = sec;
	}

	void setClock(int, int, int);
};

void Clock::setClock(int hr, int min, int sec)
{
	hours = hr;
	minutes = min;
	seconds = sec;
}

class AlarmClock : public Clock
{
private:
	int alarmHours;
	int alarmMinutes;
	int alarmSeconds;
public:
	//Constructor #1
	AlarmClock()
	{
		alarmHours = 00;
		alarmMinutes = 00;
		alarmSeconds = 00;
	}
	//Constructor #2
	AlarmClock(int hr, int min, int sec)
	{
		alarmHours = hr;
		alarmMinutes = min;
		alarmSeconds = sec;
	}

	void setAlarmClock(int, int, int);
	
	AlarmClock getAlarmClock() const
	{
		AlarmClock result;

		result.alarmHours = alarmHours;
		result.alarmMinutes = alarmMinutes;
		result.alarmSeconds = alarmSeconds;

		return result;
	}

};

void AlarmClock::setAlarmClock(int hr, int min, int sec)
{
	alarmHours = hr;
	alarmMinutes = min;
	alarmSeconds = sec;
}

int main()
{
	AlarmClock time;

	time.setAlarmClock(00, 00, 00);

	AlarmClock alarmTime = time.getAlarmClock();



	cout << endl;
	system("pause");
	return 0;
}
Nov 12, 2017 at 8:12am
A better getAlarmClock function would be:
AlarmClock* getAlarmClock () { return this;}
Notice the pointer. Alternatively, you can use the copy constructor.
 
AlarmClock newClock(oldClock);

This creates a new clock as a copy of the old clock.
Last edited on Nov 12, 2017 at 8:14am
Nov 12, 2017 at 8:13am
Your program works just fine.
Nov 12, 2017 at 6:56pm
1
2
3
4
5
6
7
8
9
10
AlarmClock getAlarmClock() const
	{
		AlarmClock result;

		result.alarmHours = alarmHours;
		result.alarmMinutes = alarmMinutes;
		result.alarmSeconds = alarmSeconds;

		return result;
	}

If I wrote this function correctly, then how do I call alarmHours, alarmMinutes, and alarmSeconds in main?
cout << time.getAlarmClock(); doesn't work.
Thank you.
Nov 12, 2017 at 7:05pm
It doens't work, because cout doesn't know how to output an object of type AlarmClock. What you could do is 1) overload the << operator
https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx
or the easy way 2)
 
cout << time.getAlarmHours() << " " << time.getAlarmMinutes() << " " << time.getAlarmSeconds() << endl;


You will need to implement the get functions I used in this snipped.

Nov 12, 2017 at 7:14pm
Ok that's what I thought... didn't know if it was possible to do it that way. Thank you so much.
Topic archived. No new replies allowed.