C ++ Primer learning notes-basic concepts and data types

Source: Internet
Author: User

Directory

1. suffix name... 1

2. include pre-processing indicator... 1

3. Condition Indicator # ifndef # indef. 2

4. the compiler automatically defines the name of the preprocessing... 2

5. assert. 2

6. standard error... 3

7. dynamic memory... 3

8. inline function inline. 3

9. References and pointers... 4

10. Exception Handling... 5

11. Namespace. 5

12. Constant... 6

13. C ++ keywords... 6

14. Variable initialization... 7

15. \ 0 understanding... 8

16. Understanding of the const pointer... 8

17. bool type... 8

18. enum type... 8

19. array... 9

20. vector type... 9

21. pair type... 9

 

 

 

1. suffix

C ++ source program file extensions are different in different products.

In Unix systems, text. c is often used as a C ++ source program file.

The suffix of the C ++ header file varies with different products.

The standard C ++ header file has no suffix

 

2. include pre-processing indicator

Preprocessor include directive

<>: Indicates that the file is a project or standard header file.

"": The header file provided by the user will first be searched in the project

 

3. Condition Indicator # ifndef # indef

#ifndef SOME_STRING#define SOME_STRING#else//some code#endif

 

4. the compiler automatically defines the pre-processing name.

Compile the C ++ program. The Compiler automatically defines the pre-processing NAME __cplusplus.

Compile the C program. The Compiler automatically defines the Preprocessor name __stdc __

_ LINE __: records the number of lines compiled by the file.

_ FILE __: name of the FILE being compiled

 

_ TIME __: current Compilation TIME of the compiled file hh: mm: ss

_ DATE __: the compilation DATE of the currently compiled file Oct 5 2011

 

5. assert

Assert () is a general-purpose Preprocessor macro provided in the C language standard library.

 

#include <assert.h>

In C ++, # include <cassert> (this is a general conversion principle. Remove the. h prefix c)

# Include <cassert> using namespace std; // This sentence is required. Otherwise, assert is still unavailable.

 

Figure 1 system precompiled name

 

6. Standard errors

Cerr

cerr  << "This is my error" << endl;

 

7. dynamic memory

A dynamic object, that is, a new object, has no name. We operate it indirectly through a pointer.

 

8. inline Function

There are two methods to define a function as an inline function: for a global function, you only need to add the keyword inline before the function declaration or definition. For a class member function, when the function is implemented inside the class body, the function is called the inline function by default. When the function is implemented outside the class body, just add the keyword inline like a global function.

Finally, even if you declare or define a function as an inline function, it cannot be guaranteed that the function can be processed by the compiler as an inline function, it can only be said that it is recommended to be treated as an inline function. For example, when your function is very large, the compiler will basically not treat it as an inline function, this causes the program code size to expand rapidly due to frequent calls to this function. In a machine with a small memory, the hit rate of the instruction high-speed buffer device will be affected.

 

Figure 2 correct inline

SetA is automatically considered as an inline function.

If get2A is implemented in CPP, an error is returned according to the compiling method.

 

Macro definition: pre-processor Processing

Inline: Compiler Processing

 

9. References and pointers

I. The Pointer Points to a piece of memory, whose content refers to the memory address; the reference is the alias of a piece of memory.

Ii. the pointer is an entity, and the reference is only an alias;

Iii. You do not need to unreference (*) when using the pointer. the pointer must be unreferenced;

Iv. The reference can only be initialized once during definition, and will not be changed afterwards; the pointer will be variable;

V. The reference cannot be blank, and the pointer can be blank;

