C++ Interview Questions


Q6: Explain the significance of vTable and vptr in C++ and how the compiler deals with them

Ans: Table is a table containing function pointers. There is a vTable in each class. Vptr is a vTable pointer. There is a vptr for every object. The C++ compiler adds additional code to two locations to maintain and utilize vptr and vTable:
This code sets vptr in every constructor:
Of the object being created
To point to vTable of the class
Polymorphic functional call code — The compilers insert the code to first check for vptr with a base class point of reference in every spot where a polymorphic call is made. Once the vptr is properly selected, the vTable of a derived class is accessible. Accessed and called with the vTable are the address of the derived class function show().

Q7: What does a Static member in C++ mean?

Ans: In the Static Storage Area, a static member is assigned storage once during the Program life as indicated by the static keyword. Some key facts about the static members are:

Q8: How do you allocate and deallocate memory in C++?

Ans: The new operator is used for memory allocation, and deletes operator is used for memory deallocation in C++.

For example-

int value=new int; //allocates memory for storing 1 integer
delete value; // deallocates memory taken by value

int *arr=new int[10]; //allocates memory for storing 10 int
delete []arr; // deallocates memory occupied by arr

Q9: Explain Virtual Functions and the concept of Runtime Polymorphism in C++ with a code example.

Ans: Any virtual keyword-accompanying function shows a virtual function's behavior. In contrast to regular functions called per pointer type or reference, virtual functions are called in accordance with the pointed or referenced object type.

Virtual functions solve at runtime, not at any time, straightforwardly. Virtual functions may alternatively be interpreted as a C++ program using the polymorphism idea. Important things to write a C++ virtual function are:

A base class

A function of the same name for both the base class and the derived class A base class type pointer or reference pointing, or referring, to a derived class object correspondingly

An example demonstrating the use of virtual functions (or runtime polymorphism at play) is:

#include using namespace std; class Base { public: virtual void show() { cout<<" In Base \n"; } }; class Derived: public Base { public: void show() { cout<<"In Derived \n"; } }; int main(void) { Base *bp = new Derived; bp->show(); // <- Runtime Polymorphism in Action return 0; }

In the program mentioned above bp is a pointer of type Base. A call to bp->show() calls show() function of the Derived class. This is because bp points to an object of the Derived class.

Q10: What differences separate structure from a class in C++?

Ans: There are two significant class-structural differences in C++. The following are: The default access to the base class or structure is public when the structure is derived from a class or another structure. Instead, when a class is derived, the default access specifier is private.

While a structure's members are public by default, class members are, by default, private.