我想請問,現在我有三個時間物件要相加,
物件相加使用的是operator+的方式,
請問const simpletime &st的目的是?
而我有三個物件,所以這三個物件會如何相加呢?
simpletime simpletime::operator+(const simpletime &st)
{
int hour_buf, minute_buf, total;
total = (hour*60+minute) + (st.hour*60+st.minute);
hour_buf = total / 60;
minute_buf = total % 60;
return simpletime(hour_buf, minute_buf);
}
int main()
{
simpletime st1(5, 30);
simpletime st2(2, 50);
simpletime st3(4, 20);
simpletime st4;
st4 = st1 + st2+st3;
cout << st4.hour << "時" << st4.minute << "分" << endl;
system("pause");
return 0;
}
附上完整程式碼:
#include <iostream>
using namespace std;
class simpletime{
public:
simpletime(){};
simpletime(int h, int m){
hour = h;
minute = m;
}
simpletime operator+(const simpletime &st);
int hour;
int minute;
};
simpletime simpletime::operator+(const simpletime &st)
{
int hour_buf, minute_buf, total;
total = (hour*60+minute) + (st.hour*60+st.minute);
hour_buf = total / 60;
minute_buf = total % 60;
return simpletime(hour_buf, minute_buf);
}
int main()
{
simpletime st1(5, 30);
simpletime st2(2, 50);
simpletime st3(4, 20);
simpletime st4;
st4 = st1 + st2+st3;
cout << st4.hour << "時" << st4.minute << "分" << endl;
return 0;
}
請問這方面網路上有參考資料可參考嗎?
謝謝!