Answer to Question #346525 in C++ for rashid

Question #346525

 Create a class named TIME that has hours, minutes, and seconds, data members, as integers. The class has gettime () to get the specified value in the object, showtime () to display the time object in hh:mm:ss format. Write a main () function to create two-time objects. get the value in two objects and display all time objects.


1
Expert's answer
2022-05-31T10:03:56-0400
#include <iostream>

using namespace std;

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

    public:
    TIME(int hours1 = 1, int minutes1 = 1, int seconds1 = 1):
    hours(hours1), minutes(minutes1), seconds(seconds1) {}

    int gettime() {
        int command;
        cout << "Choose option:" << endl;
        cout << "1 - hours" << endl;
        cout << "2 - minutes" << endl;
        cout << "3 - seconds" << endl;
        cin >> command;
        switch(command) {
            case 1:
            {
                cout << "hours: " << hours << endl;
                return hours;
            }
            case 2:
            {
                cout << "minutes: " << minutes << endl;
                return minutes;
            }
            case 3:
            {
                cout << "seconds: " << seconds << endl;
                return seconds;
            }
        }
        return 0;
    }

    void showtime() {
        if (hours / 10 < 1) {
            cout << "0" << hours << ":";
        }
        else {
            cout << hours << ":";
        }
        if (minutes / 10 < 1) {
            cout << "0" << minutes << ":";
        }
        else {
            cout << minutes << ":";
        }
        if (seconds / 10 < 1) {
            cout << "0" << seconds << endl;
        }
        else {
            cout << seconds << endl;
        }
    }
};

int main() {
    TIME first(1, 2, 30);
    TIME second(2, 6, 10);

    int hours1 = first.gettime();
    int minutes1 = first.gettime();
    int seconds1 = first.gettime();

    cout << "TIME 1:" << endl;
    cout << hours1 << " " << minutes1 << " " << seconds1 << endl;
    first.showtime();

    int hours2 = second.gettime();
    int minutes2 = second.gettime();
    int seconds2 = second.gettime();

    cout << "TIME 2:" << endl;
    cout << hours2 << " " << minutes2 << " " << seconds2 << endl;
    second.showtime();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS