C++实现一个复数类详解编程语言
2023-06-13 09:11:49 时间
复数类的实现中包含了众多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; }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18212.html
cjavaxml相关文章
- C、C++ 成众矢之的,这群程序员发起编程语言“革命”!
- 2021年游戏项目的十大编程语言:C++、Java、C#均上榜「建议收藏」
- c++日志库实战——spdlog,是不是感觉log4cxx有点笨重,不妨试一试spdlog
- 使用C++ OpenCV实现椭圆区域检测与Aruco码的生成与检测并估计位姿
- 深入理解C++11_c++ string char
- c++字符串转int_python中lower的用法
- C++stl库_c++库
- C++结构体和类的区别_c++有结构体吗
- C++字符串加密_c++字符串连接函数
- C/C++ Qt 数据库与ComBox多级联动
- 【C++ 语言】 C 与 C++ 兼容 ( extern "C" )
- 两万字长文,史上最全 C++ 年度总结!
- C++中调用Python脚本详解编程语言
- C++三大特性之封装详解编程语言
- C++中的类继承之单继承&多继承&菱形继承详解编程语言
- C++运算符的重载详解编程语言
- 队列的存储结构的实现(C/C++实现)详解编程语言
- 栈的存储结构的实现(C/C++实现)详解编程语言
- C/C++中peek函数的原理及应用详解编程语言
- C/C++中对链表操作的理解&&实例分析详解编程语言
- C++STL中map容器的说明和使用技巧(杂谈)详解编程语言
- C++ STL之min_element()与max_element()(取容器中的最大最小值)详解编程语言
- c++重载小括号,实现仿函数详解编程语言
- C++string类的实现详解编程语言
- C++中的继承详解编程语言
- C++命名空间详解编程语言
- C/C++经典网站详解编程语言
- C++中的类型转换方法详解编程语言
- C++之多态性与虚函数详解编程语言
- VS Code 编译C++详解编程语言
- C++ deque使用、创建及初始化详解
- c++函数中的指针参数与地址参数区别介绍
- C++获取zip文件列表方法
- c++巧开平方的实现代码
- C++用指针变量作为函数的参数接受数组的值的问题详细总结
- C++基础入门教程(三):数组、字符串、结构体、共用体