[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 <iostream.h>  // 引入标准库头文件,使用尖括号
#include "complex.h"   // 引入自己写的头文件,使用双引号

...

(3)标准库
Standard Library

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

头文件中的防卫式声明

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

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

...

#endif               // 防卫式声明

头文件的布局

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

#include <cmath>

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<typename T>
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<double> c1(2, 1);
  complex<int> 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 <iostream.h>

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

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


参考

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354

推荐阅读更多精彩内容