0,Preface等相关
structure of this book:
Much of the power of C++ comes from its support for programming with abstrctions. ... our focus is on how to use the features in C++ to write programs that are safe, that can be built quickly, and yet offer performance comparable to the sorts of low-level programs often written in C.
Modern C++ can be thought of as comprising three parts:
- The low-level language, largely inherited from C.
- More advanced language features that allow us to define our own data types and to organize large-scale programs and systems.
- The standard library, which uses these advanced features to provides a set of useful data structures and algorithms.
(由于之前已经不系统的学过了相当多的编程知识,这次系统性的学习应该有一些新的切入角度。考虑能否在底层思想上从Turing Machine的基本计算模型、算法的基本思想来重新的认识C++、乃至这些高级语言的一般特征,以便最终真正的理解OO等关键的思想?对此将进行尝试。)
1,Getting Started
1.1 程序基本结构 A function definition specifies four elements: the return type, the function name, a (possible empty) parameter list enclosed ion parentheses, and the function body.
(ubuntu下安装g++编译器命令: sudo apt-get install build-essential ) 1.2 语句基本样式 “endlis a special value, called a manipulator , that when written to an output stream has the effect ofwriting a new line to the output and flushing the buffer associated with that device. By flushing the buffer, we ensure that the user will see the output written to the stream”
NOTE:Programmers often insert print statements during debugging. Such statements should always flush the stream.
养成在定义变量时,对其进行初始化的习惯:“When we define a variable, we should give it an initial value unless we are certain that the initial value will be overwritten before the variable is used for any other purpose. If we cannot guarantee that the variable will be reset before being read, we should initialize it”
1.3 注释相关 大段的注释最好明确的在每一行注释前加上标记确认其属于注释!“When a comment pair does span multiple lines, it is often a good idea to indicate visually that the inner lines are part of a multi-line comment.” 1.4 基本控制结构介绍 介绍了while、for、if三种基本的流程控制结构。此外: 介绍了如何 Reading an Unknown Number of Inputs:
“When we use an istream as a condition, the effect is to test the state of the stream. If the stream is valid that is, if it is still possible to read another input then the test succeeds. An istream becomes invalid when we hit end-of-file or encounter an invalid input, such as reading a value that is not an integer. An istream that is in an invalid state will cause the condition to
fail.”
“Operating systems use different values for end-of-file. On Windows systems we enter an end-of-file by typing a control-zsimultaneously type the "ctrl" key and a "z." On UNIX systems, including Mac OS-X machines, it is usually control-d.”
#include <iostream>1.5 class类简介
int main()
{
int sum = 0, value;
// read till end-of-file, calculating a running total of all values read
while (std::cin >> value)
sum += value; // equivalent to sum = sum + value
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
class是C++的重要特点,它的目的就是更加方便程序员创建自己的数据 type从而更好地表达自己想要的的数据结构。这本书里在这第一部分就引入了class的内容,一方面是先给一个overview of whole C++,另一方面是体现了class是一种很好的封装性的元素,我们完全可以在不知道它的实现细节的基础上高效的使用它让它为我们所用!(To use a class, we need not know anything about how it is implemented. Instead, what we need to know is what operations the class provides.) To use a class we need to know three things:1.What is its name? 2.Where is it defined? 3.What operations does it support?
class的定义文件中不仅定义了它的包含元素,而且定义了所有可以使用的behavior。 NOTE:Headers for the standard library are enclosed in angle brackets (< >). Nonstandard headers are enclosed in double quotes (" " ).