您的位置:首页 > 编程语言 > C语言/C++

C++:复数类构造函数、拷贝构造、运算符重载、析构函数

2016-01-18 13:59 447 查看
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

class Complex
{
public:
void Set(double real, double image)
{
_real = real;
_image = image;
}

//构造函数
Complex(double real = 1, double image = 2)
{
cout << "缺省构造函数" << endl;
_real = real;
_image = image;
}

//拷贝构造函数
Complex(Complex& d)
{
_real = d._real;
_image = d._image;
}

//析构函数
~Complex()
{
cout << "析构函数" << endl;
}

//

void Display()
{
cout << _real << _image;
}

//等于
bool operator==(const Complex& d)
{
return _real  * _real + _image * _image
== d._real + d._real + d._image * d._image;
}

//小于
bool operator < (const Complex& d)
{
return _real  * _real + _image * _image
< d._real + d._real + d._image * d._image;
}

//大于
bool operator > (const Complex& d)
{
return _real  * _real + _image * _image
> d._real + d._real + d._image * d._image;
}
private:
double _real;
double _image;
};

int main()
{
Complex d1;
d1.Display();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 复数 拷贝构造