C++中运算符重载

运算符重载可以写在类的内部,也可以写成全局函数形式,其中运算符 ()  、[ ] 、->、 =,在重载时必须声明为类的成员函数;

而运算符 “ . ”、“ .* ” 、“ :: ”、“ ?:”、 sizeof 不能被重载;

算法运算符的重载不会改变运算符原有的优先级。

例子:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class Complex{
private:
    double real, imag;
public:
    Complex(double r=0,double i=0):real(r),imag(i){}
    friend ostream & operator<<(ostream & os, const Complex & c);
    friend istream & operator>>(istream & is, Complex & c);
    Complex operator+(Complex & c){               // 使用成员函数方法重载加法
        real +=c.real;
        imag +=c.imag;
        return * this;
    }
    friend Complex operator-(Complex &c1, Complex &c2);   // 返回类型为 Complex
    void print();

    operator double(){return real;}     // 重载强制类型转换符

    Complex & operator++();                    // ++C 形式
    Complex operator++(int);                   // C++ 形式
    friend Complex & operator--(Complex & c);      // --C
    friend Complex operator--(Complex & , int);
};

Complex &Complex::operator++(){
    real += (real/(real+imag));
    imag += (imag/(real+imag));
    return *this;
}

Complex Complex::operator++(int){
    Complex c(*this);
    c.real = real;
    c.imag = imag;
    real += (real/(real+imag));
    imag += (imag/(real+imag));
    return c;
}

Complex & operator--(Complex & c){
    c.real -= (c.real/(c.real+c.imag));
    c.imag -= (c.imag/(c.real+c.imag));
    return c;
}

Complex operator--(Complex & c, int){
    Complex tmp(c);
    tmp.real = c.real;
    tmp.imag = c.imag;
    c.imag -= (c.imag/(c.imag+c.real));
    c.real -= (c.real/(c.imag+c.real));
    return tmp;
}

Complex operator-(Complex &c1, Complex &c2){     // 使用全局函数类型重载减法
    Complex c(c1);
    c.real = c1.real - c2.real;
    c.imag = c2.imag - c1.imag;
    return c;
}

void Complex::print(){
    if(imag>0)
        cout<<real<<"+"<<imag<<"i"<<endl;
    else if(imag<0) {
        cout<<real<<imag<<"i"<<endl;
    }
    else {
        cout<<real<<endl;
    }
}

ostream & operator<<(ostream &os,const Complex &c){
    os<<c.real<<"+"<<c.imag<<"i";       //以指定格式输出
    return os;                          // os 就是cout
}
istream & operator>>(istream & is, Complex &c){
    string s;
    is>>s;                                     // is 就是cin
    int pos = s.find("+", 0);
    string sTmp = s.substr(0, pos);            // 分离出代表实部的字符串
    c.real = atof(sTmp.c_str());               // atof库函数能将const char*指针指向的内容转换成float类型
    sTmp = s.substr(pos+1,s.length()-pos-2);   // 分离出代表虚部的字符串
    c.imag = atof(sTmp.c_str());
    return is;
}



int main()
{
    Complex c1={1,2}, c2={2,3};
    Complex c3, c4;
    c3 = c1-c2;
    c1.print();
    c3.print();
    c4 = c1+c2;
    cout<<"c4=";
    c4.print();

    cout<<c2<<endl;

    cout<<(double)c2<<endl;     // 使用重载后的double
    double n = 2+c2;            // 在类中如果有类型转换的重载,编译器会自动调用

    c4++;
    cout<<"c4++ = ";
    c4.print();
    cout<<"--c4 = ";
    --c4;
    c4.print();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Op_chaos

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

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

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

打赏作者

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

抵扣说明:

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

余额充值