In: Computer Science
A. Define and implement the class Alarm as follows: The class Alarm consists of two private member variables: description of type string and atime of type Time. The class Alarm also includes the following public member functions:
1. print to print out the alarm’s description and hour, minute, and second of the alarm time.
2. setDescription to accept a string argument and use it to set the description member variable.
3. setAtime to accept three integers (for hour, minute, and second) to set the atime member variable (alarm time).
4. getDescription to return the value of the description member variable.
5. getAtime to return the atime member as a Time object.
6. A default constructor that initializes the description to the string “None” and the alarm time to 00:00:00.
7. An overloaded constructor that accepts a string argument for the alarm description and three int arguments (as the hour, minute, and second) and use them to initialize the description and atime member variables of an Alarm object.
8. An overloaded constructor that accepts a string argument for the alarm description and a Time object and use them to initialize the description and atime member variables of an Alarm object. 9. An equals function to compare two Alarm objects’ values. Return true if both objects have the same values of the member variables. Otherwise, return false. B. In the function main, write statements to declare Alarm class objects and test your class implementation.
Your project is expected to contain five source files: Time.h, Time.cpp, Alarm.h, Alarm.cpp, Lab8.cpp.
Following c++ application demonstrates the use Time , Alarm classes.
Time class has hrs, min, sec.
Alarm class has alarm description, hrs, min, sec.
Instantiates alarm objects with different inputs.
Checks two alarm objects , whether they are equal or not.
PFB source code with sample output.
------------------
Time.h
------------------
#include<iostream>
#ifndef Time_H
#define Time_H
using namespace std;
class Time {
private:
int hrs, min, sec;
public:
Time();
Time(int hrs, int min, int sec);
int getHrs();
void setHrs(int hrs);
int getMin();
void setMin(int min);
int getSec();
void setSec(int sec);
};
#endif
------------------
------------------
Time.cpp
------------------
#include<iostream>
#include"Time.h"
//default constructor
Time::Time(){
hrs = min = sec =0;
}
//Instantiate time with hrs, min, sec provided
Time::Time(int hrs, int min, int sec){
this->hrs = hrs;
this->min = min;
this->sec = sec;
}
//geet hrs
int Time::getHrs(){
return hrs;
}
//set hrs
void Time::setHrs(int hrs){
this->hrs = hrs;
}
//get min
int Time::getMin(){
return min;
}
//set min
void Time::setMin(int min){
this->min = min;
}
//get sec
int Time::getSec(){
return sec;
}
//set sec
void Time::setSec(int sec){
this->sec = sec;
}
------------------
------------------
Alarm.h
------------------
#include<iostream>
#include<string>
#include "Time.h"
#ifndef Alarm_H
#define Alarm_H
using namespace std;
class Alarm : public Time{
private:
string description;
public:
Alarm();
Alarm(string description, int hrs, int min, int sec);
Alarm(string description, Time alarmTime);
void printAlarm();
void setDescription(string description);
void setAtime(int hrs, int min, int sec);
string getDescription();
Time getAtime();
bool equals(Time alarmTime);
};
#endif
------------------
------------------
Alarm.cpp
------------------
#include<iostream>
#include<string>
#include "Time.h"
#include "Alarm.h"
//default constructor
Alarm::Alarm(){
description = "None";
Time();
}
//instantiate alarm class with description, hrs, min, sec
Alarm::Alarm(string description, int hrs, int min, int sec){
this->description = description;
Time(hrs, min, sec);
}
//instantiate alarm class with description and Time
instance
Alarm::Alarm(string description, Time alarmTime){
this->description = description;
this->setHrs(alarmTime.getHrs());
this->setMin(alarmTime.getMin());
this->setSec(alarmTime.getSec());
}
//prints alarm details
void Alarm::printAlarm(){
cout<<endl<<"Alarm's description :
"<<description;
cout<<endl<<"Alarm's time :
"<<this->getHrs()<<":"<<this->getMin()<<":"<<this->getSec()<<endl;
}
//set alarm description
void Alarm::setDescription(string description){
this->description = description;
}
//set time with hrs, min, sec
void Alarm::setAtime(int hrs, int min, int sec){
this->setHrs(hrs);
this->setMin(min);
this->setSec(sec);
}
//get alarm description
string Alarm::getDescription(){
return description;
}
//get time instance
Time Alarm::getAtime(){
return Time(this->getHrs(), this->getMin(),
this->getSec());
}
//check for equal time
bool Alarm::equals(Time alarmTime){
if((this->getHrs()==alarmTime.getHrs()) &&
(this->getMin()==alarmTime.getMin()) &&
(this->getSec()==alarmTime.getSec())){
return true;
}else{
return false;
}
}
------------------
------------------
Lab8.cpp
------------------
#include <iostream>
#include "Alarm.h"
#include "Time.h"
using namespace std;
int main(){
//alarm1 object
Alarm alarm1 = Alarm("morning alarm", 5, 0, 0);
cout<<"Alarm1 :"<<endl;
//print alarm1
alarm1.printAlarm();
//time object
Time alarm2Time = Time(17,0,0);
Alarm alarm2 = Alarm("evening snacks time", alarm2Time);
cout<<"Alarm2 :"<<endl;
//print object2
alarm2.printAlarm();
//check of both above defined alarm objects are equal
if(alarm1.equals(alarm2)){
cout<<endl<<"Both alarms are same";
}else{
cout<<endl<<"Both alarms are different";
}
return 0;
}
------------------
------------------
sample output
------------------