Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Time{
- private:
- // 資料(變數部分)
- int hour;
- int minute;
- int second;
- public:
- // 功能(函式部分)
- // Constructor(建構式)
- Time(){
- hour = 0;
- minute = 0;
- second = 0;
- }
- Time(int h, int m, int s){
- setHour(h);
- setMinute(m);
- setSecond(s);
- }
- // Set/Get Method
- void setHour(int h){
- if( h >= 0 && h <= 23 ){
- hour = h;
- }
- else{
- cout << "錯誤訊息:小時設定錯誤" << endl;
- hour = 0; // 設定錯誤時,復原成預設值。
- }
- }
- void setMinute(int m){
- if( m >= 0 && m <= 59 ){
- minute = m;
- }
- else{
- cout << "錯誤訊息:分鐘設定錯誤" << endl;
- minute = 0; // 設定錯誤時,復原成預設值。
- }
- }
- void setSecond(int s){
- if( s >= 0 && s <= 59 ){
- second = s;
- }
- else{
- cout << "錯誤訊息:秒數設定錯誤" << endl;
- second = 0; // 設定錯誤時,復原成預設值。
- }
- }
- int getHour(){
- return hour;
- }
- int getMinute(){
- return minute;
- }
- int getSecond(){
- return second;
- }
- void print(){
- cout << hour << "時" << minute << "分" << second << "秒" << endl;
- }
- int diff(Time t2){
- return (hour - t2.hour) * 3600 + (minute - t2.minute) * 60 + (second - t2.second);
- }
- int cmpGreater(Time t2){
- if( hour > t2.hour ){
- return 1;
- }
- else if( hour < t2.hour){
- return 0;
- }
- else if( minute > t2.minute ){
- return 1;
- }
- else if( minute < t2.minute){
- return 0;
- }
- else if( second > t2.second ){
- return 1;
- }
- else{
- return 0;
- }
- }
- };
- int main(){
- //Time t1 = {9, 90000, 58};
- Time t1(9, 90000, 58);
- Time t2;
- /*t2.hour = 12;
- t2.minute = 800;
- t2.second = 0;
- */
- t2.setHour(12);
- t2.setMinute(800);
- t2.setSecond(0);
- t1.print();
- t2.print();
- cout << t2.diff(t1) << endl;
- cout << t2.cmpGreater(t1) << endl;
- cout << t1.getHour() << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment