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().
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:
Ans: The new operator is used for memory allocation, and deletes operator is used for memory deallocation in C++.
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
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 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:
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.
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.
|