Part-D of C++ interview questions along with their answers:
31. What are the differences between a shallow copy and a deep copy?
- Shallow Copy: A shallow copy copies the memory addresses of the data members from one object to another. Both objects end up pointing to the same memory locations. Changes in one object affect the other.
- Deep Copy: A deep copy creates a new copy of the data from one object to another, so each object has its own independent memory. Changes in one object do not affect the other.
32. How is the this
pointer used in C++?
- The
this
pointer is a special pointer that is implicitly passed to all non-static member functions of a class. It points to the object on which the member function is called, allowing access to the object’s data members and member functions within the function.
33. What is the difference between std::shared_ptr
and std::unique_ptr
in C++?
std::shared_ptr
: Allows multiple shared pointers to own and share the same resource. It keeps track of the number of references to the resource using reference counting and deallocates the resource when the last shared pointer goes out of scope.std::unique_ptr
: Enforces exclusive ownership of a resource. It cannot be copied or shared and is typically used for managing resources with a single owner.
34. How is the copy constructor called when passing an object by value to a function?
- When an object is passed by value to a function, the copy constructor is called to create a copy of the object for the function’s parameter. This ensures that changes made to the parameter within the function do not affect the original object.
35. Explain the concept of friend functions in C++.
- A friend function is a function that is granted access to the private and protected members of a class. It is declared in the class but defined outside of it using the
friend
keyword. Friend functions are not members of the class, but they can access its private members.
36. What are virtual destructors? Why are they used?
- A virtual destructor is a destructor that is declared as
virtual
in the base class. When a derived class object is deleted through a base class pointer, a virtual destructor ensures that the correct destructor (of the derived class) is called, preventing memory leaks and proper cleanup of resources.
37. How does C++ handle multiple inheritance? What is the diamond problem?
- C++ supports multiple inheritance, allowing a class to inherit from multiple base classes. The diamond problem occurs when a class inherits from two classes that share a common base class. This can lead to ambiguity in accessing the common base class’s members. C++ resolves this by using virtual inheritance.
38. How is the const
member function different from a non-const member function?
- A
const
member function is a member function that promises not to modify the state of the object. It is called onconst
objects and can only call otherconst
member functions. Non-const member functions can modify the object’s state and are called on non-const objects.
39. What is RAII (Resource Acquisition Is Initialization) in C++?
- RAII is a programming technique where resource management is tied to object lifetime. Resources (like memory, file handles, etc.) are acquired in the constructor and released in the destructor of an object. RAII ensures proper resource cleanup and exception safety.
40. Explain the difference between std::move
and std::forward
in C++.
std::move
: It is used to indicate that an object can be moved from (transfer ownership) rather than copied. It converts an lvalue to an rvalue, enabling efficient move semantics.std::forward
: It is used to preserve the value category (lvalue or rvalue) of a function argument when forwarding it to another function. It is typically used in generic code, such as perfect forwarding in template functions.
Make sure to understand these concepts thoroughly and practice coding problems to perform well in C++ interviews. Good luck!