c++ basic knowledge

angryTom
Release: 2019-10-26 10:48:52
Original
5983 people have browsed it

c++ basic knowledge

c Basics

C is an intermediate language developed by Bjarne Stroustrup at Bell in 1979 Design and development started in the laboratory. C further expands and improves the C language and is an object-oriented programming language. C runs on a variety of platforms, such as Windows, MAC operating systems, and various versions of UNIX. C language is a process-oriented language. On this basis, C adds object-oriented and generic programming mechanisms, so C is more suitable for the development of large and medium-sized programs. However, C does not sacrifice efficiency, and if you do not use advanced features, its efficiency is almost the same as that of the C language.

Recommended study: c manual tutorial

The following is a summary of the basic knowledge of c, I hope it will be helpful to you.

1. What are the basic data types in C?

Answer: 3 types: integer, floating point, and void.

2. There are several forms of integers. What are the differences between the various forms?

Answer: Integer types include arithmetic types of integers, characters and Boolean values, which are subdivided into 13 forms according to modifiers. The main differences are reflected in the type name, length, and representation range.

3. What are the constants in C?

Answer: 6 types of constants: integer constants, floating point constants, character constants, string constants, escaped character constants, and address constants.

4. What are the differences between constants and variables?

Answer: (1) The value of a constant cannot be changed, but the value of a variable can be changed; (2) The constant must be initialized when it is defined, and the variable does not need to be initialized when it is defined; (3) The constant cannot be found The address can only be assigned to a constant pointer, and variables can be addressed; (4) Constants have higher compilation and execution efficiency;

5. What are the classifications of operators?

Answer: (1) According to the operands: unary operators, binary operators, ternary operators; (2) According to functions: arithmetic operators, relational operators, logical operations operator, bitwise operator, assignment operator, increment and decrement operator, arrow operator, conditional operator, sizeof operator, comma operator.

6. What is the difference between the pre-operation and the post-operation of self-increment and self-decrement?

Answer: The priority of the former increment and self-decrement operation is greater than the assignment operator (=), the priority of the latter increment and self-decrement operation is smaller than the assignment operator, and the expression after the latter increment and self-decrement operation is The value of the formula will not change.

7. What is the difference between the increment and decrement of pointers and variables?

Answer: The increment and decrement of a variable changes the value of the variable, and the increment and decrement of a pointer changes the address pointed by the pointer.

8. What are lvalues ​​and rvalues?

Answer: Variables are lvalues ​​and can be on the left side of assignment statements; numerical literals are rvalues ​​and cannot be assigned.

9. What are the initialization methods for variables?

Answer: 2 types: direct initialization; copy initialization. Direct initialization is more flexible and performs more efficiently.

10. What are the declaration and definition of variables?

Answer: The main purpose of variable declaration is to indicate the type and name of the variable; the main purpose of variable definition is to allocate storage space, when they are the same.

11. What kinds of scopes are there in C?

Answer: 3 types: global scope, local scope, statement scope.

12. What are the storage types of variables?

Answer: 4 types: automatic type, static type, register type, external type.

13. What is the difference between C and C?

Answer: C language is a structured programming language. It is process-oriented and considers the implementation process; C is object-oriented and considers the entire program model.

14. What is the difference between macro definition and operator?

Answer: Macro definition is one of the preprocessing commands of C. It is a replacement operation that does not perform calculations and expression solving, and does not occupy memory or compilation time.

15. What are the characteristics of virtual functions and pure virtual functions?

Answer: A virtual function must be a non-static member function of the base class, and its access permission can be protected or public; a pure virtual function is a subset of virtual functions, and a class containing a pure virtual function is Abstract class, it cannot generate objects.

16. How to use pure virtual functions?

Answer: Pure virtual functions are used to define meaningless implementations and are used for methods in abstract classes that need to be handed over to derived classes for concrete implementation.

17. What is a pointer?

Answer: A pointer is a variable used to store a memory address. It points to the address of a single object. In addition to the void pointer type, the data type of the pointer needs to be consistent with the data type of the variable pointing to the address. .

18. What is the difference between pointers to const objects and const pointers?

Answer: The value of the const pointer itself cannot be changed, but the pointer can be used to modify the value of the object it points to; the pointer to a const variable cannot modify the value of the const variable it points to, but the pointer itself Can be reassigned.

19. What is the difference between array pointer and pointer array?

Answer: An array pointer is a pointer variable, which points to an array; a pointer array is an array containing only pointer elements, and its elements can point to different objects of the same type.

20. What is a function pointer?

Answer: A function pointer is a pointer pointing to the storage space address of a function. You can assign a value to the function pointer and call the function through the function pointer.

21. What is the difference between reference and value passing?

Answer: Pass by value passes a copy of the value, and the function's operation on the formal parameter will not affect the value of the actual parameter; pass by reference passes the memory address of the reference object, and the function's operation on the formal parameter will not affect the value of the actual parameter. The operation will affect the value of the actual parameter, and the value of the actual parameter will change as the value of the formal parameter changes.

22. What is the difference between pointers and references?

Answer: (1) References do not need to be dereferenced, pointers need to be dereferenced; (2) References are initialized once when they are defined, and are immutable thereafter, while pointers are mutable; (3) References cannot be empty. , the pointer can be empty; (4) The program allocates a memory area for the pointer variable, but the reference does not need to allocate a memory area, so the pointer increment operation is the auto-increment of the pointer variable, and the reference auto-increment operation is the auto-increment of the variable value.

