实现复数类的运算符重载

/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: object.cpp
* 作者:王飞
* 完成日期:2013 年4月 19日
* 版本号: v1.0
* 输入描述:无
* 问题描述:处理虚数类
* 程序输出:如下
*/


#include 

using namespace std;

class Complex           //声明类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;
    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;
}

void Complex::display()
{
    cout<<"("<

你可能感兴趣的:(C++基础)