C++ Interview Questions
C++ is a core topic in systems, game, embedded, and backend interviews, where interviewers probe memory management and object-oriented design closely. These are the questions they actually ask, with concise answers you can speak confidently.
18 questions with concise, interview-ready answers.
1. What is the difference between C and C++?
C is a procedural language built around functions and structs, while C++ is a multi-paradigm language that adds object-oriented programming, classes, inheritance, and polymorphism on top of C. C++ also introduces features like references, function and operator overloading, templates, exceptions, namespaces, and the Standard Template Library. C++ is largely backward-compatible with C, but it enforces stricter type checking.
2. What are the four pillars of OOP in C++?
The four pillars are encapsulation, abstraction, inheritance, and polymorphism. Encapsulation bundles data and the methods that operate on it inside a class and restricts direct access using access specifiers like private. Abstraction exposes only essential behavior while hiding implementation details. Inheritance lets a class derive from another to reuse and extend behavior, and polymorphism lets the same interface behave differently depending on the actual object type.
3. What is the difference between a reference and a pointer?
A pointer is a variable that holds a memory address, can be reassigned to point elsewhere, can be null, and is dereferenced with the * operator. A reference is an alias for an existing variable, must be initialized when declared, cannot be reseated to refer to a different object, and cannot be null. References give cleaner syntax and are commonly used for function parameters to avoid copying.
4. What are virtual functions and how do they enable polymorphism?
A virtual function is a member function declared with the virtual keyword that can be overridden in a derived class. When you call it through a base-class pointer or reference, C++ dispatches to the derived class's implementation at runtime based on the actual object type rather than the pointer type. This runtime dispatch is what enables runtime (dynamic) polymorphism in C++.
5. What is a vtable and how does it work?
A vtable (virtual table) is a per-class table of pointers to the class's virtual functions, created by the compiler for any class with virtual functions. Each object of such a class holds a hidden pointer, often called the vptr, that points to its class's vtable. When a virtual function is called through a base pointer, the program follows the vptr to the vtable and looks up the correct function to run, which is how dynamic dispatch is implemented.
6. What is the difference between an abstract class and an interface in C++?
C++ has no separate interface keyword. An abstract class is any class with at least one pure virtual function, declared with = 0, and cannot be instantiated directly. The closest thing to an interface is an abstract class where every member function is pure virtual and there is no data, so it defines a contract that derived classes must implement. Abstract classes may also contain data members and ordinary implemented methods, unlike a pure interface.
7. What is a pure virtual function?
A pure virtual function is a virtual function with no implementation in the base class, declared by assigning = 0 in its declaration, for example virtual void draw() = 0;. It forces every concrete derived class to provide its own implementation. Any class containing a pure virtual function becomes abstract and cannot be instantiated on its own.
8. What are constructors and destructors?
A constructor is a special member function with the same name as the class that runs automatically when an object is created, and it is used to initialize members and acquire resources. A destructor, named with a tilde prefix like ~ClassName, runs automatically when the object goes out of scope or is deleted, and it releases resources the object held. Constructors can be overloaded, but a class has only one destructor.
9. Why should a base class destructor be virtual?
If you delete a derived object through a base-class pointer and the base destructor is not virtual, only the base destructor runs, so the derived part is not cleaned up and you leak resources. Declaring the base destructor virtual ensures the full chain of destructors runs in the correct order. As a rule, any class meant to be used polymorphically through base pointers should have a virtual destructor.
10. What is a copy constructor?
A copy constructor creates a new object as a copy of an existing one of the same type, and its signature takes a const reference to that object, for example ClassName(const ClassName& other). It is called when an object is initialized from another, passed by value, or returned by value. If you do not write one, the compiler generates a default copy constructor that copies each member.
11. What is the difference between a shallow copy and a deep copy?
A shallow copy duplicates the member values directly, so if the object holds a pointer, both the original and the copy end up pointing to the same memory. A deep copy allocates new memory and copies the pointed-to data, giving each object its own independent resources. Classes that own raw pointers usually need a custom copy constructor and copy assignment operator to perform a deep copy and avoid double-free or aliasing bugs.
12. What is the difference between function overloading and operator overloading?
Function overloading lets you define multiple functions with the same name but different parameter lists, and the compiler picks the right one based on the arguments at compile time. Operator overloading lets you define how operators like +, ==, or << behave for your own types, for example so two custom objects can be added with +. Both are forms of compile-time (static) polymorphism.
13. What is the STL and what are vector, map, and set?
The STL (Standard Template Library) is a collection of generic, template-based containers, iterators, and algorithms in the C++ standard library. A vector is a dynamic array with fast random access and amortized constant-time append at the end. A map is an ordered associative container of key-value pairs, typically implemented as a balanced binary search tree with logarithmic lookup, and a set stores unique sorted keys. There are also unordered_map and unordered_set that use hashing for average constant-time access.
14. What are templates in C++?
Templates let you write generic code that works with any type, with the actual type filled in by the compiler at instantiation. Function templates generate a function for each type used, and class templates, like std::vector<T>, generate a class per type. This gives you type safety and reuse without runtime overhead, because each instantiation is resolved at compile time.
15. What is the difference between new/delete and malloc/free?
new and delete are C++ operators that allocate and free memory and, crucially, also call the object's constructor and destructor, and new returns a correctly typed pointer. malloc and free are C library functions that only allocate and release raw memory without running any constructors or destructors, and malloc returns a void pointer you must cast. In C++ you should prefer new and delete, or better, smart pointers, so object lifetimes are handled correctly.
16. What is RAII?
RAII stands for Resource Acquisition Is Initialization, a core C++ idiom where a resource such as memory, a file handle, or a lock is tied to the lifetime of an object. The resource is acquired in the constructor and released in the destructor, so when the object goes out of scope the resource is automatically cleaned up, even if an exception is thrown. This makes code exception-safe and is the foundation behind smart pointers and standard lock guards.
17. What are smart pointers, and how do unique_ptr and shared_ptr differ?
Smart pointers are RAII wrappers in the <memory> header that manage dynamically allocated memory and free it automatically, avoiding manual delete and memory leaks. A unique_ptr expresses exclusive ownership, cannot be copied, and frees the object when it goes out of scope. A shared_ptr allows shared ownership through reference counting and frees the object when the last shared_ptr is destroyed, while a weak_ptr observes a shared_ptr without affecting the count to break reference cycles.
18. What does the const keyword do in C++?
const marks something as read-only so the compiler prevents modifying it. It can make a variable a constant, make a pointer or the data it points to immutable, and mark a member function as const to promise it does not change the object's state, which lets you call it on const objects. Passing parameters as const references is a common pattern to avoid copying while guaranteeing the function will not modify the argument.
Get these answered live in your real interview
NostrobeAI is a real-time AI interview copilot — it hears the question and drafts a strong answer on your screen, invisible on Zoom, Meet, and Teams. One-time pricing, no subscription.
Try NostrobeAI free