第四周任务二——多文件

01.#include <iostream>  
02.#include"mytime.h"    //该头文件中包含了Time类的定义  
03.using namespace std;  
04.int main( )  
05.{  
06.    Time t1;    //有了 #include"mytime.h" ,可以直接使用Time定义对象  
07.    t1.set_time( );    
08.    cout<<"现在时间是:";  
09.    t1.show_time( );  
10.  
11.    t1.add_a_sec();  //增加1秒钟  
12.    cout<<"增加1秒钟后:";  
13.    t1.show_time( );  
14.  
15.    t1.add_a_minute(); //增加1分钟  
16.    cout<<"增加1分钟后:";  
17.    t1.show_time( );  
18.  
19.    t1.add_an_hour(); //增加1小时  
20.    cout<<"增加1小时后:";  
21.    t1.show_time( );  
22.  
23.    t1.add_seconds(40); //增加40秒钟  
24.    cout<<"增加40秒钟后:";  
25.    t1.show_time( );  
26.  
27.    t1.add_minutes(127); //增加127分钟  
28.    cout<<"增加127分钟后:";  
29.    t1.show_time( );  
30.  
31.    t1.add_hours(8); //增加8小时  
32.    cout<<"增加8小时后:";  
33.    t1.show_time( );  
34.    system("PAUSE");  //在VS2008中,可出现“按任一键继续...”   
35.    return 0;  
36.}  

 头文件: mytime.h,头文件
 



[cpp] view plaincopyprint?
01.//本文件中只做类的声明,关注公用接口,而不关心私有实现,做到了信息隐藏  
02.class Time  
03.{  
04.public:  
05.    void set_time( );     
06.    void show_time( );    
07.    inline void add_a_sec();  //增加1秒钟  
08.    inline void add_a_minute(); //增加1分钟  
09.    inline void add_an_hour(); //增加1小时  
10.    void add_seconds(int); //增加n秒钟  
11.    void add_minutes(int); //增加n分钟  
12.    void add_hours(int); //增加n小时  
13.private:   
14.    bool is_time(int, int, int);  
15.    int hour;  
16.    int minute;  
17.    int sec;  
18.};  
19.  
20.//注意:下面的内置成员函数(inline)要与类声明放在一个文件中。因为在编译时,需要将对该函数的调用替换为该函数的定义,所以不能在其他文件中独立定义  
21.  
22.inline void Time::add_a_sec()  //增加1秒钟  
23.{  
24.    ++sec;              //直接修改sec的值即可,sec是Time类的数据成员  
25.    if (sec>59)          //sec超出规定的范围,因为只是增加1秒,最多也就是向分钟进位1,所以增加1分钟。        
26.        add_a_minute(); //至于增加1分钟是否会引起小时变化,由add_a_minute()处理  
27.}  
28.  
29.inline void Time::add_a_minute() //增加1分钟  
30.{  
31.    ++minute;  
32.    if (minute>59)       //参见add_a_sec()中的注释  
33.        add_an_hour();  
34.}  
35.  
36.inline void Time::add_an_hour() //增加1小时  
37.{  
38.    ++hour;  
39.    if (hour>23)  
40.        hour=0;     //到第2天了  
41.      
42.}  

 类定义文件: mytime.cpp,用于定义类Time中的成员函数
 




