?? 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 + -