编编程语言的目的是帮助程序员以代码的形式表述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,依赖倒转原则。高层模块不该爱依赖于底层模块,它们都应该依赖于抽象。抽象不依赖于细节,细节依赖于抽象。多线程