2441复数类重载运算符

/*
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: array.cpp
* 作    者:   李中意
* 完成日期:2014  年7月 7  日
* 版本号: v1.0
* 输入描述:无
*/
#include <iostream>

#include <iomanip>

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+(int &i);

    friend Complex operator+(int&,Complex &);

    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+(int &i)
{
    Complex c;
    c.real=real+i;
    c.imag=imag;
    return c;
}
Complex operator+(int&i,Complex &c2)
{
    Complex c;
    c.real=i+c2.real;
    c.imag=c2.imag;
    return c;
}
void Complex::display()
{
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<"("<<real<<","<<imag<<"i)"<<endl;
}
/* C++代码 */



int main()

{

    double real,imag;

    cin>>real>>imag;

    Complex c1(real,imag);

    cin>>real>>imag;

    Complex c2(real,imag);

    cout<<setiosflags(ios::fixed);

    cout<<setprecision(2);

    Complex c3=c1+c2;

    cout<<"c1+c2=";

    c3.display();

    int i;

    cin>>real>>imag;

    cin>>i;

    c3=Complex(real,imag)+i;

    cout<<"c1+i=";

    c3.display();

    cin>>i;

    cin>>real>>imag;

    c1=Complex(real,imag);

    c3=i+c1;

    cout<<"i+c1=";

    c3.display();

    return 0;

}

 


2441复数类重载运算符_第1张图片

你可能感兴趣的:(2441复数类重载运算符)