C++实现一个复数类
2017-07-18 12:32
399 查看
复数类的实现中包含了众多C++的基础知识,对学习C++有很大的帮助。其中,运算符重载中的“前置++、–和后置++、–”稍微难些,需注意理解它的参数设计和返回值。
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; class Complex { private: double _real; double _image; 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() { } //赋值运算符的重载 Complex operator = (const Complex& c) { this->_real = c._real; this->_image = c._image; return *this; } public://复数“==”、“+”、“-”、“+=”、“-=”的情况 //判断相等 bool operator == (const Complex& d) { return ((_real == d._real) && (_image == d._image)); } //两个复数相加 Complex operator+(const Complex& d) { Complex tmp; tmp._real = _real + d._real; tmp._image = _image + d._image; return tmp; } //两个复数相减 Complex operator-(const Complex& d) { Complex tmp; tmp._real = _real - d._real; tmp._image = _image - d._image; return tmp; } //复数对象-=d Complex& operator+=(const Complex& d) { _real += d._real; _image += d._image; return *this; } //复数对象-=d Complex& operator-=(const Complex& d) { _real -= d._real; _image -= d._image; return *this; } public://复数“前置++、--”和“后置++、--” //前置++ /*Complex operator++() { ++_real; ++_image; return *this; }*/ Complex& operator++() { this->_real++; this->_image++; return *this; } //前置-- /*Complex operator--() { --_real; --_image; return *this; }*/ Complex& operator--() { this->_real--; this->_image--; return *this; } //后置++ Complex operator++(int) { Complex tmp = *this; _real++; _image++; return tmp; } //后置-- Complex operator--(int) { Complex tmp = *this; _real--; _image--; return tmp; } //打印复数 void Display() { cout << _real << "+" << _image << "i" << endl; } }; //测试函数 void Test1() { cout << "初始化一个复数类" << endl; Complex c1(1.0, 2.0);//调用构造函数,新建复数类c1 c1.Display(); Complex c2(c1);//调用拷贝构造函数,新建复数类c2 c2.Display(); cout << endl; } void Test2() { cout << "初始化一个复数类" << endl; Complex c1(1.0, 2.0);//调用构造函数,新建复数类c1 c1.Display(); Complex c2(c1);//调用拷贝构造函数,新建复数类c2 c2.Display(); //测试operator== cout << endl << "测试operator==" << endl; int ret = c1 == c2; if (ret) { cout << "c1 == c2" << endl; } else { cout << "c1 != c2" << endl; } //测试operator+ cout << endl << "测试operator+" << endl; Complex c3 = c1 + c2; c3.Display(); //测试operator- cout << endl << "测试operator-" << endl; Complex c4 = c1 - c2; c4.Display(); //测试opertor+= cout << endl << "测试operator+=" << endl; c1 += c2; c1.Display(); //测试opertor-= cout << endl << "测试operator-=" << endl; c1.operator-= (c2); c1.Display(); cout << endl; } //测试前置++、--和后置++、-- void Test3() { cout << "初始化一个复数类" << endl; Complex c1(1.0, 2.0); c1.Display(); //前置++ cout << endl << "测试前置++" << endl; Complex c2 = ++c1; c2.Display(); //前置-- cout << endl << "测试前置--" << endl; Complex c3 = --c1; c3.Display(); c1.Display(); //后置++ cout << endl << "测试后置++" << endl; Complex c4 = c1++; c4.Display(); c1.Display(); //后置-- cout << endl << "测试后置--" << endl; Complex c5 = c1--; c5.Display(); c1.Display(); cout << endl; } int main() { Test1(); Test2(); Test3(); system("pause"); return 0; }
相关文章推荐
- 【C++】用拷贝函数实现一个复数类(complex)
- 【C++】实现一个复数类(complex)(带有默认参数的构造函数 )
- 【C++】模拟实现一个复数类,要求实现 加,减,乘,除等基本运算符的重载
- C++ 声明并实现一个复数类
- C++实现一个复数类
- 使用c++实现一个FTP客户端(二)
- 在 C++ 中实现一个轻量的标记清除 gc 系统
- 复数(Complex)类及FFT的C++实现
- [转] 一个大数相乘的C/C++实现
- 如何用C++实现一个LRU Cache(转载)
- C++实现的一个哈希表类
- 用C++实现,输入一个日期,输出它是一年中的第几天。
- coding interview guide-仅用递归函数和栈操作逆序一个栈-c++实现
- 用C++实现一个不能被继承的类
- C++实现一个栈,出栈,入栈,返回最小值 时间复杂度为O(1),查找字符串中第一个重复出现的字符
- 对于一个频繁使用的短小函数,在C语言中应用什么实现,在C++中应用什么实现?
- Hypertable 简介 (Hypertable一个 C++ 的Bigtable开源实现)
- 自己动手实现一个C++智能指针
- 用C++实现HTTP服务器 - 如何控制一个套接字的发送带宽
- 一个C++的内存池和内存管理的实现(一)