读书笔记 - C++ Primer 第五版 Chapter 1

CHAPTER 1 - GETTING STARTED

1. Basic elements of C++: types, variables, expressions, statements and functions.

2. Write, compile and execute a program.

3. The operating systems runs a C++ program by calling main function.

int main ( )

{

  return 0;

}

This main does nothing but return a value to OS.

4. A function definition has four elements: a return type, a function name, a parameter list and a function body.

A function body is a block of statements.

大括号:open and close curly brace.

return statement terminates a function, and send a value back to the function's caller.

5. Semicolons mark the end of most statements in C++.

6. The value return from main is a status indicator.

0 indicates success.

nonzero means errors.

7. Key concept: Types

A type defines both the contents of a data element and the operations that are possible on those data.

Data are stored in variables and every variables has a type.

V is a T, or V has type T.

8. How you compile a program depends on your OS and compiler.

Many PC-based compilers are fun from an integrated development enviroment(IDE) that bundles the compiler with build and analysis tools.

9. Different compilers use different suffix conventions for C++ source files.

The most common include .cc, .cxx, .cpp, .cp, and .C.

10. The value returned from main is accessed in a system-dependent manner.

Linux:

$ echo $?

Windows:

$echo %ERRORLEVEL%

11. The most common compiler: GNU compiler and the Microsoft Visual Studio Compilers.

$ g++ -o prog prog.cpp

C:\users\me> cl /EHsc prog.cpp

options in GNU:

--std=c++11

    turn on C++ 11 support:  

-Wall

    Generate all warnings.

Options in Windows:

/W4

    Generate all warnings.

12. The C++ language does not define any statements to do input or output(IO). Instead, C++ includes an extensive standard library that provides IO and many other facilities.

Standard Input and Output Objects. The library defines four IO objects.

An object of type istream named cin. This object is also referred to as the standard input.

For output, we use an ostream object named cout. This object is also known as the standard output.

The library also defines two other ostream objects, named cerr and clog.

Ordinarily, the system associates each of these objects with the windows in which the program is executed.

13. The first line of our program: 

#include <iostream>

tells the compiler that we want to use the iostream library.

14. In general, include directives must appear outside any function.

Typically, we put all the #include directives for a program at the beginning of the source file.

15. In C++ an expression yields a result and is composed of one or more operands and usually an operator.

16. The output operator (<<) takes two operands: The left-hand operand must be an osteam object; the right-hand operand is a value to print.

The result of the output operator is its left hand operand.

17. string literal.

endl is a manipulator, ending the current line and flushing the buffer associated with that device.

18. All the names defined by the standard library are in the std namespace.

Namespaces allow us to avoid inadvertent collisions between the names we defined and uses of those same names inside a library.

19. scope operator :: 

(std::cin >> v1) >> v2;

We can combine a sequence of input requests into a single statement.

20. The result of evaluating the arithmetic expression v1 + v2.

21. Multiplication operator *, the product. 

22. Before our programs get much more complicated, we should see how C++ handles comments.

Tey are typically used to summarize an algorithm, identify the purpose of a variable, or clarify an otherwise obscure segment of code.

The compiler ignores the comments, so they have no effect on the program's behavior or performance.

23. There are two kinds of comments in C++: single-line and paired.

// double slash, end with a newline.

Two delimeters /* and */, inherited from C.

A tab, space, or new line is permitted.

Multiline comment, begin each line in the comment with an asterisk.

Programs typically contain a mixture of both comment forms.

Comment pairs generally are used for multiline explanations, whereas double-slash comments tend to be used for half-line and single-line remarks.

Comment pairs Do Not Nest.

We often need to comment out a block of code during debugging.

The best way to comment a block of code is to insert single-line coments at the beginning of each line in the section we want to ignore.

24. Flow of Control.

Statements normally execute sequentially: The first statement in a block is executed first, followed by the second, and so on.

Instead, programming languages provide various flow-of-control statements that allow for more complicatd execution paths.

25. while statement.

A condition is an expression that yields a result that is either true or false.

