C++ Primer(英语第5版) 阅读日记 - 20200411

Chapter 1: Getting Start

接上篇。今天是坚持读Primer的第二天,希望看到这篇博文的你也能一起努力!生活再苦再累也要学习是吧。日记在于先把C++熟练度修上去,系统学一下。主要记录一些问题和知识点吧,尽量精简。中英文混合为保持术语一致和方便。

Note 1.4 Using

  • “::” is called the scope operator
  • namespace is to avoid inadvertent collisions between the names we define
  • Example: for std::endl, std is the namespace while endl is name in that namespace.

Note 1.5 Comments

注释就省略了啊,都0202年了,C++不会用注释的人应该不存在。

Note 1.6 Flow of control

这一张都是最基础的。不会这些还说会代码?可以飞快掠过。

while loop: 最基本循环不说了

while (condition)
    statement

++val; <=> val = val + 1;

for statement

int sum = 0;
for (int i = -100; i <= 100; ++i)
    sum += i;

Example: read until end of file

#include 

int main() {
    int value = 0;
    int sum = 0;
    while (std:: cin >> value) {
        sum += value;
    }
    std:: cout << sum;
    return 0;
}

记住 std:: cin >> value 返回的是 std:: cin,他是istream。所以,这里的condition是std:: cin, which becomes invalid when we hit end-of-file or encounters an invalid input, such as reading a value that is not an integer.

常用:windows EOF 为 ctrl+z(貌似在powershell不管用),unix EOF 为 ctrl+d(这个没问题)

if statement: 兄弟,if语句都不会,可以回家种田了。

Note 1.6: Three types of error: syntex, type, declaration

这些error平常抬头不见低头见,这里做系统梳理。因为真让你说这些error是因为哪些情况出现的,很多人可能就吃不准了。

  • syntex error: grammatical error in C++ language. (e.g: miss of operator, etc.)
  • type error: each item of data in C++ has an asscoiated type. pass a type to a function that does not expects that type may trigger type error.
  • declaration error: 

Note 1.7: Classes

Class 大家都用,然而说清楚很难。

  • C++ defines our own data structures by defining a class
  • A class defines a type along with a collection of operations that are related to that type.

头文件是什么?从应用面入手,我们能讲清楚头文件:

  • 使用库,我们include头文件:To use a library facility, we must include the associated header.
  • 所以,要定义我们自己的应用,我们要定义头文件:To define our own applications, we have to use headers.
  • Header file names are derived from the name of a class defined in that header by convention.
  • We usually have a suffix of .h or .hpp, .H or .hxx.

Each class defines a type. The type name is the same as the name of the class.

Member function(成员函数)

  • Use the dot operator (".") to call a member function : left oprend (an object of class type) and the right-hand operand must name a member of that type. 

Call operator ("()"): 看看用括号调用方程专业术语咋说吧

  • We use dot operator to access a member function
  • We call a function by call operator.
  • The call operator is a pair of parenetheses that enclose a (possibly empty) list of arguments.

今日就此搁笔,明日再战。具体class的实现应该不在这一章。

你可能感兴趣的:(C++)