<< Chapter < Page Chapter >> Page >

12: //Các hàm get

13: int GetHour();    //Trả về Hour

14: int GetMinute();    //Trả về Minute

15: int GetSecond();    //Trả về Second

16:

17: void PrintMilitary(); //Xuất thời gian theo dạng giờ quânđội

18: void PrintStandard(); //Xuất thời gian theo dạng chuẩn

19:

20: private:

21: int Hour; //0 - 23

22: int Minute; //0 - 59

23: int Second; //0 – 59

24: };

25:

26: //Constructor khởiđộng dữ liệu private

27: //Gọi hàm thành viên SetTime() để thiết lập các biến

24: //Các giá trị mặc định là 0

25: Time::Time(int Hr, int Min, int Sec)

26: {

27: SetTime(Hr, Min, Sec);

28: }

29:

30: //Thiết lập các giá trị của Hour, Minute, và Second

31: void Time::SetTime(int H, int M, int S)

32: {

33: Hour = (H>= 0&&H<24) ? H : 0;

34: Minute = (M>= 0&&M<60) ? M : 0;

35: Second = (S>= 0&&S<60) ? S : 0;

36: }

37:

38: //Thiết lập giá trị của Hour

39: void Time::SetHour(int H)

40: {

41: Hour = (H>= 0&&H<24) ? H : 0;

42: }

43:

44: //Thiết lập giá trị của Minute

45: void Time::SetMinute(int M)

46: {

47: Minute = (M>= 0&&M<60) ? M : 0;

48: }

49:

50: //Thiết lập giá trị của Second

51: void Time::SetSecond(int S)

52: {

53: Second = (S>= 0&&S<60) ? S : 0;

54: }

55:

56: //Lấy giá trị của Hour

57: int Time::GetHour()

58: {

59: return Hour;

60: }

61:

62: //Lấy giá trị của Minute

63: int Time::GetMinute()

64: {

65: return Minute;

66: }

67:

68: //Lấy giá trị của Second

69: int Time::GetSecond()

70: {

71: return Second;

72: }

73:

74: //Hiển thị thời gian dạng giờ quânđội: HH:MM:SS

75: void Time::PrintMilitary()

76: {

77: cout<<(Hour<10 ? "0" : "")<<Hour<<":"

78:      <<(Minute<10 ? "0" : "")<<Minute<<":"

79:      <<(Second<10 ? "0" : "")<<Second;

80: }

81:

83: //Hiển thị thời gian dạng chuẩn: HH:MM:SS AM (hay PM)

84: void Time::PrintStandard()

85: {

86: cout<<((Hour == 0 || Hour == 12) ? 12 : Hour % 12)<<":"

87:      <<(Minute<10 ? "0" : "")<<Minute<<":"

88:      <<(Second<10 ? "0" : "")<<Second

89:      <<(Hour<12 ? " AM" : " PM");

90: }

91:

92: void IncrementMinutes(Time&, const int); //prototype

93:

94: int main()

95: {

96: Time T;

97:

99: T.SetHour(17);

100: T.SetMinute(34);

101: T.SetSecond(25);

102 cout<<"Result of setting all valid values:"<<endl

103:      <<" Hour: "<<T.GetHour()

104:      <<" Minute: "<<T.GetMinute()

105:      <<" Second: "<<T.GetSecond()<<endl<<endl;

106: T.SetHour(234); //Hour không hợp lệđược thiết lập bằng 0

107: T.SetMinute(43);

108: T.SetSecond(6373); //Second không hợp lệđược thiết lập bằng 0

109: cout<<"Result of attempting to set invalid Hour and"

110:      <<" Second:"<<endl<<" Hour: "<<T.GetHour()

111:      <<" Minute: "<<T.GetMinute()

112:      <<" Second: "<<T.GetSecond()<<endl<<endl;

113: T.SetTime(11, 58, 0);

114: IncrementMinutes(T, 3);

115: return 0;

116: }

117:

118: void IncrementMinutes(Time&TT, const int Count)

119: {

120: cout<<"Incrementing Minute "<<Count

121:      <<" times:"<<endl<<"Start time: ";

122: TT.PrintStandard();

123: for (int I = 1; I<= Count; I++)

124: {

125: TT.SetMinute((TT.GetMinute() + 1) % 60);

126: if (TT.GetMinute() == 0)

127: TT.SetHour((TT.GetHour() + 1) % 24);

128: cout<<endl<<"Minute + 1: ";

129: TT.PrintStandard();

130: }

131: cout<<endl;

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Lập trình hướng đối tượng. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10794/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Lập trình hướng đối tượng' conversation and receive update notifications?

Ask