📄 6_8.cpp
字号:
#include<iostream.h>
class clock //时钟类的定义
{
public: //公有成员函数
int& SetTime(int h=0, int m=0, int s=0);
void ShowTime();
private: //私有数据成员
int hour;
int minute;
int second;
};
//时钟类的实现
int& clock::SetTime(int h, int m, int s)//返回整型引用
{
hour=h>=0&&h<24?h:0;//过滤非法数据
minute=m>=0&&m<60?m:0;
second=s>=0&&s<60?s:0;
return(hour);
}
void clock::ShowTime()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
//主函数
int main()
{
clock s;
cout<<"第一次设置时间并显示:"<<endl;
int &r=s.SetTime(); //定义整型引用,成为hour的别名
s.ShowTime();
cout<<"第二次设置时间并显示:"<<endl;
r=7;//修改hour的值为7
s.ShowTime();
cout<<"第三次设置时间并显示:"<<endl;
s.SetTime()=8;//修改hour的值为8
s.ShowTime();
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -