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

Posted 鱼竿钓鱼干

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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<<c<<b然后思考是否需要返回值。
  11. 设计操作函数考虑:成员还是非成员,传引用还是传值,是否需要const,是否需要内联
  12. 函数重载要注意,已经有默认值的情况,核心是编译器是否能分清

代码

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

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 <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;

//设计返回值考虑连续使用的情况,cout<<c1<<c2;
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;
  
  return 0;
}

以上是关于[C++ 面向对象高级编程]Complex类的实现过程的主要内容,如果未能解决你的问题,请参考以下文章

python 面向对象编程(高级篇)

python基础之面向对象高级编程

C++编程,定义一个复数类

C++面向对象高级编程(下) 第二周笔记 GeekBand

C++ 编程习惯与编程要点

JavaScript高级第一天学习总结—— 面向对象ES6 中的类和对象类的继承案例:面向对象版tab 栏切换