实现负数中的运算符的重载

* Copyright (c) 2012, 烟台大学计算机学院
* All rights reserved.
* 作    者:庄子豪
* 完成日期:2013 年  4月19日
* 版 本 号:v1.0
* 输入描述:
* 问题描述:
* 程序输出:
* 问题分析:
 *算法设计:

#include 

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)
{
    Complex c;
    c.real=real+c2.real;
    c.imag=imag+c2.imag;
    return c;
}
Complex Complex::operator-(Complex &c2)
{
    Complex c;
    c.real=real-c2.real;
    c.imag=imag-c2.imag;
    return c;
}
Complex Complex::operator*(Complex &c2)
{
    Complex c;
    c.real=real*c2.real+imag*c2.imag;
    c.imag=real*c2.imag+imag*c2.real;
    return c;
}
Complex Complex::operator/(Complex &c2)
{
    Complex c;
    double x;
    x=(c2.real+c2.imag)*(c2.real-c2.imag);
    c.real=(real*c2.real+imag*(-c2.imag))/x;
    c.imag=(real*(-c2.imag)+imag*c2.real);
    return c;
}
void Complex::display()
{
        cout<<"("<

你可能感兴趣的:(实现负数中的运算符的重载)