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




posted @ 2017-07-27 19:57  wzzkaifa  阅读(333)  评论(0编辑  收藏  举报