[C++] C++面向对象高级开发:complex类

课程目标

(1)培养正规的,大气的编程习惯

(2)以良好的方式编写C++ class —— Object Based(基于对象)
class without pointer members,例如,Complex
class with pointer members,例如,String

(3)学习Classes之间的关系 —— Object Oriented(面向对象)
继承(inheritance)
复合(composition)
委托(delegation)

Object Based vs Object Oriented

Object Based:面对的是单一class的设计
Object Oriented:面对的是多重classes的设计,classes和classes之间的关系

C++程序代码的基本形式

(1)header files(头文件)
Classes Declaration(声明)

(2)主程序

#include   // 引入标准库头文件,使用尖括号
#include "complex.h"   // 引入自己写的头文件,使用双引号

...

(3)标准库
Standard Library

注:
头文件的扩展名不一定是.h,主程序的扩展名不一定是.cpp

头文件中的防卫式声明

例如,complex.h,使用防卫式声明防止头文件被多次引用。

#ifndef __COMPLEX__  // 防卫式声明
#define __COMPLEX__  // 防卫式声明

...

#endif               // 防卫式声明

头文件的布局

#ifndef __COMPLEX__  // 防卫式声明
#define __COMPLEX__  // 防卫式声明
// - - - - - - - - - - - - - - - - - - - - - - - - - - -
                     // 前置声明

#include 

class ostream;
class complex;

complex& __doapl (complex* ths, const complex& r);
// - - - - - - - - - - - - - - - - - - - - - - - - - - -
                     // 类声明

class complex
{
...
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - -
                     // 类定义

complex::function ...
// - - - - - - - - - - - - - - - - - - - - - - - - - - -
#endif               // 防卫式声明

类的声明

class complex
{
public:  // 访问级别
  complex (double r=0, double i=0)
    : re(r), im(i)
  { }
  complex& operator += (const complex&);

  // 有些函数在此直接定义,另一些在body之外定义
  double real () const { return re; }
  double imag () const { return im; }
private:  // 访问级别
  double re, im;

  friend complex& __doapl (complex*, const complex&);
};

注:
函数若在class body内定义完成,便自动成为inline候选人
编译器决定最终是否inline。

在body之外定义的函数,使用inline关键字,使之成为内联函数(inline function)。

使用complex类的例子,

{
  complex c1(2, 1);
  complex c2;
  ...
}

类模板

template
class complex
{
public:
  complex (T r=0, T i=0)
    : re(r), im(i)
  { }
  complex& operator += (const complex&);

  // 有些函数在此直接定义,另一些在body之外定义
  T real () const { return re; }
  T imag () const { return im; }
private:
  T re, im;

  friend complex& __doapl (complex*, const complex&);
};

使用类模板的例子,

{
  complex c1(2, 1);
  complex c2;
  ...
}

构造函数

complex (double r=0, double i=0)
  : re(r), im(i)
  { }

注:
(1)构造函数的名称必须与类名相同
(2)本例中的构造函数的参数有默认值
(3)构造函数,可以有初始化列表(initialization list),并且推荐用初始化列表。因为,对象的构造分为两个阶段,第一个阶段是初始化,另一个阶段是执行构造函数的函数体。如果不使用初始化列表,就放弃了在初始化阶段给对象赋初值。

创建对象的例子,

{
  complex c1(2, 1);
  complex c2;
  complex* p = new complext(4);
}

构造函数重载

构造函数也可以重载,
但编译器要能判断出调用哪个构造函数,

一个编译器报错的例子,

complex (double r=0, double i=0)
  : re(r), im(i)
  { }

complex () : re(0), im(0) { }

以下两种调用方式都会报错,

{
  complex c1;
  complex c2();
}

因为,编译器无法确定调用哪个构造函数。

私有构造函数

例如,单例模式,

class A 
{
public:
  static A& getInstance ();
  setup () { ... }
private:
  A();
  A (const A& ths);
  ...
};

A& A::getInstance()
{
  static A a;
  return a;
}

用法,

A::getInstance().setup();

常量成员函数

double real () const { return re; }
double imag () const { return im; }

在成员函数的参数与函数体之间有一个const
表示该成员函数不会修改对象内的数据。

不加const的后果,

const complex c1(2, 1);  // c1是一个常量
cout << c1.real();       // 编译器报错
cout << c1.imag();       // 编译器报错

参数传递:pass by value / reference

complex (double r=0, double i=0)        // by value
  : re(r), im(i)
  { }

complex& operator += (const complex&);  // by reference

参数传递时,尽量传引用。
如果传入的引用不希望被改变,则要加const,例如const complex&

返回值传递:return by value / reference

double real () const { return re; }                  // by value
friend complex& __doapl (complex*, const complex&);  // by reference

返回值的传递,也尽量返回引用。
除非要返回函数内部的局部变量,此时必须返回value。

注:
不论是参数传递还是返回值传递,
传递者无需知道接收者是否以reference形式接收。

inline complex&  // 接收者可以随意使用by value或by reference
__doapl (complex* ths, const complex& r)
{
  ...
  return *ths;   // 传递者只需要返回对象即可
}

友元

friend complex& __doapl (complex*, const complex&);  // 友元
inline complex*
__doapl (complex* ths, const complex& r)
{
  // 自由取得friend的private成员
  ths->re += r.re;
  ths->im += r.im;

  return *ths;
}

注:
相同class的各个objects互为friends(友元)

class complex
{
public:
  complex (double r=0, double i=0)
    : re(r), im(i)
  { }

  int func (const complex& param)
  { return param.re + param.im; }  // 自由获取private成员

private:
  double re, im;
};
{
  complex c1(2, 1);
  complex c2;

  c2.func(c1);
}

操作符重载(成员函数方式)

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);  // 成员函数中隐含this指针
}

(1)C++中的操作符实际上是一个函数complex::operator +=
(2)编译器会将左操作数的指针,作为this隐含传入。

{
  complex c1(2, 1);
  complex c2(5);

  c2 += c1;
}
inline complex&
complex::operator += (this, const complex& r)
{ ... }

其中,this指向c1rc1的引用。

操作符重载(非成员函数方式)

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

由于doublecomplex定义的早,
而且也无法预见complex会出现,我们也无法去修改double类的定义。
所以,有关double的运算符重载,我们需要写成非成员函数形式。

inline complex
operator + (double x, const complex& y)
{ return complex (x+real(y), imag(y)); }

注:
(1)complex(...)是一种创建临时对象的写法,

{
  complex c1(2, 1);
  complex c2;

  complex();           // 临时对象
  complex(4, 5);       // 临时对象

  cout << complex(2);  // 临时对象
}

(2)以上三个运算符重载,都返回了局部对象,
因此必须返回value,不能返回referenece。

函数结束后,执行期间创造的局部对象都会回收,
如果返回reference,则将引用一块已经被释放的内存,会产生错误。

正负号运算符重载

inline complex  // 这里返回value或reference都可以
operator + (const complex& x) 
{ return x; }

inline complex  // 这里必须返回value,不能返回reference
operator - (const complex& x)
{ return complex(-real(x), -imag(x)); }

<< 运算符重载

#include 

ostream&
operator << (ostream& os, const copmlex& x)
{
  return os << '(' << real(x) << ',' << imag(x) << ')';
}

入参ostream& os不能是const
因为标准库实现中,改变了os的状态。


参考

C++面向对象高级开发 - 侯捷

你可能感兴趣的:([C++] C++面向对象高级开发:complex类)