栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

[C++ 面向对象高级编程]Complex类的实现过程

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[C++ 面向对象高级编程]Complex类的实现过程

[C++ 面向对象高级编程]Complex类的实现过程 文章概述

该文章为侯捷教授 C++面向对象高级编程 课程的个人笔记,非常值得一看,收获很多。

作者信息

NEFU 2020级 zsl
ID:fishingrod/鱼竿钓鱼干
Email:851892190@qq.com
欢迎各位引用此博客,引用时在显眼位置放置原文链接和作者基本信息

正文部分 要点准则
  1. 使用防卫式声明,方式头文件重复定义
  2. 使用构造函数初始化列表,设计模式中的单例模式可能把构造函数放在private中
  3. 尽量使用传引用(本质是指针)代替传值(如果是内置类型double,int之类的传值更快,STL容器也建议传值),没有创建新的对象就可以使用
  4. 如果不会发生改变一定要加const
  5. 能内联的最好内联,inline只是建议,编译器不一定会执行
  6. friend友元函数可以拿private的数据,但是破坏了封装
  7. 数据放在private里,进行封装
  8. 在调用成员函数时,调用者将指向其自身object的指针作为隐藏参数传递给函数
    参数名为this,不需要在函数的参数列表中定义
  9. 可以使用typename ()创建临时对象,作用域为该语句
  10. 重载操作符:考虑连续使用的情况c+=b+=a,cout<
  11. 设计操作函数考虑:成员还是非成员,传引用还是传值,是否需要const,是否需要内联
  12. 函数重载要注意,已经有默认值的情况,核心是编译器是否能分清
代码

看了一下现在C++ 库发现复杂的很,用了挺多模板和其他玩意的。所以还是直接看侯捷先生发的课件代码

complex.h

//防卫式头文件声明,防止头文件重复定义
#ifndef __MYCOMPLEX__	
#define __MYCOMPLEX__

//前置声明
class 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& 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;

  //友元函数,可以直接访问private数据
  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;
}
 
//成员函数隐藏了this参数
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);
}
 
//使用传引用代替传值,如果不改变那就const+引用
inline double
imag (const complex& x)
{
  return x.imag ();
}

inline double
real (const complex& x)
{
  return x.real ();
}

//因为这里是创建新对象了所以不能返回引用
inline complex
operator + (const complex& x, const complex& y)
{
  //使用typename()创建临时对象,语句结束对象就消失
  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));
}

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 

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 
#include "complex.h"

using namespace std;

//设计返回值考虑连续使用的情况,cout<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/429515.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号