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

來源:互聯網
上載者:User

標籤: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++]對象指標,引用的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.