类complex可作为入门C++面向对象的经典类。complex的原型是数学上的复数。我们都知道复数有实部和虚部。所以类complex有两个数据成员re, im。复数有加,减,乘,除,取反,求共轭等操作,类complex也有相对应的成员函数来实现这些操作。下面是类complex的定义和测试代码,已在vs测试过。

类complex的定义

//complex.h#ifndef _MYCOMPLEX
#define _MYCOMPLEXclass complex;complex& _doapl (complex* ths, const complex& r);
complex& _doami (complex* ths, const complex& r);
complex& _doaml (complex* ths, const complex& r);class complex
{
public://构造函数,采用初始化列表complex(double r = 0, double i = 0) : re(r), im(i) { }//返回complex&,允许 a+=b+=c 情况; 传参complex&,相比较传complex,效率更高complex& operator+= (const complex&);complex& operator-= (const complex&);complex& operator*= (const complex&);complex& operator/= (const complex&);//定义两个公有接口,方便普通函数访问私有成员;const禁止成员函数修改私有成员double real () const { return re; }double imag () const { return im; }private:double re, im;//声明为友元函数,可直接调用私有成员friend complex& _doapl (complex*, const complex&);friend complex& _doami (complex*, const complex&);friend complex& _doaml (complex*, const complex&);
};inline complex& _doapl (complex* ths, const complex& r)
{ths->re += r.re;ths->im += r.im;return *ths;
}//重载操作符+=,两复数相加
inline complex& complex :: operator += (const complex& r)
{return _doapl(this, r);
}inline complex& _doami(complex* ths, const complex& r)
{ths->re -= r.re;ths->im -= r.im;return *ths;
}//重载操作符-=,两复数相减
inline complex& complex :: operator -= (const complex& r)
{return _doami(this, r);
}inline complex& _doaml(complex* ths, const complex& r)
{double f = ths->re * r.re - ths->im * r.im;ths->im += ths->re * r.im + ths->im * r.re;ths->re = f;return *ths;
}//重载操作符*=,两复数相乘
inline complex& complex :: operator *= (const complex& r)
{return _doaml(this, r);
}//取实部
inline double imag(const complex& x)
{return x.imag();
}//取虚部
inline double real(const complex& x)
{return x.real();
}//重载操作符+,复数与复数相加(此处只能返回complex,因return的是一个局部变量complex)
inline complex operator+ (const complex&x, const complex& y)
{return complex(real(x) + real(y), imag(x) + imag(y));
}//重载操作符+,复数与实数相加
inline complex operator+ (const complex& x, double y)
{return complex(real(x) + y, imag(x));
}//重载操作符+,实数与复数相加
inline complex operator+ (double x, const complex& y)
{return complex(x + real(y), imag(y));
}//重载操作符-,复数与复数相减
inline complex operator- (const complex&x, const complex& y)
{return complex(real(x) - real(y), imag(x) - imag(y));
}//重载操作符-,复数与实数相减
inline complex operator- (const complex& x, double y)
{return complex(real(x) - y, imag(x));
}//重载操作符-,实数与复数相减
inline complex operator- (double x, const complex& y)
{return complex(x - real(y), imag(y));
}//重载操作符*,复数与复数相乘
inline complex operator* (const complex& x, const complex& y)
{return complex(real(x) * real(y) - imag(x) * imag(y),real(x) * imag(y) + imag(x) * real(y));
}//重载操作符*,复数与实数相乘
inline complex operator* (const complex& x, double y)
{return complex(real(x) * y, imag(x) * y);
}//重载操作符*,实数与复数相乘
inline complex operator* (double x, const complex& y)
{return complex(x * real(y), x * imag(y));
}//重载操作符/,复数与实数相除
inline complex operator/ (const complex& x, double y)
{return complex(real(x) / y, imag(x) / y);
}//取正
inline complex operator+ (const complex &x)
{return x;
}//取反
inline complex operator- (const complex &x)
{return complex(-real(x), -imag(x));
}//重载操作符==,复数等于复数
inline bool operator== (const complex& x, const complex& y)
{return real(x) == real(y) && imag(x) == imag(y);
}//重载操作符==,复数等于实数
inline bool operator== (const complex& x, double y)
{return real(x) == y && imag(x) == 0;
}//重载操作符==,实数等于复数
inline bool operator== (double x, const complex& y)
{return x == real(y) && imag(y) == 0;
}//重载操作符!=,复数不等于复数
inline bool operator!= (const complex& x, const complex& y)
{return real(x) != real(y) || imag(x) != imag(y);
}//重载操作符!=,复数不等于实数
inline bool operator!= (const complex& x, double y)
{return real(x) != y || imag(x) != 0;
}//重载操作符!=,实数不等于复数
inline bool operator!= (double x, const complex& y)
{return x != real(y) || imag(y) != 0;
}#include <cmath>
//极坐标求坐标
inline complex polar(double r, double t)
{return complex(r * cos(t), r * sin(t));
}//求共轭
inline complex conj(const complex& x)
{return complex(real(x), -imag(x));
}//求绝对值
inline double norm(const complex& x)
{return real(x) * real(x) + imag(x) * imag(x);
}#endif // !_MYCOMPLEX

测试代码

