//C++中的运算符重载——“+”“-”“++”“--”:
//(本源码为《面向对象C++程序设计(皮德常编著)(清华大学出版社)》165页第一题参考答案)
#ifndef NUMDAYS_H
#define NUMDAYS_H
/*
name:Target_MIT
NUAA
*/
class NumDays//将小时为单位的工作时间转化为天数
{
private:
float hours;
float day;
public:
NumDays(float ho)
{
hours=ho;
day=hours/8;
}
NumDays()
{hours=day=0;
}
void PUT()
{cout<<"hours="<<hours<<endl;
cout<<"day="<<day<<endl;
}
NumDays operator+(const NumDays &right);
NumDays operator-(const NumDays &right);
NumDays operator++();
NumDays operator++(int);
NumDays operator--();
NumDays operator--(int);
};
#endif
NumDays NumDays::operator+(const NumDays &right)
{
NumDays Temp;
Temp=hours+right.hours;
Temp.day=Temp.hours/8;
return Temp;
}
NumDays NumDays::operator-(const NumDays &right)
{
NumDays Temp;
Temp=hours-right.hours;
Temp.day=Temp.hours/8;
return Temp;
}
NumDays NumDays::operator++()
{
++hours;
day=hours/8;
return *this;
}
NumDays NumDays::operator++(int)
{
NumDays Temp(hours);
cout<<"Temp:_";Temp.PUT();
hours++;
return Temp;
}
NumDays NumDays::operator--()
{
--hours;
day=hours/8;
return *this;
}
NumDays NumDays::operator--(int)
{
NumDays Temp(hours);
cout<<"Temp:_";Temp.PUT();
hours--;
return Temp;
}