[2017.02.04] C++學習記錄(1)

編編程語言的目的是幫助程序員以代碼的形式表述ideas。編程語言一方面爲程序員提供一組關於能夠作什麼的抽象,另外一方面爲程序員提供能夠被機器執行的輪子。C++編程語言,支持4種編程範式:過程式(Procedural Programming,主要集中在過程和合適的數據結構)、數據抽象(Data abstraction,主要表現爲設計抽象接口隱藏具體實現細節)、面向對象編程(Object-oriented programming,主要集中在設計、實現和使用類繼承結構,提供運行時多態性)和泛型編程(Generic programming,主要集中在設計、實現和使用泛型算法,提供編譯時多態性)。程序員

C++普遍應用於教學和科學研究,雖然不是「最好」的語言,可是它是一門能夠伴隨着成長的語言,有以下特色:算法

Sufficiently clean for successfully teaching basic design and programming concepts
Sufficiently comprehensive to be a vehicle for teaching advanced concepts and techniques
Sufficiently realistic, efficient, and flexible for demanding projects
Sufficiently commercial to be a vehicle for putting what is learned into nonacademic use
Sufficiently available for organizations and collaborations relying on diverse development and execution environmentsexpress

在學習C++的時候,一是要關注基本概念(fundamental concepts),如類型安全(type safety)、資源管理(resource management)和不變量(invariants)等;二是要學習一些編程技術,如資源管理的實現、算法中的迭代器的使用、設計模式(可複用面向對象軟件的基礎)等。切記沉浸在語言的細節中。學習語言的目的是爲了更加高效設計和實現新的系統並維護舊的工程。所以評判鑑定不一樣的語言和設計技術,比理解全部的細節更加劇要。細節的熟練掌握須要時間的沉澱和實踐打磨。編程

C++語言有幾個不一樣獨立版本的實現,如Microsoft自家在VisualStudio中的實現,GNU的MinGW實現等。每種版本都有本身對C++標準的解釋和一些偏見,全部學習的時候,多多綜合參考各自的manuals、online resources等,以期達到更加全面的理解。設計模式

學好一門編程語言,就如同窗會寫做同樣。須要咱們知道本身想要說什麼(Know what you want to say)而且多加練習和模仿名家做品(Practice and imitate good writing)。大道至簡卻也知易行難promise

一些C++學習和使用建議:

  1. C++不單單是C的超集。C++雖然能夠像C那樣使用,可是在維護和性能上屬於這是未達到標準的行爲。爲了真正發揮C++優點,咱們須要採用不一樣的設計和實現風格(implementation styles)。C++提供了有用的語言特性,就沒必要再使用C的風格(如字符串賦值時使用 = 而不是strcpy(),字符串比較時使用==而不是strcmp())。
  2. C++中儘可能少用宏替換(macro substitution),雖然宏替換減小了工做量,可是不利於靜態類型檢查,不便於調試和維護。可使用const/constexpr/enum/enum class來定義常量(manifest constants),使用inline減小函數調用的開銷,使用templates來定義類似的函數和類型,使用namespace避免名稱衝突。
  3. 不要使用原生的malloc/free,使用new/delete結合智能指針unique_ptr/shared_ptr/weak_ptr等實現內存管理;使用vector等類庫替換realloc等操做。
  4. 避免隱式類型轉換,使用顯式的有名轉換(explicit named casts),如使用static_cast運算符。
  5. 使用容器和算法,不要認爲辛苦地編寫C風格的代碼就比使用STL庫更加有效率,一般吃力不討好。
  6. 使用constructor/destructor pairs 簡化資源管理操做,常使用RAII技術。
  7. 儘可能使用久經考驗的標準庫設施而不是我的開發的代碼,以加強代碼複用性和可維護性。
  8. 當錯誤不可以被局部代碼處理時,使用exception而不是error。
  9. 使用移動語義(move sematics)避免大對象的值拷貝。
  10. 使用智能指針代替原生指針,不要混用智能指針和原生指針。
  11. 使用模板維護靜態類型安全檢查和實現編譯時多態。
  12. 不要過分抽象(overabstract),避免使用沒必要要的類繼承。
  13. 使用局部變量,避免使用全局變量,最小化指針的使用場合。
  14. Keep simple things simple (without making complex things impossible).

