C++ 运算符重载

Posted 技术笔记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 运算符重载相关的知识,希望对你有一定的参考价值。

以复数来说明重载的例子,包括+,-,*,/四种运算。

不用class,不用重载,用double数组,分别表示实部、虚部,结构体写类型为引用后者指针的函数,用class中的函数都可以做到,但是用 重载运算符的方法是最最便捷的.

下面的示例代码分别使用1、数组,2、函数,3、重载。

//pointer,reference,struct also OK

#include <iostream>
using namespace std;

typedef double Complex[2];           //[0]---real,[1]---image

void cmplx_ipt(Complex);
void cmplx_prt(const Complex);
void cmplx_add(Complex c, const Complex a, const Complex b);
void cmplx_sub(Complex c, const Complex a, const Complex b);
void cmplx_mul(Complex c, const Complex a, const Complex b);
void cmplx_div(Complex c, const Complex a, const Complex b);

int main() {
    Complex op1, op2, result;
    
    cmplx_ipt(op1);
    cmplx_ipt(op2);

    cmplx_add(result, op1, op2);
    cmplx_prt(op1); cout << +; cmplx_prt(op2); cout << =; cmplx_prt(result); cout << endl;

    cmplx_sub(result, op1, op2);
    cmplx_prt(op1); cout << +; cmplx_prt(op2); cout << =; cmplx_prt(result); cout << endl;

    cmplx_mul(result, op1, op2);
    cmplx_prt(op1); cout << +; cmplx_prt(op2); cout << =; cmplx_prt(result); cout << endl;

    cmplx_div(result, op1, op2);
    cmplx_prt(op1); cout << +; cmplx_prt(op2); cout << =; cmplx_prt(result); cout << endl;

    return 0;
}

