第二周 项目三 时间类(一)

本文详细介绍了如何在已有时间类的基础上,通过增加成员函数来实现增加秒、分钟和小时的功能,并提供了完整的代码实现及运行结果。通过实例展示了类和对象的应用,以及如何在`main()`函数中调用新增加的成员函数来测试扩充后的功能。
/*  
 * Copyright (c) 2015, 烟台大学计算机学院  
 * All rights reserved.  
 * 文件名称:test.cpp  
 * 作    者:冷基栋   
 * 完成日期:2015年 3 月 15 日  
 * 版 本 号:v1.0  
 *  
 * 问题描述:阅读、运行程序后,按要求扩充类的功能;
             功能(1):请在原类基础上,在类内增加下列成员函数(将是内置成员函数)
                    add_a_sec()  //增加1秒钟
                    add_a_minute() //增加1分钟
                    add_an_hour() //增加1小时

 * 输入描述: NULL;
 * 程序输出: 输出改变后的时间。
 */


问题源代码:

#include <iostream>
using namespace std;
class Time
{
public:
    void set_time( );   
    void show_time( );  
private: 
    bool is_time(int, int, int);   //这个成员函数设置为私有的,是合适的,请品味
    int hour;
    int minute;
    int sec;
};
void Time::set_time( ) 
{
    char c1,c2;
    cout<<"请输入时间(格式hh:mm:ss)";
    while(1)
    {    cin>>hour>>c1>>minute>>c2>>sec;
        if(c1!=':'||c2!=':')
            cout<<"格式不正确,请重新输入"<<endl;
        else if (!is_time(hour,minute,sec))
            cout<<"时间非法,请重新输入"<<endl;
        else 
            break;
    }
}
void Time::show_time( )      
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
bool Time::is_time(int h,int m, int s)
{
    if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
        return false;
    return true;
}
int main( )
{
    Time t1;  
    t1.set_time( );   
    T1.show_time( );
    return 0;
}

要求:

请在原类基础上,在类内增加下列成员函数(将是内置成员函数)

  • add_a_sec()  //增加1秒钟
  • add_a_minute() //增加1分钟
  • add_an_hour() //增加1小时

  在main()数中,调用新增加的成员函数,以测试扩充后的功能。

增加后的代码

#include <iostream>
using namespace std;
class Time
{
public:
    void set_time( );
    void show_time( );
    inline void add_a_sec();
    inline void add_a_minute();
    inline void add_a_hour();
private:
    bool is_time(int, int, int);   //这个成员函数设置为私有的,是合适的,请品味
    int hour;
    int minute;
    int sec;
};
void Time::set_time( )
{
    char c1,c2;
    cout<<"请输入时间(格式hh:mm:ss)";
    while(1)
    {    cin>>hour>>c1>>minute>>c2>>sec;
        if(c1!=':'||c2!=':')
            cout<<"格式不正确,请重新输入"<<endl;
        else if (!is_time(hour,minute,sec))
            cout<<"时间非法,请重新输入"<<endl;
        else
            break;
    }
}
bool Time::is_time(int h,int m, int s)
{
    if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
        return false;
    return true;
}
inline void Time::add_a_sec()
{
    sec+=1;
    if (sec>59)
    {
        sec=00;
        minute+=1;
    }
    if (minute>59)
    {
        minute=00;
        hour+=1;
    }
    if (hour>23)
        hour=00;
}
inline void Time::add_a_minute()
{
    minute+=1;
    if (minute>59)
    {
        minute=00;
        hour+=1;
    }
    if (hour>23)
        hour=00;
}
inline void Time::add_a_hour()
{
    hour+=1;
    if (hour>23)
        hour=00;
}
void Time::show_time( )
{
    add_a_sec();
    add_a_minute();
    add_a_hour();
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}

int main( )
{
    Time t1;
    t1.set_time( );
    t1.show_time( );
    return 0;
}

运行结果:

知识点总结:

类和对象的应用

学习心得:

好好学习 天天向上

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的Anthony

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值