复数类:class Complex
2017-07-01 19:48
323 查看
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; class Complex { public: //构造函数 Complex(double real = 0.0, double image = 0.0) :_real(real) ,_image(image) { ; } //拷贝构造 Complex(const Complex& c) :_real(c._real) ,_image(c._image) { ; } //析构 ~Complex() { ; } void Display() { cout<<"实部:"<<_real<<" 虚部:"<<_image<<endl; } bool operator>(const Complex&) const; bool operator<(const Complex&) const; bool operator==(const Complex&) const; bool operator!=(const Complex&) const; Complex operator+(const Complex&) const; Complex& operator=(const Complex&); Complex& operator+=(const Complex&); Complex& operator++(); //前置++ Complex operator++(int); //后置++ private: double _real; double _image; }; bool Complex::operator>(const Complex& c) const { return _real > c._real && _image > c._image; } bool Complex::operator==(const Complex& c) const { if (_real == c._real && _image == c._image) { return true; } return false; } bool Complex::operator<(const Complex& c) const { return !(*this>c || *this==c); } bool Complex::operator!=(const Complex& c) const { return !(*this == c); } Complex Complex::operator+(const Complex& c) const { Complex tmp; tmp._real = _real + c._real; tmp._image = _image + c._image; return tmp; } Complex& Complex::operator+=(const Complex& c) { _real += c._real; _image += c._image; return *this; } Complex& Complex::operator++() //前置++,返回++以后的值 { _real += 1; return *this; } Complex Complex::operator++(int) //后置++,返回++以前的值 { Complex tmp(*this); tmp._real += 1; return tmp; } Complex& Complex::operator=(const Complex& c) { if(this != &c) //防止自己给自己赋值,注:a和b值一样的话也会赋值 { _real = c._real; _image = c._image; } return *this; }
相关文章推荐
- 面向对象复数类(class Complex) 的重载运算符 一元二元的实现与使用
- 采用C/C++语言如何实现复数抽象数据类型Complex
- A class extends ArrayList, but the instance of the calss do not work in a complex case.
- Class Complex
- 运算符重载(复数类(Complex))
- complex(x):创建一个复数
- 简单的问题,用类创建一个对象error C2380: type(s) preceding 'Complex' (constructor with return type, or illegal redefinition of current class-n
- 关于C语言中的Complex(复数类型)和imaginary(虚数类型)(
- java定义类complex,复数加减
- ||Python||complex复数类型的简单介绍
- 定义一个复数类Complex,重载运算符"+"
- C99 新增了复数类型(_Complex)和虚数类型(_Imaginary)。简单来说,C99 提供了三种复数类型:float _Complex,double _Complex,和 long do
- servlet-class :cvc-complex-type.2.4.a
- 定义一个复数类complex重载运算符+ - * /使之能用于复数的加减乘除。
- 定义一个复数类Complex,重载运算符“+”,
- 复数类模板(Complex Class)
- 定义一个复数类Complex,使得下面的代码能够工作 Complex c1(3,5); //用复数3+5i初始化c1 Complex c2=4.5; //用实数4.5初始化c2 c1.add(c2)
- 使用complex容器类处理复数数据
- 复数(Complex)类及FFT的C++实现
- C++实现复数Complex