16 commonly used questions in a C + + face

Source: Internet
Author: User
Tags shallow copy least privilege

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 that do not have a static prefix have global visibility, so use static to define a function with the same name and a variable of the same name in different files without worrying about naming conflicts.

(2) The second function of static is to keep the contents of a variable persistent. Variables stored in the static data area are initialized at the start of the program and are the only one initialized. A total of two variables are stored in static storage: Global variables and static variables.

(3) The third function of static is initialized to 0 by default. The global variable also has this property because the global variable is also stored in the static data area. In the static data area, all bytes in memory default values are 0x00, sometimes this feature can reduce the workload of the programmer.

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

Don't say a constant when you hear a const, giving the examiner a feeling of talking to a layman. It should be said that the content of the Const modifier can not be changed, the definition of constants is just a way to use, there are const data members, const parameters, const return values, const member functions, and so on, the const modification of things are enforced protection, to prevent accidental changes, Can improve the robustness of the program.

3. How does C and C + + each define constants? What's the difference?

c is defined using a macro # define, and C + + uses a better const to define it.

Difference:

1) const is a constant with data type, and the macro constant does not, the compiler can be static type security check, the latter is only a character substitution, there is no type security check, and the character substitution may produce unexpected errors (marginal effect).

2) Some compilers can debug const constants and cannot debug macros.

4. Why use macros if there is a better const in C + +?

Const cannot replace a macro as a guard whistle to prevent duplicate inclusion of a file.

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

A reference is an alias to an object, an action reference is a manipulation of the object, it must be initialized at the same time it is created (referencing a valid object, NOT NULL), the initialization is no longer immutable, the reference has the efficiency of the pointer, and the convenience and intuition of variable use, At the language level, as with the use of objects, references at the binary level are generally implemented by pointers, but the compiler helps us to complete the conversion. References are used to do the right thing with the appropriate tools, and the principle of least privilege is embodied.

6. How to say C and C + + memory allocation?

1) allocation from a static storage area. Memory is allocated when the program is compiled, and the entire runtime of the program is present, such as global variables, static variables.

2) Create on the stack. When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.

3) allocation from the heap (dynamic memory allocation) The program uses malloc or new to request any amount of memory at run time, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by itself and is very flexible to use.

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

malloc () and free () are standard library functions of the C language, New/delete are C + + operators that can be used to request and release memory, and malloc () and no () are not within the control of the compiler and cannot impose constructors and destructors on their tasks. Www.cdtarena.com

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

A: For # include <a.h>, the compiler starts searching from the standard library path A.H for # include "A.h", the compiler starts the search from the user's working path A.H

9. Why do I add extern "C" when I call a function compiled by the C compiler in a C + + program?

The C + + language supports function overloading, which does not support function overloading. Functions are compiled in C + + with different names in the library than in the C language. Suppose a function is prototyped as: void foo (int x, int y), the function is compiled by the C compiler in the library with the name _foo, and the C + + compiler produces names like _foo_int_int. C + + provides a connection exchange with the specified symbol extern "C" to resolve the name matching problem.

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

Polymorphism is the third basic feature of object-oriented programming language following data abstraction and inheritance. It is the polymorphism that occurs at run time, implemented by derived classes and virtual functions. The same function name is used in the base class and derived classes to accomplish different operations to isolate the other type of interface, that is, "W h a T" from "H o W" separated. Polymorphism improves the organization and readability of code, and virtual functions are isolated according to the different types.

11. What is a dynamic feature?

In the vast majority of cases, the function of the program is determined at compile time, which we call static characteristics. Conversely, if the function of the program can be determined at runtime, it is called dynamic characteristics. In C + +, virtual functions, abstract base classes, dynamic bindings, and polymorphism form excellent dynamic properties.

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

Encapsulation comes from the idea of information hiding, which is to create new data types by combining features and behaviors to isolate interfaces from specific implementations. C + + is implemented through classes, in order to try to avoid the behavior of a module to interfere with other modules in the same system, should let the module only expose the interface must be known to the outside world.

13. What is Rtti?

Rtti refers to runtime type recognition (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 argument whose argument is a (constant) reference to an object of the same class as it is, and if it does not provide its own copy constructor, C + + provides a default copy constructor that completes a member-to-member copy of the default copy constructor

15. What is a depth copy?

A shallow copy is the creation of an object that initializes it with a ready-made object simply by copying the member (simple assignment) without copying the resources assigned to the member (such as assigning dynamic memory to its pointer variable members); Deep copy is when an object is created, if the resource is allocated, you need to define your own copy constructor. So that not only copy members but also copy the resources assigned to it.

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

Short development time, high efficiency and high reliability. Object-oriented programming is highly reusable and can be used in applications with a large number of mature class libraries (such as STL), which makes the software easy to maintain and upgrade, although it is short of development time.

16 commonly used questions in a C + + face

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.