this needs to be a continuation of the program I posted at the end of this!!! C+
ID: 3758315 • Letter: T
Question
this needs to be a continuation of the program I posted at the end of this!!! C++
Add a default constructor and a parameterized constructor (with three parameters) to AlarmClock class if you don’t have constructors in OLA5. Extend your ola#4 to solve ExtAlarmClock problem using C++ inheritance. One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. As an example, consider a "ExtAlarm Clock" that we might use to set alarm in different time zone. As an example, consider a "Alarm Clock" that we might use to set alarm. We could use this " Alarm Clock " in any software in which the time of day was necessary. Of course, the " Alarm Clock " consists of the data plus associated operations. When you set alarm clock, and displayAlarmClock () will display message like: As an example, consider a "ExtAlarm Clock" that we might use to set alarm with time zone. When you set ExtAlarm clock, and displayAlarmClock () will display message like: Note: It is still same method name: We are assuming that we are in CST time zon. Below is the complete specification of AlarmClock class. class AlarmClock { private: //declarations of data members that are private int alarmHour; //an hour in the range 1 - 12 int alarmMinute; //a minute in the range 0 - 59 string meridian; //is the time AM or PM /* Gets the hours of current time. Use sample code to get current time in desired(?) format. @return the hours, in military or standard format */ int get_hours() const; /* Gets the minutes of current time. Use sample code to get current time in desired(?) format. @return the minutes */ int get_minutes() const; public: // set the AlarmClock to the specified time void setAlarmClock (int h, int m, string mer); /* display this AlarmClock. It display the Alarm time. and how far ahead from the current time */ void displayAlarmClock() const; }; //end of class AlarmClock Write program that uses C++ class to set alarm clock. You may set alarm clock and you may also display how far ahead the alarm is set from the current (real) time. For example if you set the alarm wi5thout time zone: 9:50 PM displayAlarmClock () will display message using the current time when this method is called. For example if the current time is 8:45 AM when the method displayAlarmClock() called: displayAlarmClock() will print three lines: Time now is: 8:45 AM Alarm is set: 9:50 PM Alarm set for 13 hours 5 minutes from now. For example if you set the alarm 9:50 PM PST displayAlarmClock () will display message using the current time when this method is called. For example if the current time is 8:45 AM when the method displayAlarmClock() called: displayAlarmClock() will print three lines: Time now is: 8:45 AM Alarm is set: 9:50 PM PST Alarm set for 15 hours 5 minutes from now. Normally a class definition is separated into two sections: specification and implementation. You have to separate your class definition into two parts: specification and implementation, and we will place these in separate files. Your client program ( which has main() function ) will be in separate file, too. Sample client program. For example, your main program will have proper include statement and //proper include statements including your class definition int main() { char ch; AlarmClock clock1; AlarmClock clock2; ExtAlarmClock clock3; ExtAlarmClock clock4; ExtAlarmClock clock5; ExtAlarmClock clock6; //set alarm clock1.setAlarmClock (9, 50, "PM"); clock2.setAlarmClock (6, 0, "AM"); clock3.setAlarmClock (9, 50, "PM", PST); clock4.setAlarmClock (9, 10, "AM", CST); clock5.setAlarmClock (5, 35, "AM", PST); clock6.setAlarmClock (12, 30, "PM", PST); //cout <<"Enter a character to advance "; //delay your response for a while //cin>>ch; clock1.displayAlarmClock(); clock2.displayAlarmClock(); clock3.displayAlarmClock(); clock4.displayAlarmClock(); clock5.displayAlarmClock(); clock6.displayAlarmClock(); return 0; } Sample output: Time now is: 05:55 PM Alarm is set: 09:50 PM Alarm is set for 3 hours 55 minutes from now. Time now is: 05:55 PM Alarm is set: 06:00 AM Alarm is set for 12 hours 5 minutes from now. Time now is: 05:55 PM Alarm is set: 09:50 PM Pacific Time Alarm is set for 5 hours 55 minutes from now. Time now is: 05:55 PM Alarm is set: 09:10 AM Central Time Alarm is set for 15 hours 15 minutes from now. Time now is: 05:55 PM Alarm is set: 05:35 AM Pacific Time Alarm is set for 13 hours 40 minutes from now. Time now is: 05:55 PM Alarm is set: 12:30 PM Pacific Time Alarm is set for 20 hours 35 minutes from now. Sample code to get current time in C++ in Unix (ranger) #include #include using namespace std; int main () { time_t rawtime; struct tm *info; info = new tm; time( &rawtime ); info = localtime( &rawtime ); cout< using namespace std; #ifndef ALARMCLOCK_H_ #define ALARMCLOCK_H_ class AlarmClock { private: //declarations of data members that are private int alarmHour; //an hour in the range 1 - 12 int alarmMinute; //a minute in the range 0 - 59 string meridian; //is the time AM or PM /* Gets the hours of current time. Use sample code to get current time in desired(?) format. @return the hours, in military or standard format */ int get_hours() const; /* Gets the minutes of current time. Use sample code to get current time in desired(?) format. @return the minutes */ int get_minutes() const; public: // set the AlarmClock to the specified time void setAlarmClock(int h, int m, string mer); /* display this AlarmClock. It display the Alarm time. and how far ahead from the current time */ void displayAlarmClock() const; }; //end of class AlarmClock #endif ___________________________________________________________________________________________ underline{AlarmClock.cpp:} #include #include #include #include "AlarmClock.h" using namespace std; void AlarmClock::setAlarmClock(int h, int m, string mer){ this->alarmHour = h; this->alarmMinute = m; this->meridian = mer; } void AlarmClock::displayAlarmClock() const{ time_t rawtime; struct tm *info; info = new tm; time(&rawtime); info = localtime(&rawtime); cout << "Time now is: "; int hour = info->tm_hour; string mer = "AM"; if (hour > 12){ hour -= 12; mer = "PM"; } cout << info->tm_hour << ":"; cout << info->tm_min; cout << " " << mer << endl; cout << "Alarm is set: " << this->alarmHour << ":" << this->alarmMinute << " " << this->meridian << endl; if (get_minutes() > info->tm_min) cout << "Alarm set for " << get_hours() - hour << " hours " << get_minutes() - info->tm_min << " minutes from now." << endl; else cout << "Alarm set for " << get_hours() - hour - 1 << " hours " << get_minutes() - info->tm_min + 60 << " minutes from now." << endl; } int AlarmClock::get_hours() const{ if (meridian == "PM"){ return this->alarmHour + 12; } else{ return this->alarmHour; } } int AlarmClock::get_minutes() const{ return this->alarmMinute; } _____________________________________________________________________________________________ underline{TestAlarmClock.cpp:} #include "AlarmClock.h" #include using namespace std; int main() { char ch; AlarmClock clock1; AlarmClock clock2; //set alarm clock1.setAlarmClock(9, 50, "PM"); clock2.setAlarmClock(6, 0, "AM"); cout << "Enter a character to advance "; //delay your response for a while cin >> ch; //entered data 1 - 20 hours later //the following two lines are executed at 8:45 AM clock1.displayAlarmClock(); clock2.displayAlarmClock(); return 0; }
Explanation / Answer
Alarm Clock:
This is a source code for alarm clock in C++. In this we have used a class named 'clok' as shown in the source code given below. In this class we have used three private elements one for hour , second for minute , third for second . There are some member functions used in this class for the initialisation and to store the values of time. The source code for the same is given below you can check it out.
Programm:
# include <iostream.h>
# include <conio.h>
# include <time.h>
# include <dos.h>
class clok
{
private:
int hh,mm,ss;
public:
void get();
void display();
void pass(tm *a);
void alarm(clok &c);
};
void clok::get()
{
e: cin>>hh>>mm>>ss;
if(((hh>23)&&(hh<0))||((mm>59)&&(mm<0))||((ss>59)&&(ss<0)))
{
cout<<"n you have entered invalid time ";
cout<<"n please try again";
goto e;
}
}
void clok::display()
{
cout<<hh<<":"<<mm<<":"<<ss;
}
void clok::pass(tm *a)
{
hh = a->tm_hour;
mm = a->tm_min;
ss = a->tm_sec;
}
void clok::alarm(clok &c)
{
clok d;
long int e;
int i;
d.hh = hh-c.hh;
d.mm = mm-c.mm;
d.ss = ss-c.ss;
e = (d.hh*3600)+(d.mm*60)+(d.ss);
for(i=e;i>=1;i--)
{
delay(1000);
}
for(i=0;i<4;i++)
{
cout<<" alarm!!!! aa";
delay(1000);
}
}
void main()
{ int a;
clok c1,c2;
time_t t;
tm *area;
clrscr();
time(&t);
area = localtime(&t);
clrscr();
cout<<"current time is: "<<asctime(area);
cout<<" set the time of alarm ";
c1.get();
c1.display();
c2.pass(area);
c1.alarm(c2);
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.