高手来看看 C++程序问题

来源:百度知道 编辑:UC知道 时间:2024/09/22 01:56:06
#include <iostream.h>
class TIME{
private:
int hour;
int minute;
int second;
public:
TIME(int h=0,int m=0,int s=0)
{
hour=h;
minute=m;
second=s;
}
TIME(const TIME& other)
{
hour=other.hour;
minute=other.minute;
second=other.second;
}
void set()
{
cout<<"please set the time!"<<endl;
cin>>hour;
cout<<":"<<endl;
cin>>minute;
cout<<":"<<endl;
cin>>second;
}
TIME operator ++();
TIME operator --();
void show()
{
cout<<"The time now is:"<<"\n"<<hour<<":"<<minute<<":"<<second;
}
};
TIME TIME::operator ++()
{

second++;
if(second==60)
{
second=0;
minute++;

没写using namespace std;
还有++是后置的话,在重载是要有参数。
请看例子
void Clock::operator ++() //前置单目运算符重载函数
{
Second++;
if(Second>=60)
{
Second=Second-60;
Minute++;
if(Minute>=60)
{
Minute=Minute-60;
Hour++;
Hour=Hour%24;
}
}
cout<<"++Clock: ";
}
void Clock::operator ++(int) //后置单目运算符重载
{ //注意形参表中的整型参数
Second++;
if(Second>=60)
{
Second=Second-60;
Minute++;
if(Minute>=60)
{
Minute=Minute-60;
Hour++;
Hour=Hour%24;
}
}
cout<<"Clock++: ";
}

你可以先定义前置++,然后后置++z这样写
Colck Clock ::operator ++ (int)
{Colck old=*this;
++(*this);
return old;}

你用的是什么编译器呀 我用的是Microsoft Visual Studio 2008

#include <iostream.h>
改成
#inc