`

运算符前置++和后置++重载为时钟类的成员函数

阅读更多

源程序:

#include<iostream.h>
class Clock	                  //时钟类声明
{
public:	                      //外部接口
	Clock(int NewH=0, int NewM=0, int NewS=0);
	void ShowTime();
	Clock& operator ++();     //前置单目运算符重载
	Clock operator ++(int);   //后置单目运算符重载
private:                      //私有数据成员
	int Hour, Minute, Second;
};

Clock::Clock(int NewH, int NewM, int NewS)
{
	if (0<=NewH && NewH<24 &&0<=NewM && NewH<60 &&0<=NewS && NewS<60)
	{
		Hour=NewH;
		Minute=NewM;
		Second=NewS;
	}
	else 
		cout << "Tine error!"<<endl;
}

void Clock::ShowTime()
{
	cout << Hour <<":"<< Minute<<":"<<Second<<endl;
}

Clock& Clock::operator ++()  //前置单目运算符重载函数
{
	Second++;
	if(Second>=60)
    {     Second=Second-60;
		Minute++;
		if(Minute>=60)
           {	Minute=Minute-60;
			Hour++;
			Hour=Hour%24;
		}
	}
	return *this;
}

Clock Clock::operator ++(int)    //后置单目运算符重载
{	
Clock old=*this;
++(*this);
return old;
}

void main()
{
	Clock myClock(23,59,59);
	cout<<"First time output:";
	myClock.ShowTime();
    cout<<"Show myclock++:";
	(myClock++).ShowTime();
    cout<<"Show ++myclock:";
	(++myClock).ShowTime();
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics