Bài tập C++ OOP: tạo lớp Time để biểu diễn thời gian theo yêu cầu | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create Time class in Time.cpp file with the following information:

Explanation:

  • Time class stores the information about hours, minutes and seconds.
  • hour, minute, second are the attributes that describe the hours, minutes and seconds of Time class.
  • Time(hour: int, minute: int, second: int) is parameterized constructor used to initialize values to attributes.
  • setTime(hour: int, minute: int, second: int) method changes the values of the following attributes hour, minute, second.
  • nextSecond() method increases the time of object by 1 second. For example:
    • Values of attributes after and before calling nextSecond() method: hour = 7, minute = 7, second = 40 and hour = 7, minute = 7, second = 41.
    • Values of attributes after and before calling nextSecond() method: hour = 23, minute = 59, second = 59 and hour = 0, minute = 0, second = 0.
    • Values of attributes after and before calling nextSecond() method: hour = 7, minute = 59, second = 59 and hour = 8, minute = 0, second = 0.
  • previousSecond() method decreases the time of object by 1 second. For example:
    • Values of attributes after and before calling previousSecond() method: hour = 7, minute = 7, second = 7 and hour = 7, minute = 7, second = 6.
    • Values of attributes after and before calling previousSecond() method: hour = 0, minute = 0, second = 0 and hour = 23, minute = 59, second = 59.
  • display() method displays the time information of object with format "HH:mm:ss". For example:
    • For hour = 1, minute = 8, second = 45, after calling display() method, the text "01:08:45" will display on the screen.
    • For hour = 23, minute = 5, second = 7, after calling display() method, the text "23:05:07" will display on the screen.
  • The other methods are setter and getter.

A program to test Time class:

#include "Time.cpp"

using namespace std;

int main() {
	Time t(12, 1, 1);
	t.display();
	t.setTime(23, 59, 59);
	t.display();
	t.nextSecond();
	t.display();
	t.nextSecond();
	t.display();
	t.setTime(7, 0, 0);
	t.display();
	t.previousSecond();
	t.display();
	return 0;
}

When the above code is compiled and executed, it produces the following result:

12:01:01
23:59:59
00:00:00
00:00:01
07:00:00
06:59:59

Instruction

You have to create a Time class as below:

#include<iostream>
#include<string>

using namespace std;

class Time {
private:
	int hour;
	int minute;
	int second;

public:
	Time(int hour, int minute, int second) {
		this->hour = hour;
		this->minute = minute;
		this->second = second;
	}

	void setTime(int hour, int minute, int second) {
		this->hour = hour;
		this->minute = minute;
		this->second = second;
	}

	int getHour() {
		return hour;
	}

	void setHour(int hour) {
		this->hour = hour;
	}

	int getMinute() {
		return minute;
	}

	void setMinute(int minute) {
		this->minute = minute;
	}

	int getSecond() {
		return second;
	}

	void setSecond(int second) {
		this->second = second;
	}

	void previousSecond() {
		if (hour == 0 && minute == 0 && second == 0) {
			hour = 23;
			minute = 59;
			second = 59;
			return;
		}
		if (minute == 0 && second == 0) {
			hour--;
			minute = 59;
			second = 59;
			return;
		}
		if (second == 0) {
			minute--;
			second = 59;
			return;
		}
		second--;
	}

	void nextSecond() {
		if (hour == 23 && minute == 59 && second == 59) {
			hour = 0;
			minute = 0;
			second = 0;
			return;
		}
		if (minute == 59 && second == 59) {
			hour++;
			minute = 0;
			second = 0;
			return;
		}
		if (second == 59) {
			minute++;
			second = 0;
			return;
		}
		second++;
	}

	void display() {
		string hour = to_string(this->hour);
		string minute = to_string(this->minute);
		string second = to_string(this->second);
		if (hour.length() == 1)
			hour = "0" + hour;
		if (minute.length() == 1)
			minute = "0" + minute;
		if (second.length() == 1)
			second = "0" + second;
		cout << hour + ":" << minute + ":" + second << endl;
	}
};