C++ Concepts

  1. C++C++ might be the most difficult programming language in many software engineers' judgment. Many interviewers believe a candidate who is proficient on C++ has the abilities needed to master other technical skills, so questions about C++ are quite popular during interviews. 

    Interview questions about the C++ programming language can be divided into three categories: C++ concepts, tricky C++ coding problems, and implementing a class or member function.
    C++ Concepts
    Questions in the first category are about C++ concepts, especially about C++ keywords. For example, what are the four keywords for type casting, and under what are scenarios would you use each of them?
    The most popular questions in this category concern the keyword sizeof.
    For instance, the following dialog is repeated again and again in many interviews:

    Interviewer:      There is an empty class, without any member fields and functions inside its definition. What is the result of      sizeof for this class?
    Candidate:       It is 1.
     Interviewer:     Why not 0?
     Candidate:      The size seems to be 0 since the class is empty without any data. However, it should occupy some memory; otherwise, it cannot be accessed. The size of memory is decided by compilers. It occupies 1 byte in Visual Studio.
    Interviewer:     What is the result of sizeof if we add the constructor and destructor functions in the class?
    Candidate:       It is also 1. The addresses for functions are irrelevant to instances, and compilers do not add any data in instances of this class.
    Interviewer:      How about declaring the destructor function as a virtual function?
    Candidate:        When a C++ compiler sees a virtual function inside a class, it creates a virtual function table for the class and adds a pointer to the table in each instance. A pointer in a 32-bit machine occupies 4 bytes, so the result of sizeof is 4. The result on a 64-bit machine is 8 because a pointer occupies 8 bytes there.

No comments:

Top Blogs