Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with overriding the tick function so it outputs alarm when I give th

ID: 3773955 • Letter: I

Question

I need help with overriding the tick function so it outputs alarm when I give the time.

Write a class named "Alarm" that is derived from the Clock class.

Your Alarm class must be split up into a separate header and implementation file.

Make a default constructor that calls the Clock class default constructor.

Make a constructor that takes the hour, minute, and second for the starting time in addition to an hour, minute and second for an alarm time. It should store the alarm time inside the class and invoke the Clock constructor for the hour, minute, and second starting time.

Override the tick() virtual function so that it calls the Clock tick() method and then if the current time equals the alarm time, output a message that the alarm has gone off.

Change the main function so it creates an Alarm object and sets the alarm to something close to the starting time. For example, if the starting time is 23 hours, 59 minutes, and 50 seconds, and the alarm time is 0 hours, 0 minutes, and 5 seconds, then the output should look something like this.

Here is the source code:

#ifndef _CLOCK_
#define _CLOCK_

class Clock
{
protected:
// Time in 24 hour format.
int hour, minute, second;
public:
Clock();
Clock(int hour, int minute, int second);
void outputTime();
int getHour();
int getMinute();
int getSecond();
virtual void tick();

private:
void startTimer(); // Internal timer for 1 second intervals
};

#endif


#include <iostream>
#include <csignal>
#include <cstdio>
#include <unistd.h>
#include "clock.h"

using namespace std;


void handleSignal(int num);   
Clock* ptrClock;


Clock::Clock() : hour(0), minute(0), second(0)
{
startTimer();
}

Clock::Clock(int hour, int minute, int second)
{
this->hour = hour;
this->minute = minute;
this->second = second;
startTimer();
}

void Clock::outputTime()
{

printf("%02d:%02d:%02d ",hour,minute,second);
}
int Clock::getHour()
{
return hour;
}
int Clock::getMinute()
{
return minute;
}
int Clock::getSecond()
{
return second;
}

// A second elapsed, update the clock variables
void Clock::tick()
{
second++;
if (second == 60)
{
minute++;
second = 0;
}
if (minute == 60)
{
hour++;
minute=0;
}
if (hour==24)
{
hour=0;
}
}


void Clock::startTimer()
{
ptrClock = this; // Remember pointer to this clock object
signal(SIGALRM, handleSignal);
alarm(1);
}


void handleSignal(int signalNum)
{
// Call the tick() method on the clock
ptrClock->tick();
alarm(1);
}

#include <iostream>
#include <unistd.h>
#include "clock.h"

using namespace std;

int main()
{
Clock c(23,59,50);
while (true)
{
c.outputTime();
sleep(1); // waits one second
}
return 0;
}

Explanation / Answer

Here is the code for implementing the Alarm Clock.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote