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

Add a default constructor and a parameterized constructor (with three parameters

ID: 3757836 • Letter: A

Question


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 <iostream>

#include <ctime>

using namespace std;

int main ()

{

time_t rawtime;

struct tm *info;

info = new tm;

time( &rawtime );

info = localtime( &rawtime );

cout<<asctime(info)<<endl;

cout<tm_hour<<endl;

return(0);

}

This is my last program!!! Needs to inherited!!! using c++

#include <string>

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

___________________________________________________________________________________________

#include <ctime>
#include <iostream>
#include <string>
#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;
}

_____________________________________________________________________________________________

#include "AlarmClock.h"

#include <iostream>

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

Default and parameterised constructors: To be added to header file AlarmClock.h.

//Constructors
   /*
   * Default constructor.
   * Sets the alarm as default alarm to the start of the day i.e. 12:00 A.M.
   */
   AlarmClock()
   {
       alarmHour=12;
       alarmMinute=0;
       meridian="AM";
   }

   /*
   * Parameterised constructor
   * Sets the alarm to specified time.
   */
   AlarmClock(int hour,int minute,string mer)
   {
       alarmHour=hour;
       alarmMinute=minute;
       meridian=mer;
   }

Inheritance:

1. First change the access-qualifier of AlarmClock's data members from "private" to "public".

2. Derived ExtAlarmClock class

ExtAlarmClock.h

#ifndef EXTALARMCLOCK_H_
#define EXTALARMCLOCK_H_

#include "alarmclock.h"

#define PST "Pacific Time"
#define CST "Central Time"

class ExtAlarmClock: public AlarmClock {
private:
   string time_zone;
public:
   //constructors
   /*
   * Default constructor: sets default alarm in default zone i.e. CST
   */
   ExtAlarmClock():AlarmClock()
   {
       time_zone=CST;
   }

   /*
   * Parameterised Constructor
   */
   ExtAlarmClock(int hour, int minute,string mer, string zone):AlarmClock(hour,minute,mer)
   {
      time_zone=zone;
   }

   //override setAlarmClock
   void setAlarmClock(int h, int m, string mer, string zone);
   //override displayAlarmClock
   void displayAlarmClock() const;
};

#endif /* EXTALARMCLOCK_H_ */

ExtAlarmClock.cpp

#include <iostream>
#include <string>
#include <stdlib.h>
#include "ExtAlarmClock.h"

using namespace std;

void ExtAlarmClock::setAlarmClock(int h, int m, string mer, string zone)
{
   this->alarmHour = h;
   this->alarmMinute = m;
   this->meridian = mer;
   this->time_zone=zone;
}

void ExtAlarmClock::displayAlarmClock() const
{
   time_t rawtime;
   struct tm *info;
   info = new tm;
   if(this->time_zone.compare(PST)==0)
   {
       putenv("TZ=PST");
       tzset();
       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 << " " << this->time_zone << 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;
   }
   else if (this->time_zone.compare(CST)==0)
   {
       putenv("TZ=CST");
       tzset();
       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 << " " << this->time_zone << 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;
   }
}

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