編編程語言的目的是幫助程序員以代碼的形式表述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++學習和使用建議:
- C++不單單是C的超集。C++雖然能夠像C那樣使用,可是在維護和性能上屬於這是未達到標準的行爲。爲了真正發揮C++優點,咱們須要採用不一樣的設計和實現風格(implementation styles)。C++提供了有用的語言特性,就沒必要再使用C的風格(如字符串賦值時使用 = 而不是strcpy(),字符串比較時使用==而不是strcmp())。
- C++中儘可能少用宏替換(macro substitution),雖然宏替換減小了工做量,可是不利於靜態類型檢查,不便於調試和維護。可使用const/constexpr/enum/enum class來定義常量(manifest constants),使用inline減小函數調用的開銷,使用templates來定義類似的函數和類型,使用namespace避免名稱衝突。
- 不要使用原生的malloc/free,使用new/delete結合智能指針unique_ptr/shared_ptr/weak_ptr等實現內存管理;使用vector等類庫替換realloc等操做。
- 避免隱式類型轉換,使用顯式的有名轉換(explicit named casts),如使用static_cast運算符。
- 使用容器和算法,不要認爲辛苦地編寫C風格的代碼就比使用STL庫更加有效率,一般吃力不討好。
- 使用constructor/destructor pairs 簡化資源管理操做,常使用RAII技術。
- 儘可能使用久經考驗的標準庫設施而不是我的開發的代碼,以加強代碼複用性和可維護性。
- 當錯誤不可以被局部代碼處理時,使用exception而不是error。
- 使用移動語義(move sematics)避免大對象的值拷貝。
- 使用智能指針代替原生指針,不要混用智能指針和原生指針。
- 使用模板維護靜態類型安全檢查和實現編譯時多態。
- 不要過分抽象(overabstract),避免使用沒必要要的類繼承。
- 使用局部變量,避免使用全局變量,最小化指針的使用場合。
- 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的目標:
- Make C++ a better language for systems programming and library building.
- Make C++ easier to teach and learn.
C++11 的 new features:
- Control of defaults: =delete and =default.
- Deducing the type of an object from its initializer, auto.
- Generalized constant expression evaluation (including literal types), constexpr.
- In-class member initializers.
- Inheriting constructors.
- Lambda expressions, a way of implicitly defining function objects at the point of their use in an expression.
- Move semantics, a way of transmitting information without copying.
- A way of stating that a function may not throw exceptions noexcept.
- A proper name for the null pointer.
- The range-for statement.
- Override controls: final and override.
- 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.
- Typed and scoped enumerations: enum class.
- Universal and uniform initialization (including arbitrary-length initializer lists and protection against narrowing).
- Variadic templates, a mechanism for passing an arbitrary number of arguments of arbitrary types to a template.
...數據結構
C++11的library facilities
- Hashed containers, such as unordered_map.
- The basic concurrency library components, such as thread, mutex, and lock.
- Launching asynchronous computation and returning results, future, promise, and async().
- The garbage collection interface.
- A regular expression library, regexp.
- A random number library.
- A pointer for simply and efficiently passing resources, unique_ptr.
- A pointer for representing shared ownership, shared_ptr.
- The tuple library.
- The general bind().
- The function type for holding callable objects.
學習C/C++的書單列表
- The C Programming Language
- The C++ Programming Language
- C++ Primer
- Computer Systems: A Programmer's Perspective
- Code Complete
- Design Patterns:Elements of reusable object-orientd- software
- Python源碼剖析
- STL源碼剖析
- 冒號課堂
- Algorithms
- Compilers:Principle,Techniques and Tools
C/C++編程入門和進階項目
- 字符串類
- 多線程編程:多線程IO環形隊列緩衝大文件英文單詞詞頻統計
- 多線程編程:消息傳遞同步操做的多線程銀行ATM模擬
- LevelDB源碼閱讀
- Python源碼剖析
- STL源碼剖析
- 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,依賴倒轉原則。高層模塊不該愛依賴於底層模塊,它們都應該依賴於抽象。抽象不依賴於細節,細節依賴於抽象。多線程