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();

}

执行结果:

 

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的示例程序,重载了加减乘除四个运算符: ```c++ #include <iostream> class MyMath { public: MyMath(int value) : m_value(value) {} // 加法运算符重载 MyMath operator+(const MyMath& other) { int result = m_value + other.m_value; return MyMath(result); } // 减法运算符重载 MyMath operator-(const MyMath& other) { int result = m_value - other.m_value; return MyMath(result); } // 乘法运算符重载 MyMath operator*(const MyMath& other) { int result = m_value * other.m_value; return MyMath(result); } // 除法运算符重载 MyMath operator/(const MyMath& other) { int result = m_value / other.m_value; return MyMath(result); } // 输出运算符重载 friend std::ostream& operator<<(std::ostream& os, const MyMath& obj) { os << obj.m_value; return os; } private: int m_value; }; int main() { MyMath a(10); MyMath b(5); std::cout << "a + b = " << a + b << std::endl; // 输出 15 std::cout << "a - b = " << a - b << std::endl; // 输出 5 std::cout << "a * b = " << a * b << std::endl; // 输出 50 std::cout << "a / b = " << a / b << std::endl; // 输出 2 return 0; } ``` 在上面的示例中,我们定义了一个 `MyMath` 类,其中包含一个整数成员变量 `m_value`,并重载了加减乘除四个运算符。在 `main()` 函数中,我们创建了两个 `MyMath` 类对象 `a` 和 `b`,并进行了加减乘除四种运算,输出了结果。 需要注意的是,除法运算符重载时,如果被除数为 0,会导致程序崩溃。因此,应该在重载运算符时,增加对输入数据的判断,避免出现异常情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值