当前位置: 首页 > news >正文

class complex

class complex from C++_OOP_base1_houjie

complex.h

#ifndef __COMPLEX__ // 防卫式声明 guard; 名称自定义
#define __COMPLEX__// 0. forward declarations
class complex;complex& __doapl (complex* ths, const complex& r);// 1. class declarations
class complex // class head
{
public: // access level// constructor (ctor): the name same with the class name; automatically called;// ctor without return typecomplex (double r = 0, double i = 0) // default argument: re (r), im (i) // initialization list 初始化要早于{}内赋值{ } // no return; {}内还可做其他事情,此处无需;// operator overloading of member function (has this pointer)complex& operator += (const complex&);// const member functiondouble real () const { return re; } // inline funcdouble imag () const { return im; }
private: // visible within classdouble re, im; // datafriend complex& __doapl (complex*, const complex&);
};// 2. class definition
// 2.1 global function
inline double real (const complex& x)
{return x.real();
}inline double imag (const complex& x)
{return x.imag();
}// operator overloading of non member function, 因为复数不仅仅只能够加复数 ,所以设置为global func而非member func
// return by value, becase the return is a local object (typename () is a temp object 临时对象, e.g., 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) // c + 5
{return complex (real (x) + y, imag (x));
}inline complex operator + (double x, const complex& y) // 5 + c
{return complex (x + real (y), imag (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 real (y) == 0 && imag (y) == x;
}inline bool operator != (const complex& x, const complex& y)
{return real (x) != real (y) || imag (x) != imag (y);
}// ...inline complex conj (const complex& x) // 共轭
{return complex (real (x), -imag (x));
}// << 会将右边的东西作用到左边身上,比如作用到cout(标准输出流), 但cout又不 认识complex
// << 不能写成member func,只能写作global func,其他操作符都可以
// 若写作member func,那么左操作数必须是类的对象,然而<< 左边一般是cout...
#include <iostream>
// os不能是const,因为在往cout丢东西的时候,其实都在改变它的状态,所以得允 许修改
// cout的返回值仍然是ostream,所以我们设计的时候返回值是ostream,然后os又不是local的,so by reference.
std::ostream& operator << (std::ostream& os, const complex& x) // param os can be 'cout', which is a obj, and the typename is ostream
{return os << "(" << real (x) << ","<< imag (x) << ")";
}// __doapl返回指针指向的object,是传递者
// 而complex&是接收者,可以是object,也可以是reference
// 传递者是无需知道接收者以什么形式接受(value or reference)
// __doapl是friend,所以可以直接访问private中的data
inline complex& __doapl (complex* ths, const complex& r)
{ths->re += r.re;ths->im += r.im;return *ths;
}// 2.2 member function
// 任何一个member func都有一个隐藏的this pointer, 指向调用者
// 返回值是complex&是考虑到连续使用+=
inline complex& complex::operator += (const complex& r) // param this hidden here
{return __doapl (this, r);
}
#endif

complex-text.cpp

#include <iostream>
#include "complex.h"using namespace std;int main()
{complex c1(2, 1);complex c2;cout << c1 << endl;cout << c2 << endl;c2 = c1 + 5;c2 = 7 + c1;c2 = c1 + c2;c2 += c1;c2 += 3;c2 = -c1;cout << (c1 == c2) << endl;cout << (c1 != c2) << endl;cout << conj(c1) << endl;return 0;
}

keypoint

  1. 数据放在private中,绝大部分函数放在public中;
  2. 函数传参尽量pass by reference,考虑是否要加const;
  3. 函数返回值尽量return by reference;
  4. constructor使用initialization list;
  5. member function应该加const时需要加上;
http://www.lryc.cn/news/304093.html

相关文章:

  • 数据库系统概论整理与总结
  • 打通新势力NAS权限壁垒,绿联私有云安装Portainer,实现更强大的Docker功能
  • 前端基础自学整理|DOM树
  • RedisDesktopManager无法远程连接到Linux虚拟机中Redis的docker容器的一种解决方案
  • HarmonyOS 权限 介绍
  • 算法训练营day33(补),复习二叉树1
  • k8s-权限管理
  • 四.QT5工具安装和环境变量的配置
  • 为什么需要MDL锁
  • nuxt项目搭建
  • RocketMQ消息队列(上)
  • 【机器学习】机器学习是什么以及有哪些应用场景
  • vue3 #跨组件通信
  • 【AI绘画工具有哪些?】讲解
  • 在Vue中使用TypeScript时 props指定枚举类型
  • 快速将excel/word表格转换为web页面(html)的方法
  • 想高薪就业鸿蒙HarmonyOS 开发岗位,到底该学习些啥?
  • Java中的建造者模式
  • 机器学习面试:逻辑回归与朴素贝叶斯区别
  • 数据结构之线性表
  • 记录解决uniapp使用uview-plus在vue3+vite+ts项目中打包后样式不能显示问题
  • 三年功能测试,测试工作吐槽
  • 0206-1-网络层
  • 以 All-in-One 模式安装 KubeSphere时避坑
  • Android T 远程动画显示流程其二——动画的添加流程(更新中)
  • Pytorch-SGD算法解析
  • 物联网土壤传感器简介
  • MySQL索引面试题(高频)
  • SouthLeetCode-打卡24年02月第2周
  • Rust CallBack的几种写法