void cmplx_prt(const Complex c) {
    cout << ( << c[0] << (c[1] < 0 ? - : +) << (c[1] < 0 ? -c[1] : c[1]) << "i)";
    return;
}

void cmplx_ipt(Complex c) {
    char s, i;
    cout << " Please input a complex in the form of a+bi -- ";
    cin >> c[0] >> s >> c[1] >> i;
    if (s == -)  c[1] = -c[1];
    return;
}

void cmplx_add(Complex c, const Complex  a, const Complex b) {
    c[0] = a[0] + b[0];
    c[1] = a[1] + b[1];
    return;
}

void cmplx_sub(Complex c, const Complex a, const Complex b) {
    c[0] = a[0] - b[0];
    c[1] = a[1] - b[1];
    return;
}

void cmplx_mul(Complex c, const Complex o1, const Complex o2) {
    c[0] = o1[0] * o2[0] - o1[1] * o2[1];
    c[1] = o1[1] * o2[0] + o1[0] * o2[1];
    return;
}

void cmplx_div(Complex c, const Complex o1, const Complex o2) {
    double  b;

    b = o2[0] * o2[0] + o2[1] * o2[1];
    c[0] = (o1[0] * o2[0] + o1[1] * o2[1]) / b;
    c[1] = (o1[1] * o2[0] - o1[0] * o2[1]) / b;
    return;
}
//Complex.h

#if _MSR_VER > 1000
#pragma once
#endif 

#include <string>

class Complex  {
public:
    void cmplx_prt(void);
    void cmplx_ipt(void);

    Complex cmplx_add(const Complex &);
    Complex cmplx_sub(const Complex &);
    Complex cmplx_mul(const Complex &);
    Complex cmplx_div(const Complex &);

private:
    double real;
    double image;
};

//Complex.cpp

#include "myClass.h"
#include <iostream>
using namespace std;

void Complex::cmplx_ipt() {
    char  s, i;
    cout << " Please input a complex in the form of a+bi -- ";
    cin >> real >> s >> image >> i;
    if (s == -)  image = -image;
    return;
}

void Complex::cmplx_prt() {
    cout << ( << real << (image<0 ? - : +) << (image<0 ? -image : image) << "i)";
    return;
}

Complex Complex::cmplx_add(const Complex &a) {
    Complex result;
    result.real = real + a.real;
    result.image = image + a.image;
    return result;
}

Complex Complex::cmplx_sub(const Complex &a) {
    Complex result;
    result.real = real - a.real;
    result.image = image = a.image;
    return result;
}

Complex Complex::cmplx_mul(const Complex &a) {
    Complex result;
    result.real = real*a.real - image*a.image;
    result.image = image*a.real + real*a.image;
    return result;
}


Complex Complex::cmplx_div(const Complex &a) {
    Complex result;

    double tmp = a.real*a.real + a.image*a.image;
    result.real =  (real*a.real + image*a.image) / tmp;
    result.image = (image*a.real - real*a.image) / tmp;
    return result;
}

//main.cpp

#include "myClass.h"
#include <iostream>
using namespace std;



int main() {
    Complex op1, op2, result;
    
    op1.cmplx_ipt();
    op2.cmplx_ipt();

    result = op1.cmplx_add(op2);
    op1.cmplx_prt(); cout << +; op2.cmplx_prt(); cout << =; result.cmplx_prt(); cout << endl;

    result = op1.cmplx_sub(op2);
    op1.cmplx_prt(); cout << -; op2.cmplx_prt(); cout << =; result.cmplx_prt(); cout << endl;

    result = op1.cmplx_mul(op2);
    op1.cmplx_prt(); cout << *; op2.cmplx_prt(); cout << =; result.cmplx_prt(); cout << endl;

    result = op1.cmplx_div(op2);
    op1.cmplx_prt(); cout << /; op2.cmplx_prt(); cout << =; result.cmplx_prt(); cout << endl;

    return 0;
}
//myClass.h

class  Complex {
public:
    Complex(double r=0, double i=0) :real(r), image(i) {};
    Complex operator+(const Complex &);
    Complex operator-(const Complex &);
        Complex operator*(const Complex &);
        Complex operator/(const Complex &);
    void cmplx_prt();
//    ~Complex();

private:
    double real;
    double image;
};

//myClass.cpp

#include    "myClass.h"
#include <iostream>
using namespace std;

Complex Complex::operator+ (const Complex &a) {
    Complex r;
    r.real = real + a.real;
    r.image = image + a.image;
    return r;
}

Complex Complex::operator-(const Complex &a) {
    Complex r;
    r.real = real - a.real;
    r.image = image - a.image;
    return r;
}

Complex Complex::operator*(const Complex &a) {
    Complex r;
    r.real = real*a.real - image*a.image;
    r.image = image*r.real + real*a.image;
    return r;
}

Complex Complex::operator/(const Complex &a) {
    Complex r;
    double tmp = real*a.real + image*image;
    r.real =  (real*a.real + image*a.image) / tmp;
    r.image = (image*a.real - real*a.image) / tmp;
    return r;
}

void Complex::cmplx_prt() {
    cout << real << (image < 0 ? - : +) << (image<0?-image:image) << i << endl;
}

//main.cpp
#include    <iostream>
#include    <string>
using  namespace  std;

#include    "myClass.h"

int  main(void) {
    Complex op1(3, 4), op2(2, -3), result;
    
    result = op1 + op2;      //result = result.operator+(op2); OK!!!
    result.cmplx_prt();
    result = op1 - op2;
    result.cmplx_prt();

    result = op1 * op2;
    result.cmplx_prt();
    result = op1 / op2;
    result.cmplx_prt();
        return 0;
}

<< ;>>; ->;[ ]这些运算符,在接下来OOP,继承多态等示例程序中会有所涉及。

以上是关于C++ 运算符重载的主要内容,如果未能解决你的问题,请参考以下文章

运算符 + 重载 C++

C++运算符重载

c++ 运算符重载问题

C++重载'--'后缀运算符

C++ 运算符重载

C++运算符的重载