zoukankan      html  css  js  c++  java
  • [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;
    }
    




  • 相关阅读:
    改变对象的字符串提示
    perl 和 python中的回调函数
    shiro权限验证标签
    user_tables 的信息依赖于统计信息
    centos jdk 1.7升级到1.8后显示还是1.7
    7.1 可接受任意数量参数的函数:
    IntelliJ IDEA中怎么查看文件中所有方法(类似eclipse里面的outline)
    Intellij IDEA 代码格式化与eclipse保持风格一致
    jquery-1.9.1.min.js:69 Uncaught TypeError: Illegal invocation
    python json模块
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7246834.html
Copyright © 2011-2022 走看看