首页 > 编程语言 > 详细

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

时间:2015-06-03 17:37:18      阅读:235      评论:0      收藏:0      [点我收藏+]

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




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

原文:http://blog.csdn.net/cherry_ermao/article/details/46348431

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!