Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

C++ program for the implementation of time class to reset

+11 votes
asked Feb 12, 2020 by anonymous

1 Answer

0 votes
answered Nov 2, 2024 by Niraj (140 points)
#include <iostream>
#include <iomanip>

class Time {
private:
    int hours;
    int minutes;
    int seconds;

public:
    // Constructor with default values set to 0
    Time(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {}

    // Method to display time in HH:MM:SS format
    void display() const {
        std::cout << std::setw(2) << std::setfill('0') << hours << ":"
                  << std::setw(2) << std::setfill('0') << minutes << ":"
                  << std::setw(2) << std::setfill('0') << seconds << std::endl;
    }

    // Method to reset time to specific values or to 00:00:00 by default
    void reset(int h = 0, int m = 0, int s = 0) {
        hours = h;
        minutes = m;
        seconds = s;
    }
};

int main() {
    // Create a Time object with default time (00:00:00)
    Time time;

    // Display the initial time
    std::cout << "Initial time: ";
    time.display();

    // Reset time to 10:30:25
    time.reset(10, 30, 25);
    std::cout << "After reset to 10:30:25: ";
    time.display();

    // Reset time to 00:00:00
    time.reset();
    std::cout << "After reset to 00:00:00: ";
    time.display();

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...