C++加法运算符重载

在oop的思想指导下,类的定义一般放在.h头文件中,类的实现则是放在.cpp文件(包含对应的.h文件)中,最后在包含主调用方法的main.cppinclude"xxxx.h"

其实,我觉得多态并不是计算机首创的方法,而是一个早已实践在人类语言中的思想。比如说,我们并没有给踢足球和踢出群聊这两个动作发明各自不同的动词,而是根据他们动作和概念上的相似性,都使用了“踢”这个动词,这样极大地简化了语言的复杂度。C++中的多态也是这样的原理,通过运算符重载,使得简单的数学运算符号可以流畅地应用在我们自己定义的对象身上,隐藏了内部机理,并强调了实质。

下面我们实现一个类,实现时间(时分秒)的表示,并能够通过sum函数对时间对象进行加减法。
类的具体定义:

//operator.h
#if !defined(OPERATOR_H_)
#define OPERATOR_H_
#include<iostream>
class Time
{
    int hour_;
    int minute_;
    int second_;

    public:
    Time();
    Time(int h,int m,int s);
    void show();
    Time sum(const Time & t) const;


};


#endif // OPERATOR_H_



类的具体实现

//operator.cpp
#include<iostream>
#include"operator.h"

using namespace std;


Time::Time()
{
    hour_ = 0;
    minute_ = 0;
    second_ = 0;
}
Time::Time(int h,int m,int s)
{
    hour_ = h;
    minute_ = m;
    second_ = s;
}
void Time::show()
{
    
    cout << "hour :" << hour_ << endl;
    cout << "minute :" << minute_ << endl;
    cout << "second :" << second_ << endl;
    cout << endl;
}

Time Time::sum(const Time& t)const{
    Time time;
    time.second_ = second_ + t.second_;
    time.minute_ = minute_ + t.minute_ + time.second_ / 60;
    time.second_ %= 60;
    time.hour_ = hour_ + t.hour_ + time.minute_ / 60;
    time.minute_ %= 60;

    return time;
}

主方法的运行:

//main.cpp
#include<iostream>
#include"operator.h"
using namespace std;
#define name_to_str(name_31415926)  (#name_31415926)

int main()
{
    Time sum;
    Time part1(2,30,45);
    Time part2(3,40,50);

    cout << name_to_str(part1) << endl;
    part1.show();
    cout << name_to_str(part2) << endl;
    part2.show();

    sum = part1.sum(part2);
    sum.show();
}

输出:

g++ main.cpp operator.cpp operator.h -o main
PS D:\程序\随笔程序\2020年2月> ./main
part1
hour :2
minute :30
second :45

part2
hour :3
minute :40
second :50

hour :6
minute :11
second :35

运算符重载其实很简单,只需要将实现那个功能的函数名修改为operator 符号就可以啦。

比如说将上面的函数Time sum()->Time operator+()即可
operator.h改为

#if !defined(OPERATOR_H_)
#define OPERATOR_H_
#include<iostream>
class Time
{
    int hour_;
    int minute_;
    int second_;

    public:
    Time();
    Time(int h,int m,int s);
    void show();
    Time operator+(const Time & t) const;


};


#endif // OPERATOR_H_

operator.cpp改为:

#include<iostream>
#include"operator.h"

using namespace std;


Time::Time()
{
    hour_ = 0;
    minute_ = 0;
    second_ = 0;
}
Time::Time(int h,int m,int s)
{
    hour_ = h;
    minute_ = m;
    second_ = s;
}
void Time::show()
{
    
    cout << "hour :" << hour_ << endl;
    cout << "minute :" << minute_ << endl;
    cout << "second :" << second_ << endl;
    cout << endl;
}

Time Time::operator+(const Time& t)const{
    Time time;
    time.second_ = second_ + t.second_;
    time.minute_ = minute_ + t.minute_ + time.second_ / 60;
    time.second_ %= 60;
    time.hour_ = hour_ + t.hour_ + time.minute_ / 60;
    time.minute_ %= 60;

    return time;
}

main方法改为:

#include<iostream>
#include"operator.h"
using namespace std;
#define name_to_str(name_31415926)  (#name_31415926)

int main()
{
    Time sum;
    Time part1(2,30,45);
    Time part2(3,40,50);

    cout << name_to_str(part1) << endl;
    part1.show();
    cout << name_to_str(part2) << endl;
    part2.show();

    sum = part1+part2;
    sum.show();
}

这里需要说明的是,虽然我们在主方法中调用时使用的是part1+part2,但是编译器调用的是这样的语句part1.operator+(part2),也就是说,该方法隐式调用了part1,并作为参数显式调用了part2.

并且,这样重载的运算符是支持连加方法的,比如sum = part1+part2+part3;这样的操作,因为仔细观察函数的定义Time operator+(const Time & t) const;会发现它会返回一个Time对象,也就是说,先进行part2和part3的计算,并将返回的对象作为参数与part1相加,最终返回连加的答案。

发布了372 篇原创文章 · 获赞 48 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/dghcs18/article/details/104254284
今日推荐