標籤:c++ class 類 對象
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;}
[c++]對象指標,引用的操作