书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

C++ 运算符重载

明白了函数重载后,运算符的重载就是小意思了。

但是运算符的重载在实现起来有一定的规则。

First:双目运算符

  运算符重载为成员函数形式。

  格式:类名 operator运算符(const 类名 &对象名) const{}

  具体的还是看代码吧。

  

复制代码
View Code
#include "iostream"
#include "cstring"
#include "string"
#include "cstdio"
using namespace std;
class Complex{
public:
    Complex(double r=0.0, double i=0.0):real(r),imag(i){}

    Complex operator+(const Complex &c2) const{        //重载"+"
        return Complex(real+c2.real, imag+c2.imag);
    }

    Complex operator-(const Complex &c2) const{        //重载"-"
        return Complex(real-c2.real, imag-c2.imag);
    }

    void display(){
        cout<<"("<<real<<","<<imag<<")"<<endl;
    }
private:
    double real;
    double imag;
};
int main(){
    Complex c1(5, 4), c2(2, 10), c3;
    cout<<"c1="; c1.display();
    cout<<"c2="; c2.display();
    c3 = c1+c2;
    cout<<"c3=c1+c2="; c3.display();
    c3 = c1-c2;
    cout<<"c3=c1-c2="; c3.display();
}
复制代码

 

Second:单目运算符

  运算符重载为成员函数形式。

  格式:类名& operator运算符(){} 或者 类名 operator运算符(int){}

  具体的还是来看代码。

复制代码
View Code
#include "iostream"
#include "cstring"
#include "string"
#include "cstdio"
using namespace std;
class Clock{
public:
    Clock(int hour=0, int minute=0, int second=0){
        if(hour>=0 && hour<24 && minute>=0 && minute<60 && second>=0 && second<60){
            this->hour = hour;
            this->minute = minute;
            this->second = second;
        }else cout<<"Time Error!"<<endl;
    }
    void showTime() const{
        cout<<hour<<":"<<minute<<":"<<second<<endl;
    }
    Clock& operator++(){       //后置++, 只要是无参的,就是后置的,注意这里有&
        second++;
        if(second>=60){
            second -= 60;
            minute++;
            if(minute>=60){
                minute -= 60;
                hour = (hour+1)%24;
            }
        }
        return *this;
    }
    Clock operator++(int){     //前置++, 只要是有参的,就是前置的,注意这里没&
        Clock old = *this;
        ++(*this);
        return old;
    }
private:
    int hour;
    int minute;
    int second;
};
int main(){
    Clock myClock(23, 45, 56);
    cout<<"at first:  "; myClock.showTime();
    cout<<"myClock++: "; (myClock++).showTime();
    cout<<"++myClock: "; (++myClock).showTime();
}
复制代码

 

上面所讲的都运算符重载为成员函数形式的重载,

下面来讲讲运算符重载为非成员函数形式的重载。

 

其实很简单,就只要在重载函数前面加上一个friend可以了。

但是有一点要注意:在重载为成员函数时,省去了一个参数。

那么在重载为非成员函数时,每一个参数都要传入了,不可以省。

还有一点就是要将修饰符const去掉。

好了,就这三点注意了。

再来总结一下:

  1.加friend

  2.每个参数都要传入,不能省

  3.去掉修饰符const

  下面来看一个代码吧。

复制代码
View Code
#include "iostream"
#include "cstring"
#include "string"
#include "cstdio"
using namespace std;
class Complex{
public:
    Complex(double r=0.0, double i=0.0):real(r),imag(i){}

    friend Complex operator+(const Complex &c1, const Complex &c2) {        //重载"+"
        return Complex(c1.real+c2.real, c1.imag+c2.imag);
    }

    friend Complex operator-(const Complex &c1,const Complex &c2) {        //重载"-"
        return Complex(c1.real-c2.real, c1.imag-c2.imag);
    }

    void display(){
        cout<<"("<<real<<","<<imag<<")"<<endl;
    }
private:
    double real;
    double imag;
};
int main(){
    Complex c1(5, 4), c2(2, 10), c3;
    cout<<"c1="; c1.display();
    cout<<"c2="; c2.display();
    c3 = c1+c2;
    cout<<"c3=c1+c2="; c3.display();
    c3 = c1-c2;
    cout<<"c3=c1-c2="; c3.display();
}
复制代码

 

posted on   More study needed.  阅读(209)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 2012年4月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12

导航

统计

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

点击右上角即可分享
微信分享提示