C++中的复数Complex类

来源:互联网 发布:文明6mac破解版 编辑:程序博客网 时间:2024/05/22 11:45

Complex类的成员变量有实部与虚部:

                                       protected: 

                                                       double _real; 

                                                       double _image;

本次将实现在C++中的Complex类的基本函数,包括四个默认成员函数,比较运算符重载函数,赋值运算符重载函数,以及前置++与后置++的重载。

四个默认成员函数是(1)构造函数:在定义对象时初始化成员变量,函数名与类名相同,无返回值(void也不能有),对象实例化时系统自动调用

                                    (2)拷贝构造函数:是一种特殊的构造函数,是对构造函数的重载,用同类对象初始化成员变量

                                    (3)析构函数:函数名是在类名前加~,无参数无返回值,函数作用是做一些清理工作,在对象生命周期结束时系统自动调用

                                    (4)赋值运算符重载:operator+合法的运算符构成运算符重载

下面是编写的代码,在默认成员函数中我加上了一些输出,便于清楚看到成员函数是怎么运行的

#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>using namespace std;class Complex{public:Complex(double real = 8.0, double image = 6.0)       //构造函数:_real(real), _image(image){cout <<" Complex(double real, double image)" << endl;}Complex(const Complex& d)          //拷贝函数{cout << "Complex(const Complex& d)" << endl;_real = d._real;_image = d._image;}~Complex()       //析构函数{cout << "~Complex() " << endl;_real = 0.0;_image = 0.0;}Complex& operator=(const Complex& d)         //赋值运算符重载{cout << "=" << endl;if (this != &d){_real = d._real;_image = d._image;}return *this;}void Display()const                //打印复数{cout << "_real:"<< _real;cout << "      _image:" << _image << endl;}bool operator==(const Complex& d)             //=={cout << "==" << endl;return this->_real == d._real&& this->_image == d._image;}bool operator!=(const Complex& d)             //!={cout << "!=" << endl;return this->_real != d._real|| this->_image == d._image;}//复数只有当虚部为0时,即_image=0时,才可以比较大小,这时比较的是实部即_real的大小bool operator>(const Complex& d)          //>{if (this->_image != 0 || d._image != 0){cout << "无法比较       " ;return false;}else{return this->_real > d._real;}}bool operator<(const Complex& d)           //<{if (this->_image != 0 || d._image != 0){cout << "无法比较      ";return false;}else{return this->_real < d._real;}}bool operator<=(const Complex& d)           //<={if (this->_image != 0 || d._image != 0){cout << "无法比较      ";return false;}else{return this->_real <= d._real;}}bool operator>=(const Complex& d)           //>={if (this->_image != 0 || d._image != 0){cout << "无法比较        ";return false;}else{return this->_real >= d._real;}}Complex operator+ (const Complex& d)           //+{cout << "+" << endl;Complex ret;ret._real = (this->_real + d._real);ret._image = (this->_image + d._image);return ret;}Complex& operator+=(const Complex& d)          //+={cout << "+=" << endl;this->_real += d._real;this->_image += d._image;return *this;}Complex& operator++()            //前置++{cout << "前置++" << endl;this->_real += 1;return *this;}Complex operator++(int)         //后置++{cout << "后置++" << endl;Complex tmp(*this);this->_real += 1;return tmp;}protected:double _real;double _image;};
1、测试用例Test()------测试构造函数,以及==和!=以及+运算符的重载

void Test(){ Complex d1;Complex d2(4.0,6.6);d1.Display();d2.Display();//d1 = d2;//d1.Display();Complex d3;d3 = d1 + d2;cout << d1.operator==(d2) << endl;cout << d1.operator!=(d2) << endl;d3.Display();}
运行结果:


我们可以看到系统调用函数的顺序,析构函数是清理工作,当生命周期结束时系统自动调用。

2、测试用例Test1()------------测试+=运算符

void Test1(){Complex d1;Complex d2(4.0, 9.6);d1.Display();d2.Display();d1 += d2;d1.Display();d2.Display();}

运行结果:


3、测试用例Test2()---------测试前置++与后置++

void Test2(){Complex d1;Complex d2(0, 0);d2 = ++d1;d2.Display();d1.Display();Complex d3(1.0, 2.0);d3 = d1++;d3.Display();d1.Display();}
运行结果:




4、测试用例Test3()--------测试>/>=/</<=比较运算符

void Test3(){Complex d1(7,0);Complex d2(6, 0);Complex d3;Complex d4(6, 0);cout << d1.operator>(d2) << endl;cout << d1.operator<(d2) << endl;cout << d3.operator<=(d4) << endl;cout << d2.operator>=(d4) << endl;}
运行结果:



原创粉丝点击