Top 30 questions frequently used in C ++ interviews (2)

Source: Internet
Author: User
11. What is the difference between structure and union?

(1 ). the structure and union are composed of multiple members of different data types, but at any time, only one selected member is stored in the Union (all members share an address space ), all members of the structure exist (the storage addresses of different members are different ).

(2) assigning values to different members of the Union will overwrite the values of other Members. The original values of the members do not exist, but the assignment values of different members of the structure do not affect each other.

12. Try to write out the program results:

Int A = 4;

Int & F (int x)

{A = a + X;

Return;

}

Int main (void)

{Int T = 5;

Cout <F (t) <Endl; A = 9

F (t) = 20; A = 20

Cout <F (t) <Endl; t = 5, a = 20 A = 25

T = f (t); A = 30 t = 30

Cout <F (t) <Endl;} t = 60

}

13. What is the difference between overload and overried?

Common Questions. In terms of definition:

Overload: multiple functions with the same name are allowed, and the parameter tables of these functions are different (maybe the number of parameters is different, maybe the parameter types are different, or both are different ).

Rewrite: it refers to the method for the subclass to redefine the parent class virtual function.

In terms of implementation principle:

Overload: the compiler modifies the names of functions with the same name based on different parameter tables of the function, and then these functions with the same name become different functions (at least for the compiler ). For example, there are two functions with the same name: function func (P: integer): integer; and function func (P: string): integer ;. The modified function names of the compiler may be: int_func and str_func. The call to these two functions is determined between compilers and is static. That is to say, their addresses are bound during compilation (early binding). Therefore, overloading is irrelevant to polymorphism!

Rewriting: It is really related to polymorphism. When the subclass redefined the virtual function of the parent class, the parent class pointer dynamically calls the function of the subclass according to the different subclass pointer assigned to it, such a function call cannot be determined during compilation (the virtual function address of the called subclass cannot be provided ). Therefore, such function addresses are bound at runtime (late binding ).

14. Which of the following situations can I use intialization list instead of assignment?

Answer: when the class contains const and reference member variables, the base class constructors need to initialize the table.

15. Is C ++ type-safe?

Answer: No. Two different types of pointers can be forcibly converted (reinterpret cast ). C # is type-safe.

16. What code will be executed before the main function is executed?

Answer: The constructor of the global object is executed before the main function.

17. describes the memory allocation methods and their differences?

1) distributed from the static storage area. The program has been allocated when it is compiled, and the program exists throughout the entire runtime. For example, 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 operations are embedded in the processor's instruction set.

3) distributed from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by the programmer. It is very flexible to use, but the problem is also the most.

18. Write the comparison statements of Boolean, Int, float, and pointer type variables A and zero respectively.

Answer:

Bool: If (! A) or if ()

INT: if (a = 0)

Float: const expressions exp = 0.000001

If (A <exp & A>-exp)

Pointer: if (! = NULL) or if (a = NULL)

19. What are the advantages of const compared with # define?

Answer:

Const functions: defining constants, modifying function parameters, and modifying function return values. All things modified by const are protected by force, which can prevent unexpected changes and improve program robustness.

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, and there is no type security check, and unexpected errors may occur during character replacement.

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

20. What is the difference between arrays and pointers?

An array is either created in a static storage area (such as a global array) or on a stack. Pointers can point to any type of memory block at any time.

(1) Differences in modification content

Char A [] = "hello ";

A [0] = 'X ';

Char * P = "world"; // note that P points to a constant string

P [0] = 'X'; // This error cannot be found by the compiler. It is a runtime error.

(2) the sizeof operator can be used to calculate the array capacity (number of bytes ). Sizeof (P), P is the number of bytes of a pointer variable, rather than the memory capacity referred to by P. C ++/C language cannot know the memory capacity referred to by the pointer unless you remember it when applying for memory. Note: When an array is passed as a function parameter, the array will automatically degrade to a pointer of the same type.

Char A [] = "Hello World ";

Char * P =;

Cout <sizeof (a) <Endl; // 12 bytes

Cout <sizeof (p) <Endl; // 4 bytes

Calculate the memory size of arrays and pointers

Void func (char a [1, 100])

{

Cout <sizeof (a) <Endl; // 4 bytes instead of 100 bytes

}

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.