In: Computer Science
C++ Program for Project Alarm.
------------------------------------------------------------------------------
Fix ALL Errors in my EXISTING code for ALL 5 files.
MUST add comments for FULL CREDIT.
Take a screenshot of the working solution output.
-------------------------------------------------------------------------------
Instructions:
Use class composition to define and implement a new class called Alarm that contains a Time member variable.
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:
- print to print out the alarm’s description and hour, minute, and second of the alarm time.
- setDescription to accept a string argument and use it to set the description member variable.
-setAtime to accept three integers (for hour, minute, and second) to set the atime member variable
(alarm time).
- getDescription to return the value of the description member variable.
- getAtime to return the atime member as a Time object.
- A default constructor that initializes the description to the string “None” and the alarm time to
00:00:00.
- 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.
- 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.
- 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.
Hint: (Time.h, Time.cpp, Alarm.h, Alarm.cpp, Main.cpp.)
-------------------------------------------------------------------------------------
Note:(Detailed comments are helpful for me so I can better
understand where I went wrong. So please be detailed.
Edit my code so it works, but do not add highly advanced code.)
------------------------------------------------------------------------------------
MY CODE:
-------------------------------------------------------------------------------------
//////////////////////////////////// Time.h ///////////////////////////////////////////
#include<iostream>
using namespace std;
class Time
{
private:
int hour;
int minute;
int second;
public:
/* Constructors */
Time(); //Default constructor
Time(int h, int m, int s); //Overloaed
Constructor
Time(const Time&);
/* Functions */
void print(); // Print the Time
int getHour(); //Accessor function
int getMinute(); //Accessor function
int getSecond(); //Accessor function
void setHour(int h); //Mutator change Hour
void setMinute(int m); //Mutator Change Minute
void setSecond(int s); //Mutator Change Second
void setTime(int h, int m, int s); //Overloaded
Constructor
bool equals(const Time&)const;
};
////////////////////////////////END OF Time.h/////////////////////////////////////
//////////////////////////////////// Time.cpp ////////////////////////////////////////
#include "Time.h"
#include "Alarm.h"
#include<iomanip>
using namespace std;
void Time::print()
{
cout << setw(2) << setfill('0') << hour <<
":"
<< setw(2) << setfill('0') << minute <<
":"
<< setw(2) << setfill('0') << second <<
endl;
}
// Default Constructor
Time::Time()
{
hour = 0; minute = 0; second = 0;
}
// copy Constructor
Time::Time(const Time& other) {
hour = other.hour;
minute = other.minute;
second = other.second;
}
// Accessor for Hour
int Time::getHour()
{
return hour;
}
// Accessor for Minute
int Time::getMinute()
{
return minute;
}
// Accessor for Second
int Time::getSecond()
{
return second;
}
// Mutator for Hour
void Time::setHour(int h)
{
// Validate Hour value
if (h >= 0 && h <= 23)
hour = h;
else
hour = 0;
}
// Mutator for Minute
void Time::setMinute(int m)
{
// Validate Minute value
if (m >= 0 && m <= 59)
minute = m;
else
minute = 0;
}
// Mutator for Second
void Time::setSecond(int s)
{
if (s >= 0 && s <= 59)
second = s;
else
second = 0;
}
// Set Time function
void Time::setTime(int h, int m, int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
Time::Time(int h, int m, int s)
{
setTime(h, m, s);
}
bool Time::equals(const Time& other)const
{
return hour == other.hour && minute == other.minute
&& second == other.second;
}
////////////////////////////////END OF Time.cpp/////////////////////////////////
//////////////////////////////////// Alarm.h //////////////////////////////////////////
#include<iostream>
using namespace std;
class Alarm
{
private:
string description;
Time atime;
public:
/* Constructors */
void print();
void setDescription(string i);
void setAtime(int h, int m, int s);
Time getAtime();
void getDescription(); // Accessor Function
Alarm() {description = "None";}
Alarm(string i, int h, int m, int s);
};
//////////////////////////////// END OF Alarm.h /////////////////////////////////
/////////////////////////////////// Alarm.cpp //////////////////////////////////////
#include "Time.h"
#include "Alarm.h"
#include<iomanip>
using namespace std;
void Alarm::print()
{
cout << description << "";
atime.print();
}
void Alarm::setDescription(string i)
{
description = i;
}
void Alarm::setAtime(int h, int m, int s)
{
atime.setTime(h, m, s);
}
Time Alarm::getAtime()
{
return atime;
}
void Alarm::getDescription()
{
}
Alarm::Alarm()
{
description = "None";
}
Alarm::Alarm(string desc, int hour, int minute, int second) :
atime(hour, minute, second)
{
description = desc;
}
bool Alarm::equals(const Alarm& other)
{
return atime.equals(other.atime) &&
description == other.description;
}
//////////////////////////////// END OF Alarm.cpp ////////////////////////////
//////////////////////////////////// Main.cpp /////////////////////////////////////////
// I didn't write the main.
// complete the main.
/////////////////////////////////// END OF Main.cpp ////////////////////////
------------------------------------------------------------------------------------------------------------
Thank you.
#include<iostream>
using namespace std;
class Time
{
private:
int hour;
int minute;
int second;
public:
/* Constructors */
Time(); //Default constructor
Time(int h, int m, int s); //Overloaed
Constructor
Time(const Time&);
/* Functions */
void print(); // Print the Time
int getHour(); //Accessor function
int getMinute(); //Accessor function
int getSecond(); //Accessor function
void setHour(int h); //Mutator change Hour
void setMinute(int m); //Mutator Change Minute
void setSecond(int s); //Mutator Change Second
void setTime(int h, int m, int s); //Overloaded
Constructor
bool equals(const Time&)const;
}
#include "Time.h"
#include "Alarm.h"
#include<iomanip>
using namespace std;
void Time::print()
{
cout << setw(2) << setfill('0') << hour <<
":"
<< setw(2) << setfill('0') << minute <<
":"
<< setw(2) << setfill('0') << second <<
endl;
}
// Default Constructor
Time::Time()
{
hour = 0; minute = 0; second = 0;
}
// copy Constructor
Time::Time(const Time& other) {
hour = other.hour;
minute = other.minute;
second = other.second;
}
// Accessor for Hour
int Time::getHour()
{
return hour;
}
// Accessor for Minute
int Time::getMinute()
{
return minute;
}
// Accessor for Second
int Time::getSecond()
{
return second;
}
// Mutator for Hour
void Time::setHour(int h)
{
// Validate Hour value
if (h >= 0 && h <= 23)
hour = h;
else
hour = 0;
}
// Mutator for Minute
void Time::setMinute(int m)
{
// Validate Minute value
if (m >= 0 && m <= 59)
minute = m;
else
minute = 0;
}
// Mutator for Second
void Time::setSecond(int s)
{
if (s >= 0 && s <= 59)
second = s;
else
second = 0;
}
// Set Time function
void Time::setTime(int h, int m, int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
Time::Time(int h, int m, int s)
{
setTime(h, m, s);
}
bool Time::equals(const Time& other)const
{
return hour == other.hour && minute == other.minute
&& second == other.second;
}
#include<iostream>
using namespace std;
class Alarm
{
private:
string description;
Time atime;
public:
/* Constructors */
void print();
void setDescription(string i);
void setAtime(int h, int m, int s);
Time getAtime();
void getDescription(); // Accessor Function
Alarm() {description = "None";}
Alarm(string i, int h, int m, int s);
};
#include "Time.h"
#include "Alarm.h"
#include<iomanip>
using namespace std;
void Alarm::print()
{
cout << description << "";
atime.print();
}
void Alarm::setDescription(string i)
{
description = i;
}
void Alarm::setAtime(int h, int m, int s)
{
atime.setTime(h, m, s);
}
Time Alarm::getAtime()
{
return atime;
}
void Alarm::getDescription()
{
}
Alarm::Alarm()
{
description = "None";
}
Alarm::Alarm(string desc, int hour, int minute, int second) :
atime(hour, minute, second)
{
description = desc;
}
bool Alarm::equals(const Alarm& other)
{
return atime.equals(other.atime) &&
description == other.description;
}