天天看點

C++ 重載運算符實作複數運算1、重載運算符為成員函數2、重載運算符為友元函數

C++ 重載運算符實作複數運算

  • 1、重載運算符為成員函數
  • 2、重載運算符為友元函數

1、重載運算符為成員函數

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
using namespace std;

class Complex
{
public:
    Complex();
    Complex(float real, float image);

    virtual ~Complex();

    Complex operator+(const Complex &c) const;
    Complex operator-(const Complex &c) const;
    Complex operator*(const Complex &c) const;
    Complex operator/(const Complex &c) const;

    friend ostream & operator<<(ostream &os, const Complex &c);

private:
    float m_real;
    float m_image;
};

#endif // COMPLEX_H
           

Complex.cpp

#include "Complex.h"

Complex::Complex()
{

}

Complex::Complex(float real, float image)
{
    m_real = real;
    m_image = image;
}

//重載+運算符
Complex Complex::operator+(const Complex &c) const
{
    Complex temp;
    temp.m_real = this->m_real + c.m_real;
    temp.m_image = this->m_image + c.m_image;

    return temp;
}

//重載-運算符
Complex Complex::operator-(const Complex &c) const
{
    Complex temp;
    temp.m_real = this->m_real - c.m_real;
    temp.m_image = this->m_image - c.m_image;

    return temp;
}

//重載*運算符
Complex Complex::operator*(const Complex &c) const
{
    Complex temp;
    temp.m_real = (this->m_real * c.m_real) - (this->m_image * c.m_image);
    temp.m_image =(this->m_image * c.m_real) + (this->m_real * c.m_image);

    return temp;
}

//重載/運算符
Complex Complex::operator/(const Complex &c) const
{
    Complex temp;
    temp.m_real = (this->m_real * c.m_real + this->m_image * c.m_image) /
                  (c.m_real * c.m_real + c.m_image * c.m_image);

    temp.m_image =(this->m_image * c.m_real - this->m_real * c.m_image) /
                  (c.m_real * c.m_real + c.m_image * c.m_image);

    return temp;
}

//重載<<運算符
ostream & operator<<(ostream &os, const Complex &c)
{
    os << c.m_real << "+" << c.m_image << "i" << endl;
    return os;
}

Complex::~Complex()
{

}
           

main.cpp

#include "Complex.h"

int main()
{
    Complex a(1, 2);
    Complex b(2, 3);
    Complex c = a + b;
    cout << c;

    c = a - b;
    cout << c;

    c = a * b;
    cout << c;

    c = a / b;
    cout << c;

    return 0;
}
           

輸出:

3+5i
-1+-1i
-4+7i
0.615385+0.0769231i
           

2、重載運算符為友元函數

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
using namespace std;

class Complex
{
public:
    Complex();
    Complex(float real, float image);

    virtual ~Complex();

    friend Complex operator+(const Complex &c1, const Complex &c2);
    friend Complex operator-(const Complex &c1, const Complex &c2);
    friend Complex operator*(const Complex &c1, const Complex &c2);
    friend Complex operator/(const Complex &c1, const Complex &c2);

    friend ostream & operator<<(ostream &os, const Complex &c);

private:
    float m_real;
    float m_image;
};

#endif // COMPLEX_H
           

Complex.cpp

#include "Complex.h"

Complex::Complex()
{

}

Complex::Complex(float real, float image)
{
    m_real = real;
    m_image = image;
}

//重載+運算符
Complex operator+(const Complex &c1, const Complex &c2)
{
    Complex temp;
    temp.m_real = c1.m_real + c2.m_real;
    temp.m_image = c1.m_image + c2.m_image;

    return temp;
}

//重載-運算符
Complex operator-(const Complex &c1, const Complex &c2)
{
    Complex temp;
    temp.m_real = c1.m_real - c2.m_real;
    temp.m_image = c1.m_image - c2.m_image;

    return temp;
}

//重載*運算符
Complex operator*(const Complex &c1, const Complex &c2)
{
    Complex temp;
    temp.m_real = (c1.m_real * c2.m_real) - (c1.m_image * c2.m_image);
    temp.m_image =(c1.m_image * c2.m_real) + (c1.m_real * c2.m_image);

    return temp;
}

//重載/運算符
Complex operator/(const Complex &c1, const Complex &c2)
{
    Complex temp;
    temp.m_real = (c1.m_real * c2.m_real + c1.m_image * c2.m_image) /
                  (c2.m_real * c2.m_real + c2.m_image * c2.m_image);

    temp.m_image =(c1.m_image * c2.m_real - c1.m_real * c2.m_image) /
                  (c2.m_real * c2.m_real + c2.m_image * c2.m_image);

    return temp;
}

//重載<<運算符
ostream & operator<<(ostream &os, const Complex &c)
{
    os << c.m_real << "+" << c.m_image << "i" << endl;
    return os;
}

Complex::~Complex()
{

}
           

main.cpp

#include "Complex.h"

int main()
{
    Complex a(1, 2);
    Complex b(2.0, 3);
    Complex c = a + b;
    cout << c;

    c = a - b;
    cout << c;

    c = a * b;
    cout << c;

    c = a / b;
    cout << c;

    return 0;
}
           

輸出:

3+5i
-1+-1i
-4+7i
0.615385+0.0769231i