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.
#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();
}
Comments
Leave a comment