《C++编程语言》学习笔记1

来源:互联网 发布:java 内存流 生成pdf 编辑:程序博客网 时间:2024/04/27 23:49

    看的是Bjarne Stroustrup(C++之父)的The C++ Programming Language, 第三版,Special Edition。感觉每一章末尾的Advice或Suggestion都写的特别实用。Stroustrup不但介绍创造C++的思想,还顺便说了很多和编程有关的感悟.......重新系统的学一遍C++,我就用这本书了......坚持......

    和大多C++书籍一样,首先从整体上介绍一下C++,给大家个直观印象,然后是C++的语法。第三部分应该是重点:抽象机制,最后是标准库和C++设计。

   

    设计C++程序的关键,记住,这是指导思想:

Try thinking of a program as a set of interacting concepts represented as classes and objects, instead of as a bunch of data structures with functions twiddling their bits.

1.1 Suggestions for C Programmers
    [1] Macros are almost never necessary in C++. Use const, enum, inline(避免函数调用的额外开销), templates, namespace(避免重名).
    [2] Don`t declare a variable before you need it. Declaration can occur anywhere a statement can, in for-statement or in conditions.(声明可以出现在for语句或判断语句中)
    [3] Don`t use malloc(), realloc(), try vector(可以动态分配存储空间).
    [4] Try to avoid void*, pointer arithmetic, casts.(强制类型转换)
    [5] minimize the use of arrys and c-style strings, but use vector classes .and try not to build yourself what has already been provided by the standard library.

1.2 Thinking about programming in C++
    (1) The key to writing good programs is to design classes
    (2) One of the most powerful intellectual tools for managing complexity is hierarchical ordering
    (3) Virtual functions can often be used to define operations for the most general version of a concept(a base class)
    (4) If two classes have a common interface, make that interface an abstract class.

  还有一些重要概念
    (1)数据抽象:在结构内部放入函数,称之为抽象数据类型
    (2)继承:一种机制
    (3)虚函数:vitual 声明的 
    (4)纯虚函数:vitual f() = 0声明的, 就是在子类中必须定义的函数

    (5)容器:容器类的简称;用模板机制定义的类,就是容器,它可以适应很多不同类型的的变量
    (6)模板:一种编译机制,与宏类似。用这种机制,一个算法可适应于所有类型的变量

  设计C++程序,要那么来思考

   

    优秀的编程语言,首先要能够完成功能,其次要做到表达最简,最后是可以用多种方法实现同一个功能,即“what you don`t know won`t hurt you”

原创粉丝点击