C++ Interview Questions


Q11: What is the difference between shallow copy and deep copy?

Ans: The difference between shallow copy and a deep copy is given below:

Shallow Copy

Shallow copy stores the object references to the original address in memory.
Shallow copy is faster.
Shallow copy reflects the modifications to the original object of the new/copied item.

Deep Copy

Deep copy makes a new and separate copy of an entire object with its unique memory address.
Deep copy is comparatively slower.
A deep copy does not reflect changes in the source object made to the new/copied item.

Q12: 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:
Any function of a static member is not virtual
Static members have no 'this' pointer functions
For static members functions, the constant, volatile, and volatile declaration is not provided

Q13: Explain ‘this’ pointer?

Ans: The "this" point is a constant pointer and contains the current object's memory address. It passes all non-statutory function calls as a concealed parameter. It is also available in the non-static body of all functions as a local variable.
The "this" pointer is not available for them as static member functions can also be invoked without objects, i.e., class names.

Q14: What is the 'diamond problem' that occurs with multiple inheritances in C++? Explain using an example.

Ans: The C++ diamond problem highlights the programming language's inability to allow mixed heritage via many and hierarchical heritages.

Suppose we had a university with certain professors and students. A basic legacy plan may have several sorts of individuals in various positions in this situation. They all belong to the same class of persons, though.

The Personal class defines a getRole() abstract function, which is then overridden by its subclasses to return the right role model. Things are straightforward till this point; however, if you want to mimic your position as a TA or teaching assistant, things are strange.

A teacher is both a student and a professor. As seen in the image below, this causes the diamond problem:

The dilemma creates a diamond-like heritage diagram and, therefore, the diamond issue. What should the Teaching Assistant be inherited from the getRole() implementation? Student or member of the Faculty? The Teaching Assistant class may override getRole() and return a newly defined role, says TA. However, such a reply is far from comprehensive because it conceals that both a faculty member and a graduate student are teaching assistants.

Q15: Define the copy builder used in C++ together with its generic prototype

Ans: function. In addition, describe the different calling circumstances.
Ans: The copy builder in C++ is recognized as a member function that initializes an object through a different object of the same class. It can also be made private using Copy Constructor. In any of the following four circumstances, a call to the copy builder may occur
when:

  • The compiler generates a temporary item
  • An object is built or built on another item of the same kin
  • A value returns an object of the class
  • An item of the class (i.e., a function) is transferred as an argument by value

The general function prototype for the Copy Constructor is:
ClassName (const ClassName &old_obj);
Point(int x1, int y1) { x=x1; y=y1;}
Point(const Point &p2) { x=p2.x; y=p2.y; }