C++运算符重载实现加减乘除

运算符重载可以作为类成员函数和友元函数

使用哪种方式,取决于使用的环境

一般情况下,单目运算符和复合运算符(+=,-=,/=,*=,!=)重载作为成员函数

双目运算符重载作为友元函数

运算符加减乘除属于双目运算符一般采用友元函数,个人觉得友元函数更好理解一点

下面我们分别用两种方法实现类复数的加减乘除

废物不多说,直接上代码:

成员函数实现

#include <iostream>

using namespace std;

class Complex {
public:
    Complex(){real = 0;imag = 0;} //构造函数
    Complex(double r,double i){real = r;imag = i;}  //构造函数重载
    Complex operator+(Complex &c2); //加法运算符重载
    Complex operator-(Complex &c2); //减法运算符重载
    Complex operator*(Complex &c2); //乘法运算符重载
    Complex operator/(Complex &c2); //除法运算符重载
    void display();  // 打印函数
private:
    double real;    //实部
    double imag;      //虚部
};

//加号重载函数
Complex Complex::operator+(Complex &c2)
{
    //运算符重载作为类成员函数,传进去的参数只需要一个
    //real这里是省略了this指针
    //返回一个Complex类型的数据
    return Complex(real+c2.real,imag+c2.imag);
}

//减号重载函数
Complex Complex::operator -(Complex &c2)
{

    return Complex(this->real-c2.real,this->imag-c2.imag);
}

//乘号重载函数
Complex Complex::operator *(Complex &c2)
{

    return Complex(real*c2.real,imag*c2.imag);
}

//除号重载函数
Complex Complex::operator /(Complex &c2)
{

    return Complex(real/c2.real,imag/c2.imag);
}

void Complex::display()
{
    cout<<"("<<real<<","<<imag<<"i)"<<endl;
}

int main()
{
    Complex c1(3,4),c2(5,-10) ,c3;
    c3 = c1+c2;         //加号重载
    cout<<"c3 = ";
    c3.display();
    c3 = c1-c2;     //减号重载
    cout<<"c3 = ";
    c3.display();
    c3 = c1*c2;     //乘号重载
    cout<<"c3 = ";
    c3.display();
    c3 = c1/c2;     //除号重载
    cout<<"c3 = ";
    c3.display();

}

执行结果:

 

 友元函数实现:

#include <iostream>

using namespace std;

class Complex {
public:
    Complex(){real = 0;imag = 0;}
    Complex(double r,double i){real = r;imag = i;}
    friend Complex operator+(Complex &c1,Complex &c2);
    friend Complex operator-(Complex &c1,Complex &c2);
    friend Complex operator*(Complex &c1,Complex &c2);
    friend Complex operator/(Complex &c1,Complex &c2);
    void display();  // 打印函数
private:
    double real;
    double imag;
};

//加号重载函数
Complex operator+(Complex &c1,Complex &c2)
{

    return Complex(c1.real+c2.real,c1.imag+c2.imag);
}

//减号重载函数
Complex operator -(Complex &c1,Complex &c2)
{

    return Complex(c1.real-c2.real,c1.imag-c2.imag);
}

//乘号重载函数
Complex operator *(Complex &c1,Complex &c2)
{

    return Complex(c1.real*c2.real,c1.imag*c2.imag);
}

//除号重载函数
Complex operator /(Complex &c1,Complex &c2)
{

    return Complex(c1.real/c2.real,c1.imag/c2.imag);
}

void Complex::display()
{
    cout<<"("<<real<<","<<imag<<"i)"<<endl;
}

int main()
{
    Complex c1(3,4),c2(5,-10) ,c3;
    c3 = c1+c2;         //加号重载
    cout<<"c3 = ";
    c3.display();
    c3 = c1-c2;     //减号重载
    cout<<"c3 = ";
    c3.display();
    c3 = c1*c2;     //乘号重载
    cout<<"c3 = ";
    c3.display();
    c3 = c1/c2;     //除号重载
    cout<<"c3 = ";
    c3.display();

}

执行结果:

 

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值