The questions that come up again and again, with crisp answers. Each links back to a topic in these notes for depth.
Fundamentals
What is the difference between struct and class in C++?
What is the difference between a pointer and a reference?
What are the differences between new/delete and malloc/free?
What does the static keyword mean in different contexts?
What is the difference between const and constexpr?
OOP
How does runtime polymorphism work in C++?
Why should a base class destructor be virtual?
What is object slicing?
Can constructors be virtual? Can destructors be pure virtual?
What is the diamond problem?
Memory & lifetime
What are the Rule of Three, Five and Zero?
Explain move semantics.
unique_ptr vs shared_ptr vs weak_ptr?
What is RAII?
What is a memory leak and how do you prevent it in C++?
STL & templates
vector vs list? map vs unordered_map?
What is iterator invalidation?
How does std::sort work and what does it require?
What are templates and when are they instantiated?
What are concepts?
Modern C++ & concurrency
What is the difference between std::thread and std::async?
What is a data race and how do you prevent it?
What does noexcept do and why does it matter for performance?
What is std::move vs std::forward?
Tricky output questions
#include <iostream>
struct A {
A() { std::cout << "A"; }
virtual ~A() { std::cout << "~A"; }
virtual void f() { std::cout << "A::f "; }
};
struct B : A {
B() { std::cout << "B "; }
~B() override { std::cout << "~B"; }
void f() override { std::cout << "B::f "; }
};
int main() {
A* p = new B; // prints "AB "
p->f(); // virtual dispatch
A a = *p; // slicing: copy of the A part (copy ctor prints nothing)
a.f();
delete p; // virtual destructor: ~B then ~A
std::cout << '\n';
} // then the sliced copy `a` is destroyedAB B::f A::f ~B~A
~A#include <iostream>
#include <vector>
int main() {
int i = 5;
int& r = i;
int* p = &i;
r++;
(*p) *= 2;
std::cout << i << ' ';
std::vector<int> v{1, 2, 3};
auto a = v; // copy
auto& b = v; // alias
a.push_back(4);
b.push_back(5);
std::cout << v.size() << a.size() << ' ';
int x = 10;
auto byVal = [x]() { return x; };
auto byRef = [&x]() { return x; };
x = 20;
std::cout << byVal() << ' ' << byRef() << ' ';
std::cout << sizeof(char) << sizeof(bool) << '\n';
}12 44 10 20 11