1牵触、概述
課程名稱:C++面向?qū)ο蟪绦蛟O(shè)計
1.1 課程介紹
通過這門課程你能獲得什么?
培養(yǎng)正規(guī)、大氣的編程習(xí)慣
這門課程無時無刻不在提醒你好的c++編程習(xí)慣是什么塘淑,讓人第一眼看到你的代碼就覺得你很nice。課程中會有c++編程作業(yè)蚂斤,這些作業(yè)會幫助你把這些細(xì)碎的知識點串接起來存捺。
以良好的方式編寫c++ class
按照class是否包含指針而將class分為兩大類,課程中會重點強調(diào)這兩種class的區(qū)別以及處理方法曙蒸。
作為一門c++面向?qū)ο蟪绦蛟O(shè)計課程捌治,c++ class無疑算是重點,在這里侯捷老師開門見山纽窟,在第一講中就給出了c++ class的一種經(jīng)典分法肖油,之后的大部分課程都會緊緊圍繞這個分法來講解知識。這種風(fēng)格讓我有一種提綱挈領(lǐng)地感覺臂港,更對c++ class有了一個更為清晰的認(rèn)識森枪,非常受用。
掌握class之間的3種關(guān)系-繼承趋艘、復(fù)合疲恢、委托
當(dāng)class之間具備某種關(guān)系的時候就是面向?qū)ο蟮模粗畣我籧lass就是基于對象的瓷胧,學(xué)習(xí)面向?qū)ο笠獜幕趯ο箝_始显拳,先要能寫出正確的單一class,這也是這門課程的學(xué)習(xí)順序搓萧。
1.2 推薦書籍
入門書籍:C++ Primer, The C++ Programming Language
進階書籍:Effective C++, The C++ Standard Library, STL源碼剖析
2杂数、上課內(nèi)容梳理
2.1 Header File規(guī)范
2.1.1 要具備防衛(wèi)式聲明
#ifndef __COMPLEX__
#define __COMPLEX__
.................
.................
#endif
2.1.2 基本的結(jié)構(gòu)
基本上是由一下三部分構(gòu)成的:
Forward declarations:前置聲明
class ostream;
class complex;
Class declarations:類-聲明
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&);
double real () const { return re; }
double imag () const { return im; }
private:
double re, im;
friend complex& __doapl (complex *, const complex&);
friend complex& __doami (complex *, const complex&);
friend complex& __doaml (complex *, const complex&);
};
Class definition:類-定義
inline complex&
complex::operator *= (const complex& r)
{
return __doaml (this, r);
}
2.2 內(nèi)聯(lián)函數(shù)
函數(shù)如果是在class body內(nèi)定義完成宛畦,便自動成為inline的候選人
2.3 訪問級別
public vs private:一般來說數(shù)據(jù)都是private屬性的,而函數(shù)是public屬性揍移,但是也并不絕對次和。
{
complex c1(2,1);
cout << c1.re; ?//不被允許,應(yīng)該使用cout << c1.real();
cout << c1.im; //不被允許那伐,應(yīng)該使用cout << c1.img();
}
2.4 構(gòu)造函數(shù)
構(gòu)造函數(shù)的完整結(jié)構(gòu)包括以下三個:
1踏施、默認(rèn)實參;2罕邀、初始列畅形;3、賦值诉探。
構(gòu)造函數(shù)會用到overloading的一些功能
在C++的內(nèi)部實現(xiàn)層面日熬,編譯器會對overloading的構(gòu)造函數(shù)賦予不同的實際名稱
例如:
?real@Complex@@QBENXZ
?real@Complex@@QAENABN@Z
2.5 Const的使用
const 保證得到的數(shù)據(jù)不被更改
double real() const {return re;}
double imag() const {return im;}
2.6 Return by value VS Return by reference (to const)
我們在編程的時候,
1肾胯、應(yīng)該首先考慮return by reference的用法竖席。
原因是:return by value有可能數(shù)據(jù)量非常大,而return by reference不會出現(xiàn)這種情況敬肚,就是4個4節(jié)毕荐。
2、要考慮reference 的對象const的屬性
3帘皿、何時可以使用return by reference东跪,何時不可以
第一參數(shù)將被改變,但是第二參數(shù)不改變的情況可以使用:
“+=”的這種情況
但是像temp object是不能使用return by reference鹰溜,因為他們返回的必定是local object虽填。
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));
}
4、傳遞著無需知道接受者是以reference接收的
inline complex&
__doapl (complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}
2.7 友元
1曹动、友元的特性是可以自由取得friend的private成員
inline complex&
__doapl (complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}
2斋日、相同class的各個objects互為友元
2.8 Operator Overloading
在考慮這些函數(shù)是否應(yīng)該成為成員函數(shù),還是非成員函數(shù)的時候墓陈,要考慮函數(shù)的參數(shù)是否一定是成員類恶守。