C++ 类模板重载运算符实现复数运算

C++ 类模板重载运算符实现复数运算

注:类模板中函数的实现和声明必须在同一个文件中,可放在.hpp文件中,不能分开放在.cpp和.h文件中

1、重载为成员函数

Complex.hpp

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
using namespace std;

template <typename T1, typename T2>
class Complex
{
public:
    Complex();
    Complex(T1 real, T2 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)
    {
        os << c.m_real << "+" << c.m_image << "i" << endl;
        return os;
    }

private:
    T1 m_real;
    T2 m_image;
};

template <typename T1, typename T2>
Complex<T1, T2>::Complex()
{

}

template <typename T1, typename T2>
Complex<T1, T2>::Complex(T1 real, T2 image)
{
    m_real = real;
    m_image = image;
}

//重载+运算符
template <typename T1, typename T2>
Complex<T1, T2> Complex<T1, T2>::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;
}

//重载-运算符
template <typename T1, typename T2>
Complex<T1, T2> Complex<T1, T2>::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;
}

//重载*运算符
template <typename T1, typename T2>
Complex<T1, T2> Complex<T1, T2>::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;
}

//重载/运算符
template <typename T1, typename T2>
Complex<T1, T2> Complex<T1, T2>::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;
}

template <typename T1, typename T2>
Complex<T1, T2>::~Complex()
{

}

#endif // COMPLEX_H

main.cpp

#include "Complex.hpp"

int main()
{
    Complex<float, float> a(1, 2);
    Complex<float, float> b(2, 3);
    Complex<float, float> c;

    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.hpp

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
using namespace std;

template <typename T1, typename T2>
class Complex
{
public:
    Complex();
    Complex(T1 real, T2 image);

    virtual ~Complex();

    //友元重载+运算符
    friend 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;
    }

    //友元重载-运算符
    friend 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;
    }

    //友元重载*运算符
    friend 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;
    }

    //友元重载/运算符
    friend 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;
    }

    //友元重载<<运算符
    friend ostream& operator <<(ostream& os, const Complex& c)
    {
        os << c.m_real << "+" << c.m_image << "i" << endl;
        return os;
    }

private:
    T1 m_real;
    T2 m_image;
};

template <typename T1, typename T2>
Complex<T1, T2>::Complex()
{

}

template <typename T1, typename T2>
Complex<T1, T2>::Complex(T1 real, T2 image)
{
    m_real = real;
    m_image = image;
}

template <typename T1, typename T2>
Complex<T1, T2>::~Complex()
{

}

#endif // COMPLEX_H

main.cpp

#include "Complex.hpp"

int main()
{
    Complex<float, float> a(1, 2);
    Complex<float, float> b(2, 3);
    Complex<float, float> c;

    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
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值