C++的history/timeline/language features/library facilities。

通過幾十年發展,C++的語言標準也在不停的進化,咱們想要作一個合格的碼農,也須要跟上語言標準的步伐。學習新技術不是爲了學習而學習,而是爲了使用新技術解決了過往技術的痛點。現在C++11已經提出了五六年了,新版本C++14/17也近在眼前,若是咱們還僅僅停留在C++98而對新技術無動於衷,未免有點故步自封了。學習和使用新版本中的語法特性,使得設計和編程時更加現代化。安全

C++11的目標:

  1. Make C++ a better language for systems programming and library building.
  2. Make C++ easier to teach and learn.

C++11 的 new features:

  1. Control of defaults: =delete and =default.
  2. Deducing the type of an object from its initializer, auto.
  3. Generalized constant expression evaluation (including literal types), constexpr.
  4. In-class member initializers.
  5. Inheriting constructors.
  6. Lambda expressions, a way of implicitly defining function objects at the point of their use in an expression.
  7. Move semantics, a way of transmitting information without copying.
  8. A way of stating that a function may not throw exceptions noexcept.
  9. A proper name for the null pointer.
  10. The range-for statement.
  11. Override controls: final and override.
  12. Type aliases, a mechanism for providing an alias for a type or a template. In particular, a way of defining a template by binding some arguments of another template.
  13. Typed and scoped enumerations: enum class.
  14. Universal and uniform initialization (including arbitrary-length initializer lists and protection against narrowing).
  15. Variadic templates, a mechanism for passing an arbitrary number of arguments of arbitrary types to a template.

...數據結構

C++11的library facilities

  1. Hashed containers, such as unordered_map.
  2. The basic concurrency library components, such as thread, mutex, and lock.
  3. Launching asynchronous computation and returning results, future, promise, and async().
  4. The garbage collection interface.
  5. A regular expression library, regexp.
  6. A random number library.
  7. A pointer for simply and efficiently passing resources, unique_ptr.
  8. A pointer for representing shared ownership, shared_ptr.
  9. The tuple library.
  10. The general bind().
  11. The function type for holding callable objects.

學習C/C++的書單列表

  1. The C Programming Language
  2. The C++ Programming Language
  3. C++ Primer
  4. Computer Systems: A Programmer's Perspective
  5. Code Complete
  6. Design Patterns:Elements of reusable object-orientd- software
  7. Python源碼剖析
  8. STL源碼剖析
  9. 冒號課堂
  10. Algorithms
  11. Compilers:Principle,Techniques and Tools

C/C++編程入門和進階項目

  1. 字符串類
  2. 多線程編程:多線程IO環形隊列緩衝大文件英文單詞詞頻統計
  3. 多線程編程:消息傳遞同步操做的多線程銀行ATM模擬
  4. LevelDB源碼閱讀
  5. Python源碼剖析
  6. STL源碼剖析
  7. Redis源碼閱讀

面向對象編程和設計(Object oriented programming and design)的SOLID原則:

S:SRP,single responsibility principle,單一職責原則。一個類應該只有一個職責。
O:OCP,Open/Closed principle,開閉原則。一個軟件實體(類、模塊、函數等)應該對拓展開放,對修改關閉。這樣能夠不修改實體的源碼而直接拓展它的行爲。
L:LSP,Liskov substitution principle,里氏替換原則。 基類能夠被子類替換而不產生類型錯誤。
I:ISP,Interface segregation principle,接口隔離原則。 多個特例化的接口優於一個通用的接口。對軟件進行解耦合,便於後期重構和維護。
D:DIP,Dependency inversion principle,依賴倒轉原則。高層模塊不該愛依賴於底層模塊,它們都應該依賴於抽象。抽象不依賴於細節,細節依賴於抽象。多線程

相關文章
相關標籤/搜索