[cpp] view plaincopyprint?
01.#include <iostream>  
02.#include"mytime.h"  //该头文件中包含了Time类的定义  
03.using namespace std;  
04.  
05.//下面实现的是非内置成员函数,实现了公用接口与私有实现的分离,做到了信息隐藏  
06.  
07.void Time::set_time( )   
08.{  
09.    char c1,c2;  
10.    cout<<"请输入时间(格式hh:mm:ss)";  
11.    while(1)  
12.    {  
13.        cin>>hour>>c1>>minute>>c2>>sec;  
14.        if(c1!=':'||c2!=':')  
15.            cout<<"格式不正确,请重新输入"<<endl;  
16.        else if (!is_time(hour,minute,sec))  
17.            cout<<"时间非法,请重新输入"<<endl;  
18.        else   
19.            break;  
20.    }  
21.}  
22.  
23.void Time::show_time( )        
24.{  
25.    cout<<hour<<":"<<minute<<":"<<sec<<endl;  
26.}  
27.  
28.bool Time::is_time(int h,int m, int s)  
29.{  
30.    if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)  
31.        return false;  
32.    return true;  
33.}  
34.  
35.void Time::add_seconds(int n) //增加n秒钟  
36.{  
37.    sec+=n;         //直接加上去。此操作可能使sec超出取值范围,将在下面处理,我们只要保证此函数执行完sec的取值正确即可  
38.    if (sec>59)      //思考:if中的两条语句能否交换顺序?为什么不能?后果将是……?  
39.    {  
40.        add_minutes(sec/60);    //增加sec/60分钟  
41.        sec%=60;                //秒数应该是sec%=60  
42.    }  
43.}  
44.  
45.void Time::add_minutes(int n) //增加n分钟  
46.{  
47.    minute+=n;  
48.    if (minute>59)       //参见add_seconds()中的注释  
49.    {  
50.        add_hours(minute/60);  
51.        minute%=60;  
52.    }  
53.}  
54.  
55.void Time::add_hours(int n) //增加n小时  
56.{  
57.    hour+=n;  
58.    if (hour>23)  
59.        hour%=24;       //此程序不涉及日期,如果设计类DateTime,修改将继续下去  
60.}  
【特别强调】掌握这样的结构,并且体会内置成员函数要与class的定义放在同一个头文件中。如果三个内置成员函数与其他成员函数都定义在了mytime.cpp中,将会在编译时出现错误:
 



[plain] view plaincopyprint?
01.1>------ 已启动生成: 项目: time, 配置: Debug Win32 ------  
02.1>正在链接...  
03.1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall Time::add_an_hour(void)" (?add_an_hour@Time@@QAEXXZ),该符号在函数 _main 中被引用  
04.1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall Time::add_a_minute(void)" (?add_a_minute@Time@@QAEXXZ),该符号在函数 _main 中被引用  
05.1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall Time::add_a_sec(void)" (?add_a_sec@Time@@QAEXXZ),该符号在函数 _main 中被引用  
06.1>D:\C++\VS2008 project\time\Debug\time.exe : fatal error LNK1120: 3 个无法解析的外部命令  
07.1>生成日志保存在“file://d:\C++\VS2008 project\time\time\Debug\BuildLog.htm”  
08.1>time - 4 个错误,0 个警告  
09.========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========  

 【更好的写法】直接在class中实现的成员函数被 认为是内置函数,本题中add_a_xxxxx()中代码短,(可能)会被频繁调用,设置为内置函数是合理的。所以,mytime.h采用如下写法更简洁。
 


[cpp] view plaincopyprint?
01.class Time  
02.{  
03.public:  
04.    void set_time( );     
05.    void show_time( );    
06.    inline void add_a_sec()  //增加1秒钟  
07.    {  
08.        ++sec;                
09.        if (sec>59)  add_a_minute();   
10.    }  
11.    inline void add_a_minute() //增加1分钟  
12.    {  
13.        ++minute;  
14.        if (minute>59) add_an_hour();  
15.    }  
16.    inline void add_an_hour() //增加1小时  
17.    {  
18.        ++hour;  
19.        if (hour>23) hour=0;       
20.    }  
21.    void add_seconds(int); //增加n秒钟  
22.    void add_minutes(int); //增加n分钟  
23.    void add_hours(int); //增加n小时  
24.private:   
25.    bool is_time(int, int, int);  
26.    int hour;  
27.    int minute;  
28.    int sec;  
29.};  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值