C++ interview questions
Virtual functions, references, constructors, STL and memory in C++.
1. What is a virtual function?
A member function declared with virtual in the base class so that a call through a base pointer or reference runs the derived class's version. This is run-time polymorphism, implemented with a virtual table (vtable).
2. What is a pure virtual function and an abstract class?
A pure virtual function is declared as virtual void f() = 0; and has no body in the base class. A class with at least one is abstract: it cannot be instantiated, and derived classes must override the function.
3. What is the difference between a reference and a pointer?
A reference is another name for an existing variable: it must be initialised, cannot be null and cannot be changed to refer to something else. A pointer holds an address, can be null, can be reassigned and supports pointer arithmetic.
4. What is the difference between new and malloc?
new is an operator that allocates memory and calls the constructor, returns the correct type and throws on failure; it is released with delete. malloc is a C function that only allocates raw bytes, returns void* and NULL on failure; it is released with free.
5. What is a copy constructor? Shallow versus deep copy?
A constructor that creates an object from another object of the same class. The default one copies member by member (shallow), so two objects may share the same heap memory. A deep copy allocates new memory and copies the contents, which you need when a class owns pointers.
6. What is a destructor and why make it virtual?
A destructor (~ClassName) runs when an object is destroyed and releases its resources. In a base class used polymorphically it should be virtual, so deleting a derived object through a base pointer calls the derived destructor too.
7. What is the STL? Name some containers.
The Standard Template Library provides containers (vector, list, deque, set, map, unordered_map, stack, queue), algorithms (sort, find, binary_search) and iterators. vector is a dynamic array with fast random access; list is a doubly linked list with fast insertion in the middle.
8. What are templates?
A way to write generic code that works with any type, such as template <typename T> T maxOf(T a, T b). The compiler generates a version for each type used. The STL is built on templates.
9. What is an inline function?
A request to the compiler to replace the function call with the function body to save call overhead. It suits very small functions; the compiler may ignore the request.
10. What is a friend function?
A non-member function (or class) given access to a class's private and protected members by declaring it with friend inside the class. Useful for operator overloading such as <<.
11. What are smart pointers?
Classes that manage a heap object and free it automatically: unique_ptr (single owner), shared_ptr (reference-counted shared ownership) and weak_ptr (non-owning link to a shared_ptr). They prevent memory leaks.