SUMMARY OF CHAPTER 1-2

来源:互联网 发布:单片机p0 p1 p2 p3 编辑:程序博客网 时间:2024/06/16 13:39

CHAPTER 1

SECTION 1.2

        1. C++ does not directly define any statements to do input or output(IO). Instead, IO is provided by thestandard library.

        2. the :: operator means the scope operator.

        3. Reading an Unknown Number of Inputs: while(cin>>value).

        4. Headers for the standard library are enclosed in angle brackets(< >). Nonstandard headers are enclosed in double quotes(" ").


CHAPTER 2

SECTION 2.1-2.4

1. we usually refer to a chunk of 8 bits as a "byte" and 32 bits or 4 bytes, as a "word".

2. Literals exist only for the built-in types. There are no literals of class types.

3. For compatibility with C, string literals in C++ have one character in addition to those typed in by the programmer. Every string literal ends with a null character added by the compiler.

4. Multi-Line Literals: \ .

5. In C++, whether an operation is legal or not is checked at compile time.

6. an object is a region of memory that has a type. More specifically, evaluating an expression that is an lvalue yields an object.

7. identifier must begin with either a letter or an underscore. Upper-and lowercase are distinct: Identifiers in C++ are case-sensitive.

8. Identifiers cannot contain two consecutive underscores, nor can an identifier begin with an underscore followed immediately by an upper-case letter. Certain identifiers---those that are defined outside a function---may not begin with an underscore.

9. initialization is not assignment. Initialization happens when a variable is created and gives that variable its initial value. Assignment involves obliterating an object's current value and replacing that value with a new one.

10. The member functions that define how initialization works are known as constructors.

11. Whether a variable of built-in type is automatically initialized depends on where it is defined. Variables defined outside any function body are initialized to zero. Variables of built-in type defined inside the body of a function are uninitialized.

12. A definitionof a variable allocates storage for the variable and may also specify an initial value for the variable. A declarationmakes known the type and name of the variable to the program. A definition is also a declaration.

13. An extern declaration is not a definition and does not allocate storage.

14. Because we cannot subsequently change the value of an object declared to be const, we must initialize it when it is defined.

15. const variable declared at global scope are local to the file in which the object is defined. We can make a const object accessible throughout the program by specifying that it is extern:

16. Nonconst variables are extern by default. To make a const variable accessible to other files we must explicitly specify that it is extern.

SECTION 2.5-2.9

1. A reference is a compound type that is defined by preceding a variable name by the & symbol. A compound type is a type that is defined in terms of another type.

int ival = 1024;

int &refVal = ival;//refVal refers to ival

2. We cannot define a reference to a reference type, but can make a reference to any other data type.

3. When a reference is initialized, it remains bound to that object as long as the reference exits. There is no way to rebind a reference to a different object.

4. The important concept to understand is that a reference isjust another name for an object.

5. initialization is the only way to say to which object a reference refers.

6. A const reference is a reference that may refer to a const object. And it must be a const reference.

const int ival = 1024;

const int &refVal = ival;//both reference and object are const

7. A referencemust be initialized using an object of the same type as the reference. While  a const reference can be initialized to an object of a different type or to an rvalue, such as a literal constant.

int &refVal3 = 100;//error: initializer must be an object

const int &r = 42;//right

const int &r2 = r + i;//right

8. Allowing only const references to be bound to values requiring temporaries avoids the problem entirely because a const reference is read-only.

9. A nonconst reference may be attached only to an object of the same type as the reference itself. A const reference may be bound to an object of a different but related type or to an rvalue.

10. A typedeflets us define a synonym for a type.

11. Enumerators are const values.

12. The value used to initialize an enumerator must be aconstant expression.

13. A constant expression is an expression of integral type that the compiler can evaluate at compile time.

14. It is not possible to change the value of an enumerator. As a consequence an enumerator is itself a constant expression and so can be used where a constant expression is required.

15. (CLASS TYPE)The implementation also includes any functions needed to define the class but that are not intended for general use.

16. Thestructkeyword is inherited from C.

17. The only difference between a class defined with theclasskeyword or thestructkeyword is the default access level: By default, members in astructare public; those in aclassprivate.

18. Headers are for declarations, not definitions.

19. There are three exceptions to the rule that headers should not contain definitions:classes,const objects whose value is known at compile time, and inline functions are all defined in headers.

20. const objects are local to the file in which they are defined, it is legal to put their definition in a header file.

21. When we define a const in a header file, every source file that includes that header has its own const variable with the same name and value.

22. most compilers will replace any use of such const variables by their corresponding constant expression at compile time. So, in practice, there won't be any storage used to hold const variables that are initialized by constant expressions.

When a const is initialized by a value that is not a constant expression, then it should not be defined in header file. Instead, as with any other variable, the const should be defined and initialized in a source file. An extern declaration for that const should be made in the header, enabling multiple files to share that variable.

23. C++ inherits a fairly elaborate preprocessor from C.

24. The preprocessor replaces each #include by the contents of the specified header.

25. We can use these facilities to guard against including a header more than once:

#ifndef SALESITEM_H

#define SALESITEM_H

// Definition of Sales_item class and related functions goes here

#endif

26. Headers should have guards, even if they aren't included by another header. Header guards are trivial to write and can avoid mysterious compiler errors if the header subsequently is included more than once.

27. C++ is a statically typed language: Variables and functions must be declared before they are used. A variable can be declared many times but defined only once.