Vi. "sizeof reference" gets the size of the variable (object) to which it points, while "sizeof Pointer" gets the size of the pointer itself (the address of the variable or object to which it points;
Typeid (T) = typeid (T &) always true, sizeof (T) = sizeof (T &) always true,
However, when a reference is a member, the occupied space is the same as that of the pointer (the standard rule is not found ).

Vii. The pointer and reference auto-increment (++) operations have different meanings;

Figure 3 output result 4

 

Figure 4 references and pointer Definitions

 

 

Figure 5 Reference without type conversion

Figure 4 shows that if B is not defined as const, then when B changes, it will not affect the value of a, so it is no longer a reference concept. When defined as const, B is read-only, B is still equivalent to saving the address of a, or reference.

10. Exception Handling

Void fun () throw (int, string ){...}

 

11. Namespace

Figure 6 namespace

Q: How to create multiple classes with the same name in a project and put them in different namespaces?

Alias: Namespace newName = oldName;

Figure 7 namespace alias

 

12. Constants

The C ++ language can use const to define constants, or use # define to define constants. However, the former has more advantages than the latter:

(1) const constants have data types, while macro constants do not. The compiler can perform type security checks on the former. However, only character replacement is performed for the latter, no type security check is performed, and character replacement is performed for the latter.

Unexpected errors may occur (marginal effect ).

(2) Some integrated debugging tools can debug const constants, but cannot debug macro constants.

[Rule] in the C ++ program, only the const constant is used instead of the macro constant, that is, the const constant completely replaces the macro constant.

Figure 8 Constants

 

13. C ++ keywords

Figure 9 C ++ keywords

 

14. Variable Initialization

For int and double:

Global variable. The Compiler assigns the initial value 0.

For local variables, you must manually assign the initial value. (The same is true for new variables)

For char:

Both global and local values are assigned 0 (ASCII code)

Figure 10 initial variable values

 

15. \ 0

const char* p = “Hello world”;

The last digit is \ 0, so

While (* p ++) // ++ has a higher priority *

: First move the pointer back and then extract the content.

: When the string ends, there will be a \ 0, while condition is false

 

16. const pointer understanding

What follows the const keyword cannot be changed.

const char* str = “Hello Everyone!”;

Char * cannot be changed, that is, the content of the memory block pointed to by the str pointer cannot be changed, that is, "Hello Everyone !" It cannot be changed, but str itself can be changed. It can point to another memory zone.

(For example

Char * str1 = "Hello World"; str = str1; // although the syntax is correct, "Hello Everyone !" This memory block has no pointer pointing to it and becomes garbage. // Str [0] = 'a'; // error. The content of the memory block cannot be changed.

)

char* const str = “Hello Everyone!”;

It indicates that str cannot be changed. That is, the address saved by the str pointer cannot be changed. It can only point to a fixed memory block, but the content of the memory block can be changed. However, the content of the memory block pointed to by the str pointer can be changed.

(For example

Str [0] = '0'; // The correct char * str1 = "Hello World"; // str = str1; // The address saved by str cannot be changed, therefore, it cannot point to another memory block.

)

 

17. bool type

1) Why use the built-in bool type instead of the bool type simulated by typedef and enum?

L The program is readable!

L give the compiler the opportunity to optimize the code.

The built-in bool type is generally 1 byte.

Some CPUs optimize the bool type into a bit of memory

2) bool type in C Language

18. enum type

1) one element can only be defined in one enum; otherwise, an error is returned.

2) No ++ operations

WeekDay day = Tuesday;day++;//Error,No Operator ++ for day

 

3) integer values cannot be directly assigned.

WeekDay day;day = 1;//Error,Cannot Convert int to enum 

4) the return value is the index value.

WeekDay day = Monday; cout <day <endl; // The result is 0.

5) The default index value of the last element is always the index value of the previous element + 1

enum WeekDay{Monday=1,Tuesday,Wednesday=1,Thursday,Friday,Saturday=9,Sunday};

The index values of each element are: 1, 2, 2, 3, 9, 10.

 

19. Array

1) The C ++ compiler does not cross-border check on the array table.

2) values cannot be directly assigned between arrays.

int a[4] = {1,2,3,4};//int b[4] = a;//Error

 

20. vector type

We recommend that you use vector instead of array, unless you must use array in special cases.

/*************************************** ****************************************: Main. cpp * function: learning C ++ Premier note Vector * Description: No * OPERATOR: JarvisChu * Creation Time: 2011-7-3; 7-4 modification; **************************************** **************************************** * ****************/# include <iostream> # include <vector> # include <iterator> using namespace std; int main () {// array usage vector <int> ArrNum (10); // defines a vector with an initial size of 10. The default initial values are all 0 arrNum [0] = 10; arrNum [1] = 11; cout <arrNum [0] <endl; // output 10 cout <arrNum [3] <endl; // output 0 cout <arrNum [10] <endl; // output a random number of cout <"------------ gorgeous split line ------------" <endl; // STL usage: vector <int> stlNum; stlNum. push_back (10); stlNum. push_back (11); stlNum. push_back (12); cout <stlNum. at (0) <endl; cout <stlNum. at (1) <endl; // cout <stlNum. at (3) <endl; // an exception is thrown, Cout <"------------ gorgeous split line ------------" <endl; vector <int> stlNum_1 = stlNum; // initialize cout with a direct value <stlNum_1.at (0) <endl; cout <"------------ gorgeous split line ------------" <endl; vector <int>: iterator it; // use the iterator to traverse the vector for (it = stlNum. begin (); it! = StlNum. end (); it ++) {cout <* it <endl;} return 0 ;}

 

21. pair type

Associate two values of the same type or different types within a single object.

 

/*************************************** ****************************************: Main. cpp * skill: learning the pair of C ++ Premier Notes * Description: none * OPERATOR: JarvisChu * Time: create ************************************** **************************************** * *****************/# include <iostream> # include <utility> using namespace std; typedef pair <string, int> PERSON; int main () {PERSON me ("Jarvis", 21); cout <me. first <endl <me. second <endl; return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.