The while continues, alternately testing the condition and executing statement until the condition is false.

26. less-than-or-equal operator, the <= operator.

27. A block is a sequence of zero or more statements enclosed by curly braces.

A block is a statement and my be used wherever a statement is required.

28. Compound assignment operator, the += operator.

29. Prefix increment operator, the ++ operator.

30. A loop, test condition and execute the body.

31. decrement operator --.

32. The for statement.

This pattern - using a variable in a condition and incrementing that variable in the body - happens so often that the language defines a second statement, the for statement, that abbreviates code that follows this pattern:

while (val <= 10) {

  sum += val;

  ++val;

}

for(int val=1; val<=10;++val) 

    sum += val;

Each for statement has two parts, a header and a body.

The header controls how often the body is executed. The header itself consists of thress parts: an init-statement, a condition and an expression.

The variable val exists only inside the for, the init-statement is executed only once, on entry to the for.

The condition is tested each time through the loop.

The expression is executed after the for body.

33. Familiarize yourself with the messages that compiler generates.

34. A logical extension of this program would be to ask the user to input a set of numbers to sum.

In this case, we won't known how many numbers to add. 

Instead, we'll keep reading numbers until there are no more numbers to read:

while (std::cin>>value)

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 the stream hasn't encountered an error, then the test succeeds.

An istream becomes invalid when we hit end-of-file or encounters an invalid input, such as reading a value that is not an integer.

Entering an end of file from the keyboard:

Windows: Ctrl - z

Unix/Mac OS X : Ctrl - d

35. Part of the compiler's job is to look for errors in the program text.

Syntax errors, type errors.

36. Each item of data in C++ has an associated type.

37. Declaration erros: Every name used in a C++ program must be declared before it is used.

38. It is a good practice   to correct errors in the sequence they are reported.

This cycle is known as edit-compile-debug.

39. The if statement.

Supports conditional execution.

if(std::cin >> currVal)

40. The equality operator, the == operator.

41. C++ use = for assignment and -- for equality.

42. Key concept: Indentation and formatting of C++ programs.

C++ is free formal.

Blank and comments are ignored.

For readability and comprehension.

Endless debates occur as to the right way to format C or C++ programs.

Our belief is that there is no single correct style but that there is value in consistency.

43. Introducing Classes.

Define a data structure to represent our transaction data.

In C++ we define our own data structures by defining a class.

A class defines a type along with a colleciton of operations that are related to that type.

In fact, a primary focus of the design of C++ is to make it possible to define class types that behave as naturally as the built-in types.

44. Header files that we write usually have a suffix of .h, .H, .hpp or .hxx.

The standard library headers typically have no suffix at all.

Conventionally, header file ames are derived from the name of a class defined in that header.

We use headers to access classes defined for our own applications, lie to use a library facility.

45. The purpose of the class.

How these data are stored or computed is not our concern.

To use a class, we need not care about how it is implemented.

Instead, what we need to know is what operations objects of that type can perform.

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

As with the built-in type, we can define a variable of a class type.

The vriable is an object of type classXX.

47. Key concept: Class define behavior.

The author of the class defines all the actions that can be performed by objects of this class.

That is, the class defines what happens wen a XX object is created and what happens when the assignment, addition, or the input and output operators are applied to XX class.

48. Headers from the standard library are enclosed in angle brackets (< >).

Those that are not part of the library are enclosed in double quotes (" ").

49. Using file redirection

Most operating systems support file redirection which let us associate a named file with the standard input and the standard output.

50. A first look at Member Functions.

A member function is a function that is defined as part of a class.

Member functions are sometimes refferred to as methods.

51. Ordinarily, we call a member function on behalf of an object.

The dot operator, applies only to objects of class type.

The left-hand operand must be an object of class type, and the right-hand operand must name a member of that type.

The Result of the dot operator is the member named by the right-hand operand.

When we use the dot operator to access a member function, we usually do so to call that function.

We call a function using the call operator( the ( ) operator).

The call operator is a pair of parentheses that enclose a (possibley empty) list of arguments.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值