23. What is the difference between object-oriented and process-oriented?

Answer: Process-oriented is a process-centered programming idea, driven by algorithms; object-oriented is an object-centered programming idea, driven by messages. The composition of a process-oriented programming language: program = algorithm data; the composition of an object-oriented programming language: program = object message.

24. What are the characteristics of object-oriented?

Answer: There are three elements of object-facing: encapsulation, inheritance, and polymorphism. All objects in object-oriented can be classified into a class.

25. What is the difference between a class and a structure?

Answer: (1) The structure is stored in the stack, and the instantiation of the class can be stored in the stack or the heap; (2) The execution efficiency of the structure is higher than that of the class ; (3) Structures do not have destructors, but classes have destructors; (4) Structures cannot be inherited, but classes can.

26. How to access static members?

Answer: Static members can be called directly through the class name without creating an instance of the class. They can also be called through an instance of the class, but the bottom layer is still called through the class name, so this is not recommended. method of calling. Static methods of a class can only access static members of the class.

27. What is polymorphism?

Answer: Polymorphism is to assign subclass objects to parent class variables. Parent class variables show different characteristics during compilation and runtime.

28. How to implement polymorphism in C?

Answer: Polymorphism includes dynamic polymorphism, static polymorphism, function polymorphism and macro polymorphism. The polymorphism we often refer to refers to dynamic polymorphism, which is based on the inheritance mechanism and virtual polymorphism. Function is implemented.

29. Conversion between derived class and base class?

Answer: A derived class can always be converted to a reference type of a base class; conversion from a base class to a derived class requires the use of cast when it is safe to do so.

30. What is a virtual member and what is its role?

Answer: The function of virtual function is to realize dynamic binding. After the program finds the keyword virtual before the name of the virtual function, it will automatically treat it as dynamic binding, that is, dynamic selection when the program is running. Appropriate member functions.

31. Overview of C covering and hiding?

Answer: (1) Overwriting means that there is a function with the same function name and parameters in the subclass and the parent class, and the function of the parent class is a virtual function; (2) Hiding It means that there are functions with the same function name and different parameters in the subclass and the parent class. At this time, regardless of whether the parent class function is a virtual function, the parent class function will be hidden, or there are functions with the same function name and parameters. , at this time, the parent class function will be hidden only when the parent class function is not a virtual function.

32. What are deep copy and shallow copy?

Answer: If a class has resources, when the resources of this class are copied, it is called a deep copy; if the object has resources but the resources are not copied during the copy process, it is a shallow copy.

33. What is the copy constructor and when is it called?

Answer: The copy constructor is called by the compiler to complete the construction of other objects based on the same class. and initialization. There are three situations where the copy constructor will be used: (1) An object is passed into the function body by value; (2) An object is returned from the function by value; (3) An object needs to be passed through another object. initialization.

34. What is the type conversion constructor?

Answer: The constructor of a class has only one parameter. The parameter type is not the type of the class but other types. This constructor is called a type conversion constructor, which can be used to process The same data value in different domains.

35. Does C support functions with an uncertain number of parameters?

Answer: C can support functions with uncertain parameters through the hidden parameter mechanism.

36. What is an inline function?

Answer: Member functions declared or defined inside a class declaration are called inline functions. Loop statements and switch statements are not allowed within inline functions.

37. What is the difference between reference formal parameters and non-reference formal parameters?

Answer: The reference parameter is to pass the address of the parameter variable, and the value of the actual parameter can be modified by calling the function on the formal parameter.

38. What are the problems with using reference parameters?

Answer: When calling a reference parameter of non-const type, the actual parameter must not be of const type, and the two types should be consistent; when calling a formal parameter function with a const reference, if the actual parameter is not a When the variables or types do not match, the function will create an unnamed temporary variable to store the value of the actual parameter, and use this formal parameter as a reference to the temporary variable.

39. What is the difference between pointer parameters and reference parameters?

Answer: Pointer parameter means that the parameter of the function is a pointer. It will not affect the value of the actual parameter through function call like the reference parameter, but it will modify the object of the actual parameter after the call. It is recommended to use pointer parameters as little as possible in the program, as this will reduce the readability of the program.

40. What is a static function? How to use static functions?

Answer: A static function is a function modified with the static modifier. A static function does not have this pointer and can only access static variables. If the result of a function call in a class will not access or modify any object data members, it is better to declare such members as static member functions.

41. Function overloading and scope?

Answer: Function overloading refers to multiple functions with the same name but different parameter lists in the same scope.

42. How to implement type conversion of actual parameters when function overloading?

Answer: When matching function overloading, the matching is first achieved through standard conversion. If No, then use class type conversion to achieve matching.

43. What is a function template?

Answer: Function template technology refers to the use of template technology to define non-member functions of parameterized types, which enables programs to call the same function using different parameter types.

44. What is a class template?

Answer: Class templates are classes that use template technology to describe general data types that can manage other data types. Class template technology is often used to create container classes that contain other types (queues, linked lists, stacks, etc.).

45. What is generic programming?

Answer: Generic programming is writing code in a way that is independent of the implementation of a specific class, and providing a common implementation for different types.

46. How to implement generic programming in C?

Answer: The implementation of generic programming in C is achieved using template technology in C, mainly by designing function templates and class templates.

The above is the detailed content of c++ basic knowledge. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!