How to Pass a C++ Interview: Top Questions from A to Z
An interview for a C++ developer position is a test not only of syntax knowledge, but also of a deep understanding of how the compiler works, memory management, and the features of object-oriented programming. In this article, we have broken down the top questions from A to Z that are most often asked in interviews. The material will be useful for both junior developers and those preparing for a middle level.
1. Basics of Syntax and Data Types
Let's start with the basics — questions that test your understanding of fundamental C++ concepts.
1.1. What is the difference between a reference and a pointer?
This is a classic question. The main differences are:
- A reference cannot be
nullptr, a pointer can. - A reference cannot be rebound to another object after initialization.
- Pointers support arithmetic, references do not.
int a = 5;int& ref = a; // referenceint* ptr = &a; // pointer
ref = 10; // a = 10*ptr = 20; // a = 201.2. What is const and how to use it correctly?
const in C++ is used to protect data from modification. It is important to distinguish between const before the type and after it:
const int* ptr— pointer to a constant integer (cannot change the value at the address).int* const ptr— constant pointer to an integer (cannot change the address).
2. Object-Oriented Programming (OOP)
OOP questions are a mandatory part of a C++ interview. Let's break down the key topics.
2.1. Virtual Functions and Polymorphism
Virtual functions provide late binding (dynamic polymorphism). Example:
class Base {public: virtual void show() { std::cout << "Base"; } virtual ~Base() = default;};
class Derived : public Base {public: void show() override { std::cout << "Derived"; }};
int main() { Base* b = new Derived(); b->show(); // Prints "Derived" delete b;}It is important to remember the virtual destructor — without it, when deleting an object through a base class pointer, only the base class destructor will be called, leading to a memory leak.
2.2. Pure Virtual Functions and Abstract Classes
A class containing at least one pure virtual function (= 0) becomes abstract. You cannot create an instance of such a class — only a derived class that implements all pure virtual methods.
2.3. What are vtable and vptr?
To implement polymorphism, the compiler creates a virtual table (vtable) for each class with virtual functions. Each object contains a hidden pointer (vptr) to this table. Calling a virtual function occurs through indirect access via vptr, which adds a small overhead.
3. Memory Management and Smart Pointers
C++ gives the developer full control over memory, which requires special care.
3.1. Difference between new/delete and malloc/free
mallocallocates memory but does not call a constructor.freefrees memory without calling a destructor.newallocates memory and calls a constructor.deletecalls a destructor and frees memory.
3.2. Smart Pointers: unique_ptr, shared_ptr, weak_ptr
Smart pointers automatically manage the lifetime of an object. Key points:
std::unique_ptr— exclusive ownership, cannot be copied, only moved.std::shared_ptr— shared ownership via a reference counter.std::weak_ptr— does not increase the reference counter, and