Some C ++ test questions are excerpted online

Source: Internet
Author: User

C ++ pen questions
1. Is the virtual function table created in the polymorphism class compile-time or run-time?

Answer: The virtual function table was created during the compilation period. Each virtual function is organized into an array of virtual function entry addresses. the hidden Member of the object-virtual function table pointer is initialized at runtime-that is, when the constructor is called, which is the key to implementing polymorphism.

2. A parent class writes a virtual function. If the child class overwrites its function without virtual, can it also implement polymorphism? Is there a function of the parent class or a private variable of the parent class in the subclass space? (Huawei pen) Answer: as long as the base class has declared the virtue keyword when defining the member function, the virtue keyword can be added or not added when the base class is implemented to override this function, the implementation of polymorphism is not affected. All variables of the parent class (except static) exist in the space of the Child class ).

3. the sprintf, strcpy, and memcpy functions can be used to complete string copying. What are the differences between these functions? Which one do you like and why?

Answer: The difference between these functions is that they implement different functions and operate on different objects.
1. The strcpy function operates on a string to copy the source string to the target string.
2. snprintf function operation objects are not limited to strings: although the target object is a string, the source object can be a string or any basic type of data. This function is mainly used to convert (string or basic data type) to a string. If the source object is a string and the % s format character is specified, you can also copy the string.
3. the memcpy function, as its name implies, is a memory copy function that copies the content of one memory block to another. The memory block is determined by its first address and length. The entity object that appears in the program, no matter what type, is expressed as occupying a place (a memory interval or block) in the memory ). Therefore, memcpy's operation object is not limited to a certain type of data, or can be applied to any data type, as long as it can provide the initial address and Memory Length of the object, and the object can be operable. Given the features of long copies such as memcpy functions and the physical meaning of data types, memcpy functions are generally limited to copying data of the same type or between objects, of course, it also includes copying strings and basic data types.

For string copying, the above three functions can be used, but their implementation efficiency and ease of use are different:

1. strcpy is undoubtedly the most appropriate choice: High Efficiency and convenient calls.

2. snprintf requires additional format characters and format conversion, which is troublesome and inefficient.

3. although memcpy is efficient, it requires an additional parameter of the copied memory length, which is easy to error and inconvenient to use. If the length is too large (the optimal length is the length of the source string + 1 ), it will also bring about a reduction in performance. In fact, the strcpy function is generally implemented internally by calling the memcpy function or using the Assembly directly to achieve efficient purposes. Therefore, using memcpy and strcpy to copy strings should have no major performance difference.

4. Compile a C function that provides the number of BITs set to 1 in a byte and at least one different solution to this question.

The first method // I have not succeeded in this method. If the method is successful, do not inform the situation.

Unsigned int testasone0 (char log)

{
Int I;
Unsigned int num = 0, Val;
For (I = 0; I <8; I ++)
{
Val = Log> I; // shift
Val & = 0x01; // Phase 1
If (VAL)
Num ++;
}
Return num;
}
The second kind // I have succeeded in this kind of practice

Unsigned int testasone1 (char log)
{
Int I;
Unsigned int num = 0, Val;

Int op = Log-'0 ';
For (INT I = 0; I <8; I ++)
{
Val = op % 2;
OP = OP/2; // Shift Effect
If (VAL)
Num ++;
}
Return num;
}

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.