問題 C B C 時間類的運算子過載

2022-06-28 08:42:14 字數 1957 閱讀 5947

c++時間類的運算子過載

定義乙個時間類time,其資料成員為表示時間的小時(hour)、分(minute),秒(second)。

過載運算子「+」,使之能用於時間物件的加法運算;過載運算子「<<」,使之能用於時間物件的輸出操作。

(1)參加運算的兩個運算元可以都是時間類物件,也可以其中有乙個是整數(表示秒),順序任意。

例如,t1+t2,i+t1,t1+i均合法(設i為整數表示秒數, t1,t2為時間類time物件)。

(2)輸出時間物件的方式為 小時:分:秒,例如 14:23:55、00:00:00、23:59:59等。

編寫程式,分別求兩個時間物件之和、整數和時間物件之和,時間物件和整數之和,並且輸出。

請在下面的程式段基礎上完成設計:

#include

using namespace std;

class time

time(int h,int m,int s)

time operator+(time &);

time operator+(int &);

friend time operator+(int,time &);

friend ostream& operator << (ostream& output, time & c);

private:

int hour;

int minute;

int second;

};//將程式需要的其他成份寫在下面,只提交begin到end部分的**

//******************** begin ********************

//********************* end ********************

int main()

乙個時間類的小時 分 秒,另乙個時間類的小時 分 秒(注意要符合實際)

乙個時間類的小時 分 秒,乙個整數(表示秒數)

乙個整數(表示秒數),乙個時間類的小時 分 秒

兩個時間之和、時間和整數之和,整數和時間之和。

1 2 3 4 5 6

0 0 0 200

59 14 59 1

t1+t2=5:7:9

t1+i=0:3:20

i+t1=15:0:0

只提交自己定義的函式部分

#include using namespace std;

class time

time(int h,int m,int s)

time operator+(time &);

time operator+(int &);

friend time operator+(int,time &);

friend ostream& operator << (ostream& output, time & c);

private:

int hour;

int minute;

int second;

};//將程式需要的其他成份寫在下面,只提交begin到end部分的**

//******************** begin ********************

time time::operator+(time &t)

} }return tmp;

}time time::operator+(int &t)

} }

return tmp;

}time operator + (int s, time &tmp)

} }

return tmp;

}ostream & operator << (ostream &output, time & tmp)

//********************* end ********************

int main()

YTU OJ C 時間類的運算子過載

time limit 1 sec memory limit 128 mb submit 187 solved 58 submit status web board c 時間類的運算子過載 定義乙個時間類time,其資料成員為表示時間的小時 hour 分 minute 秒 second 過載運算子 使...

時間類的改進(運算子過載)

問題描述 主函式設計如下,請勿修改 int main 樣例輸出 t1 t2 15 57 34 t1 65 02 35 05 65 t1 02 35 05 t2 t1 10 49 34 t1 70 02 32 50 include include include using namespace std...

運算子過載 類的賦值運算子過載

下面介紹類的賦值運算子 1.c 中物件的記憶體分配方式 在c 中,物件的例項在編譯的時候,就需要為其分配記憶體大小,因此,系統都是在stack上為其分配記憶體的。這一點和c 完全不同!千 萬記住 在c 中,所有類都是reference type,要建立類的實體,必須通過new在heap上為其分配空間...