C/C++ final exam review

C/C++ final exam review---knowledge points + exercises Introduction:

Knowledge point
1. Introduction to C++ and basic data types
1. In C++ , a function can only be used (called) after the function is declared.
2.C++ function declaration always consists of function prototype.
3. When declaring a parameter, point out its type.
4. The parameters in the function definition are called formal parameters, or formal parameters for short.
5. The value actually passed when calling the function is called the actual parameter, or actual parameter for short.
6. On most computers, short int means 2 bytes long. short can only modify int, and short int can be omitted as short.
7.long can only modify int and double. When modified as long int (can be omitted as long), it generally represents 4 bytes, and when modified as long double, it generally represents 10 bytes.
8.Unsigned and signed can only modify char and int. In general, the default char and int are signed. Real numbers float and double are always signed and cannot be modified with unsigned.
9. Use typeof (data type) to determine the byte length of a data type.
10. Rules for naming variable names:
(1) It cannot be a C++ keyword.
(2) The first character must be a letter or an underscore.
(3) Do not exceed 31 characters.
(4) There can be no spaces in between.
(5) Special symbols such as " . ; , " ' + - " cannot be included. In addition to 26 English upper and lower case letters and numbers, only underscore " _ " can be used in variable names.
(6) Variable names are not allowed Same as library function name, class name and object name in C++.
Knowledge point
1. Introduction to C++ and basic data types
1. In C++ , a function can only be used (called) after the function is declared.
2.C++ function declaration always consists of function prototype.
3. When declaring a parameter, point out its type.
4. The parameters in the function definition are called formal parameters, or formal parameters for short.
5. The value actually passed when calling the function is called the actual parameter, or actual parameter for short.
6. On most computers, short int means 2 bytes long. short can only modify int, and short int can be omitted as short.
7.long can only modify int and double. When modified as long int (can be omitted as long), it generally represents 4 bytes, and when modified as long double, it generally represents 10 bytes.
8.Unsigned and signed can only modify char and int. In general, the default char and int are signed. Real numbers float and double are always signed and cannot be modified with unsigned.
9. Use typeof (data type) to determine the byte length of a data type.
10. Rules for naming variable names:
(1) It cannot be a C++ keyword.
(2) The first character must be a letter or an underscore.
(3) Do not exceed 31 characters.
(4) There can be no spaces in between.
(5) Special symbols such as " . ; , " ' + - " cannot be included. In addition to 26 English upper and lower case letters and numbers, only underscore " _ " can be used in variable names.
(6) Variable names are not allowed Same as library function name, class name and object name in C++.
15. Conditional compilation directives are: #if, #else, #elif , #endif, #ifdef, #ifndef , #undef .
One effective use of conditional compilation is to coordinate multiple header files. Use #undef to undefine a symbol, which turns the symbol on and off as needed.
4. Arrays and pointers
1. The number of initialized array values cannot exceed the number of array elements, and the initialized array values cannot be omitted by skipping commas.
2. The number of initialization values can be less than the number of array elements. When the number of initialization values is less than the number of elements in the array, the corresponding values are initialized in order in the front, and the latter are initialized to 0.
13. Constant pointer (pointer to constant): Add const before the type of the pointer definition statement, indicating that the pointed object is a constant. Defining a pointer to a constant only restricts the indirect access operation of the pointer, but cannot specify the operation specification of the value pointed to by the pointer itself . const int pi = &a; The value of a cannot be modified by pi, but pi can be pointed to another variable.
14. Pointer constant: Add const before the pointer name of the pointer definition statement, indicating that the pointer itself is a constant. It must be initialized when defining pointer constants. char const pc = " asdf "; its content can be modified through pc, but the pointer value cannot be changed, that is, it points to another variable.
15. Constant pointer constant (pointer constant to constant): const int const cpc = &b; where cpc and cpc are both constants and cannot be operated as lvalues . *
16. Array of pointers: Each element in an array is a pointer. char * proname [] = { "Fortran", "C", "C++" };
17. There is a difference between an array of pointers and a two-dimensional array. The in-memory representation of an array of character pointers, the strings pointed to by the pointers are of irregular length. The size of each column of the two-dimensional array must be the same.
Pointer array names are pointers to pointers (ie, secondary pointers).
18. To pass an array to a function is to pass a pointer to a function. Passing an array of pointers to a function is passing a secondary pointer to the function.
Five, classes, constructors, static members and friends
1.C++ features: abstraction, encapsulation, inheritance, polymorphism.
2.:: is called the scope specifier , indicating which class a function belongs to or which class a data belongs to. :: can not be followed by the class name, indicating global data or global functions (ie non-member functions).
3. The data members of the class cannot be initialized.
4. Since the class name is part of the member function name, even if the member function of one class has the same name as the member function of another class, it cannot be considered overloaded.
5. Member functions must be called with an object.
6. The memory space occupied by a class object is determined by the sum of the space occupied by its data members. The member functions of the class do not occupy the memory space of the object.
7. The scope of a class refers to the scope of the class definition and the corresponding member function definition.
8. If a non-type name hides the type name, the type name is available by prefixing it. If a type name hides a non-type name, the normal scoping rules are sufficient.
9. C++ stipulates that a name cannot refer to two types at the same time. Non-type names (variable names, constant names, function names, object names, or enumeration members) cannot have the same name.
10.C++ stipulates that the member function with the same name as the class is the constructor, which is automatically called when the object of the class is created.
11. The constructor has no return type, and the return value is not allowed in the function body, but there can be a valueless return statement "return;".
12. In a class definition, the data members of the class may be objects of another class.
13. If a class object is a data member of another class, its constructor is automatically called for that member (object) in the constructor called by the creation of that class.
14. Destructors have no return type, no parameters, and no overloading. It is only called automatically by the system when the life of the class object ends.
15. Destructors are called in the reverse order of calling constructors.
16. A constructor with no parameters is called a default constructor.
17.C++ stipulates that each class must have a constructor, without a constructor, no object can be created.
18. If a class constructor is not provided (none is provided), C++ provides a default constructor, which is a no-argument constructor , which is responsible for creating objects without any initialization.
19. As long as a class defines a constructor (not necessarily a no-argument constructor ), C++ no longer provides a default constructor. That is, if you define a constructor with parameters for a class, and you want a parameterless constructor , you need to define it yourself.
20. Static objects are the same as static variables. Static objects at file scope are all constructed before the main function starts running. A static object in the block scope is constructed when the function that defines the static object is entered for the first time.
21. Global variables, static data, and constants are stored in the global data area, all class member functions and non-member function codes are stored in the code area, and local variables, function parameters, return data, and return addresses allocated for running functions are stored in the stack . area, and the rest of the space is used as a heap area.
22. To allocate an array of objects from the heap, only the default constructor can be called, and no other constructors can be called. An array of objects cannot be allocated if the class has no default constructor.
23. If your class needs a destructor to destruct resources, it also needs a copy constructor.
The constructor can be called directly to produce an unnamed object. An unnamed object can be passed to a function as an argument, used to copy-construct a new object, or to initialize a referenced declaration.
24. A conversion constructor is defined as a constructor with one parameter.
25. The operator new allocates heap memory. If it succeeds, it returns the space for the memory, and if it fails, it returns NULL. So every time you use operator new to dynamically allocate memory, you should test the return pointer value of new in case the allocation fails. pName = new char[ strlen ( pN ) + 1 ]; if ( pName != 0 ) strcpy ( pName , pN );
26. Class members declared as static can coexist in the class scope and are called static members.
27. Static data members are allocated and initialized outside the class declaration.
28. Public static data members can be accessed from outside the class, protected or private static data members can only be accessed from inside the class.
29. The static member function definition is the internal implementation of the class and is part of the class definition. It is defined in the same location as a general member function.
30. A static member function is not associated with any object, so it cannot perform default access to non-static members.
31. The fundamental difference between static member functions and non-static member functions: static member functions do not have this pointer, while non-static member functions have a pointer this that points to the current object.
32. Declare an ordinary function in a class and mark it with the keyword friend, it becomes a friend of the class and can access all members of the class.
33. The position of the friend declaration can be in any part of the class, either in the public area or in the protected area, with exactly the same meaning. Friend function definitions are outside the class, generally together with the class member function definitions.
34. A member function of a class can be a friend of another class.
35. An entire class can be a friend of another class, which is called a friend class. Each member function of a friend class can access protected or private data members in another class.
36. The word static of static members and the static of static storage classes are two concepts, one refers to the class, the other refers to the location of the memory space and the scope limitation. So distinguish between static objects and static members.
Six, multiple inheritance and operator overloading
1. Virtual inheritance of virtual and virtual functions have nothing to do with the virtual.
2. The construction order of multiple inheritance: First , the constructors of any virtual base class are constructed in the order in which they are inherited. Two, the constructors of any non- virtual base classes are constructed in the order in which they were inherited. Three, the constructors of any member object are called in the order in which they are declared. Fourth, the class's own constructor.
3. In the inheritance relationship, the private members of the base class are not only hidden from the application, but also hidden from the derived class. The protected members of the base class are only hidden from the application, but nothing is hidden from the derived class.
4. A private or protected derived class is not a subclass because a non-public derived class cannot do everything the base class can do.
5. Protected inheritance is similar to private inheritance, the class after inheritance is independent from the base class . To protect inherited class objects, members of the base class cannot be used in public .
6. When a class is a derived class of two or more base classes , the class names of all base classes must be listed after the derived class name and a colon, and the base classes are separated by commas.
7. The constructor of the derived class must activate all the constructors of the base class and pass the corresponding parameters to them.
8. In a non-inherited class, there is no difference between protected and private controllers. In inheritance, the private of the base class is shielded from all the outside world (including its own derived classes), and the protected controller of the base class is shielded from the application, but accessible to its derived classes.