[c++]對象指針,引用的操作

1.time類保存在“htime.h”中,要求:

數據成員包含時(hour)、分(minute)、秒(second),爲私有成員;

能給數據成員提供值的成員函數(默認值爲000秒);

能分別取時、分、秒;

能輸出時、分、秒(用分隔),並顯示上午(am)或下午(pm);

有默認值的構造函數(默認值爲000秒)。

說明:成員函數均定義爲公有成員。

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;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章