C++高手请进,谢谢急用

来源:百度知道 编辑:UC知道 时间:2024/06/05 21:45:08
#include<iostream.h>
#include<stdlib.h>
class Time
{ private:

int minute;
protected:
int second;
public:
int hour;
Time(int h,int m,int s):hour(h),minute(m),second(s){}
void display();
};
class Time1: public Time
{ private:
int year,month,day;
public:
Time1(int h,int m,int s,int y,int mon,int d):Time(h,m,s),year(y),month(mon),day(d){}
void display1();
};
void Time::display()
{cout<<hour<<'/'<<minute<<'/'<<second<<endl;
}
void Time1::display1()
{ cout<<year<<'/'<<month<<'/'<<day<<'/';
display();

}
int main()
{Time t1(0,0,0);
t1.display();
Time1 t2(1,1,1,2,2,2);
t2.display1();
cout<<t2.hour ;
system("pause");
return 0;
}
这个程序的倒数第四行"cout&l

因为输出endl会强制将缓冲区清空,它相当于flush加换行;如果你的输出未占满缓冲区的话,系统将等到缓冲区满或出现其他流操作或程序结束时才输出。

不加<<endl;可以显示,当出现“请按任意键继续…”,你随便按个键,就可以出现t2.hour的值为1,然后再出现“Press any……”。
问题应该出现在system("pause")上。具体怎样我也不知道。

在system("pause")前加上cout<<flush;
在系统暂停前输出缓冲区数据。。