C/c ++ common interview questions, interview questions

Source: Internet
Author: User
Tags least privilege

C/c ++ common interview questions, interview questions

1. What is the role of static in C?

(1) Hide. When we compile multiple files at the same time, all global variables and functions without the static prefix have global visibility. Therefore, we use static to define functions with the same name and variables with the same name in different files, you don't have to worry about naming conflicts.
(2) The second role of static is to keep the variable content persistent. Variables stored in the static data area will be initialized at the beginning of the program, which is also the only initialization. There are two types of variables stored in the static storage area: global variables and static variables.
(3) The third role of static is that the default Initialization is 0. In fact, global variables also have this attribute, because global variables are also stored in the static data zone. In the static data area, the default values of all bytes in the memory are 0 × 00. In some cases, this feature can reduce the workload of programmers.

2. What is the use of const in C ++?

Don't say const is a constant as soon as you hear it. This gives the examiner a feeling of talking to a layman. It should be said that the content modified by const cannot be changed. Defining constants is just a way to use them. There are also const data members, const parameters, const return values, and const member functions, all things modified by const are protected by force, which can prevent unexpected changes and improve program robustness.


3. How do C and C ++ define constants? What is the difference?

In C, macro # define is used for definition, and C ++ uses better const for definition.

1) const is a constant with a data type, but a macro constant does not exist. The compiler can perform a static type security check on the former, and the latter can only be replaced by characters without a type security check, in addition, unexpected errors (marginal effect) may occur during character replacement ).

2) Some compilers can debug const constants rather than macros.

4. Since C ++ has a better const, why should I use Macros?

Const cannot replace macros as guard posts to prevent repeated file inclusion.

5. What is the difference between reference and pointer in C ++?

Reference is the alias of an object, and Operation Reference is the operation of this object. It must be initialized at the same time of creation (reference a valid object, cannot be NULL), and cannot be changed after initialization, references are pointer-efficient, convenient, and intuitive to use variables. At the language level, references are generally implemented through pointers, just like object usage, the compiler helped us complete the conversion. the reason why reference is used is to use appropriate tools to do the right thing, reflecting the principle of least privilege.

6. How do I allocate C and C ++ memory?

1) distributed from the static storage area. When the program is compiled, it has been allocated. This program exists throughout the runtime, such as global variables and static variables.

2) create a stack. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.

3) Allocate (dynamic memory allocation) on the stack when the program is running and apply for any amount of memory with malloc or new. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by yourself, and the usage is flexible.

7. What is the difference between new/delete and malloc ()/free?

Malloc () and free () are C-language standard library functions, and new/delete are C ++ operators. They can be used to apply for and release memory, malloc () and free () the constructor and destructor tasks are not allowed to be imposed on the compiler.

8. What is the difference between # include <a. h> and # include "a. h?

A: For # include <. h>, the compiler searches for. h for # include ". h ", the compiler searches for. h

9. Why should I add the extern "C" to call the function compiled by the C compiler in the C ++ program "?

C ++ supports function overloading, while C does not. The name of the function in the library after being compiled by C ++ is different from that in C language. Assume that the prototype of a function is void foo (int x, int y). After the function is compiled by the C compiler, its name in the library is _ foo, the C ++ compiler generates names such as _ foo_int_int. C ++ provides a C connection exchange with the specified symbol extern "C" to solve the name matching problem.

10. What is polymorphism in C ++? How is it implemented?

Polymorphism is the third basic feature of object-oriented programming language after data abstraction and inheritance. It is implemented by using the derived classes and virtual functions during running. The same function name is used in the base class and the derived class to perform different operations, that is, "w h a t" is separated from "h o w. Polymorphism improves Code Organization and readability. virtual functions are isolated based on different types.

11. What are dynamic features?

In most cases, the functions of a program are determined during compilation, which is called static features. on the contrary, if the function of a program can be determined at the runtime, it is called a dynamic feature. In C ++, virtual functions, abstract base classes, dynamic binding and polymorphism constitute excellent dynamic characteristics.

12. What is encapsulation? How is C ++ implemented?

Encapsulation comes from the design concept of information hiding. It creates a new data type by combining features and behaviors to isolate interfaces from specific implementations. C ++ is implemented through the class, in order to avoid the behavior of a module interfere with other modules in the same system, should make the module only public interface must let the outside know. http://hovertree.com/menu/cpp/

13. What is RTTI?

Run-time type identification determines the exact type of an object when there is only one pointer or reference to the base class.

14. What is a copy constructor?

It is a constructor of a single parameter. Its parameter is a (frequent) reference to an object of the same class as it. If you do not provide your own copy constructor in the class definition, C ++ provides a default copy constructor that completes copying a member to a Member.

15. What is deep copy?

A shallow copy creates an object and initializes it with a ready-made object. It only copies members (simple assignment) the resources allocated to the member are not copied (for example, the pointer variable member is allocated with dynamic memory). When an object is created, if resources are allocated, you need to define your own copy constructor so that it not only copies members but also copies the resources allocated to it. http://hovertree.com/menu/c/

16. What are the advantages of object-oriented programming?

Short development time, high efficiency, and high reliability. The coding of object-oriented programming is highly reusable. Mature class libraries (such as STL) can be widely used in applications, which makes the software easy to maintain and upgrade even though the development time is short.


Recommended: http://www.cnblogs.com/roucheng/p/cpptimu.html

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.