牛骨文教育服务平台(让学习变的简单)
博文笔记

[c++]对象指针,引用的操作

创建时间:2015-06-03 投稿人: 浏览次数:729

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




声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。