1.time類保存在“htime.h”中,要求:
⑴ 數據成員包含時(hour)、分(minute)、秒(second),爲私有成員;
⑵ 能給數據成員提供值的成員函數(默認值爲0時0分0秒);
⑶ 能分別取時、分、秒;
⑷ 能輸出時、分、秒(用“:”分隔),並顯示上午(am)或下午(pm);
⑸ 有默認值的構造函數(默認值爲0時0分0秒)。
說明:成員函數均定義爲公有成員。
2.編寫一個測試time類的main()函數(存放在exp_104.cpp)中。要求:
⑴ 定義對象、對象指針、對象的引用;
⑵ 用輸入的值設置時間;
⑶ 用輸出時、分、秒的成員函數顯示時間;
⑷ 用取時、分、秒的成員函數以“ 時 分 秒”的格式顯示時間;
⑸ 分別用對象、對象指針、對象的引用調用成員函數。
#ifndef Time_htime_h
#define Time_htime_h
#include<iostream>
using namespace std;
class Time
{
public:
Time(int h = 0,int m = 0,int s = 0)
{
hour = h;
minute = m;
second = s;
}
~Time(){}
void set_time(int h,int m,int s)
{
hour = h;
minute = m;
second = s;
}
int get_hour()
{
return hour;
}
int get_second()
{
return second;
}
int get_minute()
{
return minute;
}
void ptint()
{
if (hour <12 && hour > 0)
{
cout<<"pm ";
}
else
cout<<"am ";
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
private:
int hour;
int minute;
int second;
};
#endif
#include "htime.h"
int main()
{
Time T;
Time *P;
Time &S = T;
P = &T;
T.set_time(13, 56, 33);
cout<<"hour:"<<S.get_hour()<<endl;
cout<<"minute:"<<S.get_minute()<<endl;
cout<<"second:"<<S.get_second()<<endl;
P->ptint();
return 0;
}