码迷,mamicode.com
首页 > 编程语言 > 详细

[c++]复数的运算符重载

时间:2015-06-07 09:49:23      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:c++   重载   

类的定义和声明

<span style="font-size:32px;">#include<iostream>
#include<string>
using namespace std;

class Complex
{
    friend istream& operator>>(istream &in, Complex &c);
    friend ostream& operator<<(ostream &out, const Complex &c);
    friend Complex operator+(int x, Complex &c);
public:
    Complex(const int real=0,const int imag=0):m_real(real),m_imag(imag)
    {}
    ~Complex()
    {}
    void Show()const
    {
        cout<<"("<<m_real<<","<<m_imag<<")"<<endl;
    }

public:
    Complex operator+(Complex &c)
    {
        return Complex(m_real+c.m_real,m_imag+c.m_imag);
    }

    Complex operator-(Complex &c)
    {
        return Complex(m_real-c.m_real,m_imag-c.m_imag);
    }


    Complex operator*(Complex &c);
    Complex operator/(Complex &c);
    Complex operator+=(Complex &c);
    Complex operator-=(Complex &c);
    Complex operator*=(Complex &c);
    Complex operator/=(Complex &c);
    Complex operator+(int x);
    //friend Complex operator+(int x, Complex &c);
private:
    int m_real;
    int m_imag;
};


Complex Complex::operator+(int x)
{
    return Complex(m_real+x,m_imag);
}

Complex Complex:: operator+=(Complex &c)
{
    m_real = m_real+c.m_real;
    m_imag = m_imag+c.m_imag;
    return *this;
}

Complex Complex::operator-=(Complex &c)
{
    m_real = m_real - c.m_real;
    m_imag = m_imag - c.m_imag;
    return *this;
}


Complex Complex:: operator*(Complex &c)
{
    return Complex(m_real*c.m_real-m_imag*c.m_imag,m_imag*c.m_real+m_real*c.m_imag);
}

Complex Complex::operator*=(Complex &c)
{
    int i = m_real;
    m_real = m_real*c.m_real-m_imag*c.m_imag;
    m_imag = m_imag*c.m_real+i*c.m_imag;
    return *this;
}

Complex Complex::operator/(Complex &c)
{
    Complex temp;

    temp.m_real = (m_real*c.m_real+m_imag*c.m_imag)/(c.m_real*c.m_real+c.m_imag*c.m_imag);
    temp.m_imag = (m_imag*c.m_real-m_real*c.m_imag)/(c.m_real*c.m_real+c.m_imag*c.m_imag);
    return temp;
}

Complex Complex:: operator/=(Complex &c)
{
    int i = m_real;
    m_real = (m_real*c.m_real+m_imag*c.m_imag)/(c.m_real*c.m_real+c.m_imag*c.m_imag);
    m_imag = (m_imag*c.m_real-i*c.m_imag)/(c.m_real*c.m_real+c.m_imag*c.m_imag);
    return *this;
}
ostream& operator<<(ostream &out, const Complex &c)
{
    out<<"("<<c.m_real<<","<<c.m_imag<<")";
    return out;
}

istream& operator>>(istream &in, Complex &c)
{
    in>>c.m_real>>c.m_imag;
    return in;
}
</span>
测试程序:

int main()
{
    Complex c1(1,1);
    Complex c2(1,-1);
    
    Complex c;
    //c1.operator+(c2)
    c = c1 + c2;
    cout<<c<<endl;
//    c = c1 * c2;
//    cout<<c<<endl;
//    c = c1 / c2;
//    cout<<c<<endl;
//    c = 2 + c1;
//    cout<<c<<endl;
//    c1+=c2;
//    cout<<c1<<endl;
//    c1-=c2;
//    cout<<c1<<endl;
//    c1*=c2;
//    cout<<c1<<endl;
//    c1/=c2;
//    cout<<c1<<endl;
//请不要把它们一起测试,分开
    Complex c3;
    cin>>c3;
    cout<<c3;
    return 0;
}


[c++]复数的运算符重载

标签:c++   重载   

原文地址:http://blog.csdn.net/cherry_ermao/article/details/46389979

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!