重载单目运算
今天就只写程序吧,把所有的程序写好,因为这一章讲的基本上没什么大的差别,只是程序上不同,重载的运算符不同。所以今天晚上就直接把所有的程序列出来进入下一章。
重载单目i++和++i:
#include <iostream>
using namespace std;
class Time
{
public:
Time(){ minute = 0; second = 0; }
Time(int m, int s);
Time operator ++ ();
Time operator ++ (int);
void display();
private:
int minute;
int second;
};
Time::Time(int m, int s)
{
minute = m;
second = s;
}
Time Time :: operator++ ( )//前置++
{
if (++second >=60)
{
second -= 60;
++minute;
}
return *this;
}
Time Time::operator++(int)
{
Time t (*this);
second++;
if (second>=60)
{
second -= 60;
++minute;
}
return t;
}
void Time::display()
{
cout << minute << ":" << second << endl;
}
int main()
{
Time t1(23, 59), t2;
cout << "t1" << ":";
t1.display();
++t1;
t1.display();
t2=t1++;
t1.display();
t2.display();
system("pause");
return 0;
}
下面一章是关于继承的,暂时告一段落,改写C语言的理解,因为这些足够解决一般的问题了。