Programming Project 10.13.1: ••. Implement a class Clock whose get_hours and get_minutes member functions return the current time at your location. To get the current time, use the following code, which requires that you include the header: time_t current_time = time(0); tm* local_time = localtime(&current_time); int hours = local_time->tm_hour; int minutes = local_time->tm_min; Also provide a get_time member function that returns a string with the hours and minutes by calling the get_hours and get_minutes functions. Provide a derived class WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show the time in New York, three time zones ahead. Which member functions did you override? (You should not override get_time.)

Report
Question

Please briefly explain why you feel this question should be reported.

Report
Cancel
Programming Project 10.13.1: ••.

Implement a class Clock whose get_hours and get_minutes member functions return the current time at your location. To get the current time, use the following code, which requires that you include the <ctime> header:

time_t current_time = time(0);
tm* local_time = localtime(&current_time);
int hours = local_time->tm_hour;
int minutes = local_time->tm_min;

Also provide a get_time member function that returns a string with the hours and minutes by calling the get_hours and get_minutes functions. Provide a derived class WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show the time in New York, three time zones ahead. Which member functions did you override? (You should not override get_time.)

MathJax Example

Answer ( 1 )

  1. Please briefly explain why you feel this answer should be reported.

    Report
    Cancel

    Code
    #include <iostream>
    #include <ctime>
    #include <string>

    using namespace std;

    // Base Clock class
    class Clock {
    private:
    bool alarm_on = false; // Tracks if the alarm is active
    int alarm_hours; // Stores the alarm hour
    int alarm_minutes; // Stores the alarm minute

    public:
    // Default constructor
    Clock() {}

    // Virtual function to get the current hour
    virtual int get_hours() {
    time_t current_time = time(0); // Get current time
    tm* local_time = localtime(&current_time); // Convert to local time
    return local_time->tm_hour; // Return current hour
    }

    // Virtual function to get the current minute
    virtual int get_minutes() {
    time_t current_time = time(0); // Get current time
    tm* local_time = localtime(&current_time); // Convert to local time
    return local_time->tm_min; // Return current minute
    }

    // Function to get the current time as a formatted string
    string get_time() {
    int hours = get_hours(); // Get current hour
    int minutes = get_minutes(); // Get current minute

    // Format the time as a string
    string time_str = to_string(hours) + “:” + (minutes < 10 ? “0” : “”) + to_string(minutes);

    // Check if the alarm should trigger
    if (alarm_on && alarm_hours <= hours && alarm_minutes <= minutes) {
    time_str += ” Alarm”; // Append “Alarm” to the time string
    alarm_on = false; // Turn off the alarm
    }

    return time_str; // Return the formatted time string
    }

    // Function to set the alarm
    void set_alarm(int hours, int minutes) {
    alarm_on = true; // Turn on the alarm
    alarm_hours = hours; // Set alarm hour
    alarm_minutes = minutes; // Set alarm minute
    }
    };

    // Derived WorldClock class
    class WorldClock : public Clock {
    private:
    int time_offset; // Stores the time offset in hours

    public:
    // Constructor to set the time offset
    WorldClock(int offset) : time_offset(offset) {}

    // Override the get_hours method to include the time offset
    int get_hours() override {
    int hours = Clock::get_hours(); // Get base hours
    return (hours + time_offset) % 24; // Apply offset and ensure it stays within 24 hours
    }

    // No need to override set_alarm or get_minutes, as they work as expected
    };

    // Driver function to test the code
    int main() {
    Clock clk; // Create a base Clock object
    WorldClock wclk(5); // Create a WorldClock object with a 5-hour offset

    // Display current time for both clocks
    cout << “Local Time: ” << clk.get_time() << endl;
    cout << “World Time (+5 hours): ” << wclk.get_time() << endl;

    // Set an alarm on the WorldClock
    wclk.set_alarm(5, 15);
    cout << “World Time with Alarm: ” << wclk.get_time() << endl;

    return 0;
    }
    Output
    Local Time: 14:35
    World Time (+5 hours): 19:35
    World Time with Alarm: 19:35 Alarm

Leave an answer

Browse

By answering, you agree to the Terms of Service and Privacy Policy.