Top C++ Interview Questions
A concise, interview-ready rundown of the C++ questions that come up again and again — covering OOP, memory management, STL, and modern C++ — with straight-to-the-point answers.

C++ interviews tend to circle back to the same core set of ideas — how objects are managed, how memory is handled, and whether you actually understand the "why" behind the syntax, not just the syntax itself. Here's the set that shows up most consistently, answered directly.
OOP Fundamentals
1. What's the difference between a class and a struct in C++?
Functionally almost identical — the only real difference is default access: class members are private by default, struct members are public by default. By convention, structs are used for simple data bundles and classes for types with real behavior.
2. What is polymorphism, and what's the difference between compile-time and runtime polymorphism? Polymorphism means the same interface behaves differently depending on the object involved. Compile-time polymorphism (function/operator overloading) is resolved by the compiler before the program runs. Runtime polymorphism (virtual functions) is resolved while the program is running, based on the object's actual type.
3. What is a virtual function, and why does it matter?
A member function declared with virtual in a base class, intended to be overridden in a derived class. Without it, calling a function through a base-class pointer always runs the base class's version, even if the object is actually a derived type — virtual fixes that by deferring the decision to runtime.
4. Why do you need a virtual destructor? If a base class is ever deleted through a base-class pointer to a derived object, and the destructor isn't virtual, only the base class's destructor runs — the derived class's cleanup is skipped, causing a resource leak. Any class meant to be inherited from and deleted polymorphically needs a virtual destructor.
Memory and Pointers
5. What's the difference between a pointer and a reference? A pointer stores an address and can be reassigned or set to null. A reference is an alias for an existing variable, must be initialized immediately, and can never be reassigned to refer to something else.
6. What's the difference between stack and heap memory?
Stack memory is automatically managed — allocated and freed as functions are called and return — and is fast but limited in size. Heap memory is manually managed (new/delete, or smart pointers), larger, but slower and prone to leaks if not handled carefully.
7. What is a memory leak, and how do smart pointers help?
A memory leak happens when dynamically allocated memory is never freed, because the pointer to it is lost or delete is never called. Smart pointers (unique_ptr, shared_ptr) apply RAII to free memory automatically when they go out of scope, removing the need for manual delete calls entirely.
8. What's the difference between shallow copy and deep copy? A shallow copy duplicates a pointer's value (the address), leaving both objects pointing at the same underlying memory — dangerous if one is destroyed. A deep copy duplicates the actual data being pointed to, giving each object independent, safely-owned memory.
STL and Language Features
9. What's the difference between vector and array in C++?
array is fixed-size, set at compile time. vector is dynamic — it can grow and shrink at runtime, at the cost of some overhead compared to a raw array.
10. What's the difference between map and unordered_map?
map keeps keys sorted (usually backed by a balanced tree), giving O(log n) operations. unordered_map uses hashing for average O(1) operations but gives no ordering guarantee.
11. What are templates, and why does the STL rely on them?
Templates let a function or class work generically across multiple types, with the compiler generating a specific version for each type actually used. The entire STL — every container and most algorithms — is built as templates, which is why vector<int> and vector<std::string> share one implementation.
12. What is operator overloading?
Redefining how a built-in operator (+, ==, <<, etc.) behaves for a user-defined type, so objects of that type can be used with natural, intuitive syntax instead of explicit method calls.
Modern C++ and Concurrency
13. What's the difference between unique_ptr and shared_ptr?
unique_ptr allows exactly one owner at a time and can't be copied, only moved. shared_ptr allows multiple owners via reference counting, freeing the memory only once the last owner is destroyed.
14. What is a race condition, and how do you prevent one?
A bug that occurs when multiple threads access shared, mutable data at the same time without synchronization, producing unpredictable results. It's prevented with synchronization primitives like std::mutex, typically wrapped in a std::lock_guard for automatic, exception-safe unlocking.
15. What is move semantics, and why was it introduced? Move semantics (introduced in C++11) let a resource be transferred from a temporary or no-longer-needed object instead of being copied, which avoids expensive, unnecessary duplication — especially relevant for containers and smart pointers that manage heap-allocated resources.
How to Actually Prepare
Knowing these answers by heart isn't the goal — being able to explain why each answer is true, and writing small code snippets to demonstrate each concept live, is what separates a memorized answer from real understanding in an interview setting. If any of these feel shaky, the dedicated posts on OOP, pointers, smart pointers, and multithreading each go a level deeper.
The Takeaway
C++ interviews reward depth over breadth — a small, well-understood core of memory management, OOP principles, and STL fluency will carry you through most questions you'll actually be asked. Get comfortable explaining these fifteen out loud, in your own words, and the syntax-level questions tend to take care of themselves.


