C++实现一个复数类
来源:互联网 发布:淘宝网针织连衣裙 编辑:程序博客网 时间:2024/05/16 01:02
复数类的实现中包含了众多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;}
阅读全文
0 0
- 【c++】实现一个复数类
- 实现一个复数类
- 【C++】实现复数类
- 【C++】用拷贝函数实现一个复数类(complex)
- 如何实现一个复数类
- C++实现一个复数类
- C++实现一个复数类
- [c++]复数类的实现
- 【C++】复数类的实现
- 【C++】复数类的实现
- 【C++】复数类的实现
- 编写一个类实现复数的运算
- C++ 声明并实现一个复数类
- 【C++】实现一个复数类(complex)(带有默认参数的构造函数 )
- 【c/c++】复数类
- 【C++】模拟实现一个复数类,要求实现 加,减,乘,除等基本运算符的重载
- 构建一个复数类
- 【c++】复数加减法类
- 二、Spring知识汇总
- 机器学习路线
- springMVC通过getBean()获取context.xml文件中定义的service接口类
- php流程控制的goto
- webpack配置文件详解(二)
- C++实现一个复数类
- MFC整型和字符串互相转换
- opencv
- 添加好友(快速幂 金马五校赛-上海大学)
- extern "c"{}
- Ubuntu遇到的错误总结
- Java精选笔记_JSTL(JSP标准标签库)
- 强联通注意点
- deepin激活SCRT