//complex_test.cpp#include <iostream>
#include "complex.h"using namespace std;
//重载操作符<<,可直接输出类complex;返回ostream&,允许cout << a << b 操作
ostream& operator<< (ostream& os, const complex& x)
{return os << '(' << real(x) << ',' << imag(x) << ')';
}int main()
{complex c1(2, 1);complex c2(4, 0);cout << c1 << endl;cout << c2 << endl;cout << c1 + c2 << endl;cout << c1 - c2 << endl;cout << c1 * c2 << endl;cout << c1 / 2 << endl;cout << conj(c1) << endl;cout << norm(c1) << endl;cout << polar(10, 4) << endl;cout << (c1 += c2) << endl;cout << (c1 == c2) << endl;cout << (c1 != c2) << endl;cout << +c2 << endl;cout << -c2 << endl;cout << (c2 - 2) << endl;cout << (5 + c2) << endl;system("pause");return 0;
}

C++面向对象之类complex详解相关推荐

  1. php面向对象代码_PHP面向对象之抽象类详解(代码实例)

    [摘要] PHP即"超文本预处理器",是一种通用开源脚本语言.PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言.PHP独特的语法混合了C.Java.Perl以及 ...

  2. Java 面向对象:封装详解

    Java 面向对象:封装详解 封装:属性私有,get/set 属性私有化 提供get/set方法操作数据 优点: 1.提高程序的安全性,保护数据 2.隐藏代码的实现细节 3.统一接口 4.系统可维护性 ...

  3. Java 面向对象:构造器详解

    Java 面向对象:构造器详解 构造器: 和类名相同 没有返回值,也不能写void 作用: new本质是调用构造方法 初始化对象的值 注意点: 定义有参构造之后,如果想使用无参构造,显示的定义一个无参 ...

  4. python数值类型教程_Python数值类型 int、float、complex 详解

    Python数值类型 int.float.complex 详解 Python数值类型:int.float.complex 在Python程序中,int.float和complex是三种十分重要的数值类 ...

  5. [转载] python支持complex吗_Python 内置函数complex详解

    参考链接: Python complex() 英文文档: class complex([real[, imag]]) Return a complex number with the value re ...

  6. python中的complex是什么意思_Python 内置函数complex详解,pythoncomplex

    Python 内置函数complex详解,pythoncomplex 英文文档: class complex([real[, imag]]) Return a complex number with ...

  7. JAVA面向对象三大特征详解:(封装,继承,多态)

    面向对象语言: 大家经常会听到说JAVA这门语言是面向对象的,但面向对象又是什么呢? 提到面向对象就要引入一个叫做面向过程的概念: 举个例子:把大象装进冰箱,需要几步. 面向过程的做法: 人走到冰箱前 ...

  8. php面向对象高级应用详解(2)

    魔术方法__get()实例详解 在PHP中以两个下划线开头的方法,被称为"魔术方法"(Magic methods).比如__construct(), __destruct (), ...

  9. python中complex函数的用法_Python 内置函数complex详解

    英文文档: class complex([real[, imag]]) Return a complex number with the value real + imag*1j or convert ...

  10. php面向对象 设计模式,PHP面向对象单态设计模式详解

    单态设计模式定义: PHP中单态模式主要作用是保证在面向对象编程设计中,一个类只能有一个实例对象存在. 为何用单态设计模式: 在很多php操作中,比如建立目录.数据库连接类中其实只需要有一个实例对象就 ...

最新文章

  1. 未来,AI可用于5G网络分析
  2. ZOJ 3781 最短路(想法好题目)
  3. 软件工程概论 课堂练习【静态结构建模——画出相应的对象模型】
  4. Kafka消息序列化和反序列化(上)
  5. 电脑f2还原系统步骤_使用冰点还原电脑每次重启都会还原,打造一个百毒不侵的系统...
  6. @EqualsAndHashCode()注解详解
  7. 容器编排技术 -- Kubernetes Pod 生命周期
  8. 干货|基于深度学习的目标检测算法面试必备(RCNN~YOLOv5)
  9. 华为云数据库TaurusDB性能挑战赛,50万奖金等你来拿!
  10. UTF8,Unicode 的区别(zt)
  11. 英特尔发布年度企业社会责任报告,首次定义“全球性挑战”
  12. php删除更新修改时间,php怎样获取文件的最后修改时间
  13. 四、ip classless与默认路由
  14. wsimport简单实现WebService
  15. zf:tf7: RNN—古诗词
  16. STL emplace_back
  17. Huawei RH2288 V3 风扇噪音大的解决方案
  18. 计算机毕业设计(附源码)python银行服务评价系统
  19. RNN代码简单实现(周杰伦歌词示例)
  20. 华为荣耀9青春版系统像鸿蒙,荣耀9青春版参数配置

热门文章

  1. 九阴真经 正在获取服务器列表信息,九阴真经4月9日更新内容介绍_九阴真经4月9日更新公告_飞翔教程...
  2. sqlserver 2000操作数据库
  3. excel公式编辑器_V14.0发布:组件化编辑器+数据透视表
  4. 关系数据库——关系操作关系模型的完整性
  5. 物流项目管理的团队建设 (zt)
  6. 编译一个java源程序文件,会产生多少个字节码文件
  7. dedecms源码分析(第一部分)
  8. Android图形编程篇--OpenGL实现三角形隧道效果
  9. All-one Matrices
  10. 多目标蜉蝣优化算法(MOMA)附Matlab代码