zoukankan      html  css  js  c++  java
  • 计算时间:一个C++运算符重载示例

    Time类是一个用于计算时间的类,其原型如下:
    程序清单11.1 mytime0.h

    // mytime0.h -- Time class before operator overloading
    #ifndef MYTIME0_H_
    #define MYTIME0_H_
    
    class Time
    {
    private:
        int hours;
        int minutes;
    public:
        Time();
        Time(int h, int m = 0);
        void AddMin(int m);
        void AddHr(int h);
        void Reset(int h = 0, int m = 0);
        Time Sum(const Time & t) const;
        void Show() const;
    };
    #endif // MYTIME0_H_

    Time类提供了用于调整和重新设置事时间、显示时间、将两个事件相加的方法。程序清单11.2列出了方法定义。
    程序清单11.2 mytime0.cpp

    // mytime0.cpp -- implementing Time methods
    #include <iostream>
    #include "mytime0.h"
    
    Time::Time()
    {
        hours = minutes = 0;
    }
    
    Time::Time(int h, int m)
    {
        hours = h;
        minutes = m;
    }
    
    void Time::AddMin(int m)
    {
        minutes += m;
        hours += minutes / 60;
        minutes %= 60;
    }
    
    void Time::AddHr(int h)
    {
        hours += h;
    }
    
    void Time::Reset(int h, int m)
    {
        hours = h;
        minutes = m;
    }
    
    Time Time::Sum(const Time & t) const
    {
        Time sum;
        sum.minutes = minutes + t.minutes;
        sum.hours = hours + t.hours + sum.minutes / 60;
        sum.minutes %= 60;
        return sum;
    }
    
    void Time::Show() const
    {
        std::cout << hours << " hours, " << minutes << "minutes";
    }

    最后,程序清单11.3对Time类中计算时间总和的部分进行了测试。
    程序清单11.3 usetime0.cpp

    // usetime0.cpp -- using the first draft of the Time class
    // conpile usetime0.cpp and mytime0.cpp together
    #include <iostream>
    #include "mytime0.h"
    
    int main()
    {
        using std::cout;
        using std::endl;
        Time planing;
        Time coding(2, 40);
        Time fixing(5, 55);
        Time total;
    
        cout << "planing time = ";
        planing.Show();
        cout << endl;
    
        cout << "coding time = ";
        coding.Show();
        cout << endl;
    
        cout << "fixing time = ";
        fixing.Show();
        cout << endl;
    
        total = coding.Sum(fixing);
        cout << "coding.Sum(fixing) = ";
        total.Show();
        cout << endl;
    
        return 0;
    }

    下面是程序清单11.1、程序清单11.2和程序清单11.3组成的程序的输出:

    planing time = 0 hours, 0 minutes
    coding time = 2 hours, 40 minutes
    fixing time = 5 hours, 55 minutes
    coding.Sum(fixing) = 8 hours, 35 minutes

    11.2.1 添加加法运算符
    将Time类转换为重载的加法运算符很容易,只要将Sum()的名称改为operator +()即可。这样做是对的,只要把运算符(这里为+)防盗operator的后面,并将结果用作方法名即可。在这里,可以在标识符中使用字幕、数字或下划线之外的其他字符。程序清单11.4和程序清单11.5反映了这些细微的修改。
    程序清单11.4 mytime1.h

    // mytime1.h -- Time class before operator overloading
    #ifndef MYTIME1_H_
    #define MYTIME1_H_
    
    class Time
    {
    private:
        int hours;
        int minutes;
    public:
        Time();
        Time(int h, int m = 0);
        void AddMin(int m);
        void AddHr(int h);
        void Reset(int h = 0, int m = 0);
        Time operator + (const Time & t) const;
        void Show() const;
    };
    #endif // MYTIME1_H_

    程序清单11.5 mytime1.cpp

    // mytime1.cpp -- implementing Time methods
    #include <iostream>
    #include "mytime1.h"
    
    Time::Time()
    {
        hours = minutes = 0;
    }
    
    Time::Time(int h, int m)
    {
        hours = h;
        minutes = m;
    }
    
    void Time::AddMin(int m)
    {
        minutes += m;
        hours += minutes / 60;
        minutes %= 60;
    }
    
    void Time::AddHr(int h)
    {
        hours += h;
    }
    
    void Time::Reset(int h, int m)
    {
        hours = h;
        minutes = m;
    }
    
    Time Time::operator+(const Time & t) const
    {
        Time sum;
        sum.minutes = minutes + t.minutes;
        sum.hours = hours + t.hours + sum.minutes / 60;
        sum.minutes %= 60;
        return sum;
    }
    
    void Time::Show() const
    {
        std::cout << hours << " hours, " << minutes << " minutes";
    }

    用一个usetime1.cpp做测试,得到和上述一样的结果。

    usetime1.cpp:

    // usetime1.cpp -- using the first draft of the Time class
    // conpile usetime0.cpp and mytime0.cpp together
    #include <iostream>
    #include "mytime1.h"
    
    int main()
    {
        using std::cout;
        using std::endl;
        Time planing;
        Time coding(2, 40);
        Time fixing(5, 55);
        Time total;
    
        cout << "planing time = ";
        planing.Show();
        cout << endl;
    
        cout << "coding time = ";
        coding.Show();
        cout << endl;
    
        cout << "fixing time = ";
        fixing.Show();
        cout << endl;
    
        total = coding + fixing;
        cout << "coding.Sum(fixing) = ";
        total.Show();
        cout << endl;
    
        return 0;
    }

    和Sum()一样,operator+()也是由Time对象调用的,它将第二个Time对象作为参数,并返回一个Time对象。因此,可以像调用Sum()那样来调用operator+()方法:

    total = coding.operator+(fixing);    // function notation

    但将该方法命令为operator+()后,也可以使用运算符表示法:

    total = coding + fixing;        // operator notation

    这两种表示法都将调用operator+()方法。注意,在运算符表示法中,运算符左侧的对象(这里为coding)是调用对象,运算符右边的对象(这里为fixing)是作为参数被传递的对象。

  • 相关阅读:
    游戏引擎服务端应该也要具备测试模块
    My Trap For C++
    unix网络编程--锁(一)
    所遇不良设计(四)
    所遇不良设计(二)
    有趣的emacs
    [Java复习]Hashcode
    [Java复习]java线程
    [Java复习]重载、覆盖、继承、多态
    折磨我两天的坑!小程序云函数调用时本地和云端测试成功,控制台接收不到正确结果
  • 原文地址:https://www.cnblogs.com/moonlightpoet/p/5660253.html
Copyright © 2011-2022 走看看