30 C++ Interview Questions and Answers (+Quiz!)

William ImohWilliam Imoh

C++ Interview Questions and Answers

30 C++ Interview Questions and Answers

C++ interviews may not exactly be a walk in the park. Recruiters today ask more sophisticated questions that test your understanding of the programming language and how you approach problems.

If you have a C++ interview coming up, you may want to brush up on questions and answers typically asked by recruiters. In this guide, you’ll learn 30 C++ interview questions, grouped by difficulty level and by important OOP concepts like classes, objects, functions, and inheritance.

This will help you review the basics while also getting comfortable with advanced topics that recruiters often ask about.

TL;DR:

  • Learn 30 common C++ interview questions

  • Test yourself with flashcards

  • Review key OOP concepts like classes, objects, functions, and inheritance

  • Strengthen understanding of both basic and advanced topics

  • Explore the C++ roadmap to continue mastering the language

To go even further, you can use the roadmap AI tutor to get help and ask questions, understand concepts, track your progress, and receive personalized learning advice.

Tips for preparing for your C++ interview

Preparing for an interview where your C++ coding skills are tested can feel challenging, especially if you’re a beginner. These tips will help you prepare better, answer each question confidently, and code your way to the final selection.

  • Master the fundamentals: Start with the fundamentals like OOP concepts, memory management, data structures, and algorithms to cover all your bases. Read up on these concepts and practice the syntax.

  • Know the standard library: Familiarize yourself with the Standard Template Library (STL). It's a key part of modern C++ development. If you’re applying for a position that requires some experience in C++, study containers, algorithms, and smart pointers.

  • Practice problem-solving and debugging: You might be asked to write code to solve a problem on a whiteboard or a shared editor. Similarly, you might get a debugging task where the code is already written, and it’s your job to fix any bugs. You can find C++ practice tests online to prepare for such scenarios.

  • Explain your work precisely and clearly: A piece of code that works isn’t enough. You should be able to explain the logic behind it.

  • Brush up on the latest features: A lot has been introduced with C++11, C++14, C++17, and C++20. Learn about the new capabilities and how they can be used to program various applications.

  • Prepare for behavioral questions: Hiring managers also want to learn how you’ll fit into their development teams. They’re interested to learn about your experiences and interpersonal skills. Besides technical C++-related questions, prepare to discuss your previous projects and collaborations.

Test yourself with Flashcards

You can either use these flashcards or jump to the questions list section below to see them in a list format.

0 / 30
Knew0 ItemsLearnt0 ItemsSkipped0 Items

1. What is C++? How is it different from C?

C++ is a general-purpose, object-oriented programming language built as an extension of C. While C focuses on structured programming, C++ adds OOP features like classes, inheritance, polymorphism, and encapsulation. It also supports function and operator overloading, plus templates, making code more reusable, efficient, and flexible.

Questions List

If you prefer to see the questions in a list format, you can find them below.

C++ interview questions for beginners (Basic concepts)

1. What is C++? How is it different from C?

C++ is a general-purpose, object-oriented programming language built as an extension of C. While C focuses on structured programming, C++ adds OOP features like classes, inheritance, polymorphism, and encapsulation. It also supports function and operator overloading, plus templates, making code more reusable, efficient, and flexible.

2. What are access modifiers in C++?

Access modifiers control who can access class members and data members. They help enforce object oriented programming principles like encapsulation. There are three access modifiers in C++:

  • public: Accessible from anywhere. and class objects outside the class can call these member functions.

  • private: Accessible only inside the same class. Private and protected members cannot be accessed directly by other classes or friend functions, unless declared as a friend class. If no access specifier is provided, the default is private for classes and public for structs.

  • protected: Accessible within the class and its derived class(es). A derived class object can use these members, but code outside the hierarchy cannot.

cpp
class Example {private:    int secret;  // only within classpublic:    int value;   // accessible anywhereprotected:    int code;    // accessible in derived classes};

3. What is std in C++?

std is a namespace in C++ that stands for standard. It contains the most standard library, including essential components like input/output streams (cin, cout), string manipulation (string), and various data structures and algorithms (vector, sort).

Here’s an example of how namespace works:

cpp
#include <iostream>using namespace std;int main() {    cout << "Hello, World!"; // using std::cout    return 0;}

4. What is a pointer in C++, and how is it different from a reference?

A pointer stores the memory address of a variable. Each pointer has a unique memory address and can directly access the value it points to. Pointers are key for dynamic memory allocation with new and delete, giving control over manual memory management. Misuse can cause leaks or dangling pointers.

Reference, on the other hand, is an alias to an existing variable. The main difference between the two is that the pointer can be null and can also be reassigned. However, a reference can’t be null or reassigned. So, it must be initialized at declaration.

cpp
int x = 10;int *p = &x;    // pointerint &r = x;     // referencecout << *p;     // dereference pointer -> prints 10cout << r;      // reference -> prints 10

5. What is the difference between stack memory and heap memory?

Stack memory handles static allocation (local variables, function calls), while heap memory manages dynamic allocation (objects, data that grow or shrink).

The table below highlights the key differences.

Stack memory

Heap memory

Automatically managed by the compiler

Managed manually using new and delete

Faster allocation and deallocation

Slower allocation and deallocation

Smaller in size

Larger in size

Stores local variables and function calls

Stores dynamically allocated variables

Examples:

cpp
int a = 10;              // stored on stackint* p = new int(20);    // stored on heapdelete p;                // free heap memory

6. What is the difference between pass by value and pass by reference?

In pass-by-value, only a copy of the data is sent, keeping the original safe. In pass-by-reference, the actual data is shared, so any changes affect the original.

See the table below for a clearer comparison.

Pass by value

Pass by reference

A copy of the variable is created and passed to the function

The actual variable is passed using a reference (&)

Function works on the copy; changes inside do not affect the original

Function works on the original; changes inside do affect the original

More memory usage if large objects are copied

More memory efficient since no copy is made

Safer (original data remains unchanged)

Riskier (original data can be unintentionally modified)

Slightly slower for large objects (due to copying)

Faster for large objects since only a reference is passed

Commonly used for simple data types (int, char, float)

Commonly used when modifying objects, or for efficiency in passing large structures/classes

Here’s an example:

cpp
void byValue(int x) { x = 20; }void byReference(int &y) { y = 20; }int main() {    int a = 10, b = 10;    byValue(a);        // a remains 10    byReference(b);    // b becomes 20}

For positions that require deeper knowledge of C++, interviewers often focus on questions of intermediate difficulty. These questions cover more advanced concepts and may require you to write code examples to showcase your understanding.

C++ interview questions for intermediate coders

7. Explain Shallow Copy, Deep Copy, and Copy Constructor in C++

A copy constructor in C++ is a special constructor used to create a new object as a copy of an existing one. By default, the compiler provides a shallow copy constructor, which duplicates only the outer structure of the object.

A shallow copy means copying an object in a way that only the outer structure is duplicated. If the object has pointers or references inside it, both the original and the copy will still point to the same memory. This makes it fast, but risky, because changes in one object can affect the other. Additionally, it may cause issues such as double deletion.

A deep copy, on the other hand, makes a full, independent copy of the object, including any data the object points to. This ensures that the original and the copy are completely separate, so a change in one does not affect the other. Deep copying takes more time and memory, but it is safer.

Note: By default, C++’s copy constructor and copy assignment operator perform a shallow copy unless you override them.

Here’s an example that explains shallow copy:

cpp
#include <iostream>class DeepCopy {public:    int* data;    // Constructor    DeepCopy(int value) {        data = new int(value);    }    // User-defined copy constructor (deep copy)    DeepCopy(const DeepCopy& other) {        data = new int(*other.data);  // allocate new memory and copy the value    }    ~DeepCopy() {        delete data;    }    void print() {        std::cout << "Value: " << *data << std::endl;    }};int main() {    DeepCopy obj1(10);    DeepCopy obj2 = obj1; // Deep copy happens here    *obj2.data = 20;      // Modifying obj2 does NOT affect obj1    obj1.print();         // Output: Value: 10    obj2.print();         // Output: Value: 20    // Both destructors delete different memory blocks safely.    return 0;}

Tip: If your class manages ownership (e.g., new/delete), implement the Rule of 3 (copy ctor, copy assign, dtor) or Rule of 5 (add move ctor and move assign). It’s usually better to follow the Rule of 0 by using RAII types (std::string, std::vector, smart pointers) to avoid manual memory management.

8. What is a recursive function? Provide an example.

A recursive function calls itself to solve a problem by breaking it into smaller subproblems, with at least one base case to stop recursion. You can use recursion when the structure of the problem is naturally recursive (trees, divide-and-conquer, backtracking).

Example (factorial + tail recursion variant):

cpp
#include <stdexcept>long long fact(long long n) {    if (n < 0) throw std::invalid_argument("neg!");    if (n == 0) return 1;         // base case    return n * fact(n - 1);       // recursive step}// tail-recursive helper (may be optimized by some compilers)long long fact_tail(long long n, long long acc = 1) {    if (n < 0) throw std::invalid_argument("neg!");    return n == 0 ? acc : fact_tail(n - 1, n * acc);}

Mistakes to avoid: Deep recursion risks stack overflow. It’s better to implement iterative solutions or explicit stacks for very deep calls. For tree or graph problems, ensure visited checks to avoid infinite recursion.

9. What is Function Overloading in C++ (Compile Time Polymorphism)?

Function overloading allows multiple functions with the same name but different parameter lists (types or arity). The compiler will pick the best match at compile-time.

Here’s an example:

cpp
#include <stdexcept>long long fact(long long n) {    if (n < 0) throw std::invalid_argument("neg!");    if (n == 0) return 1;         // base case    return n * fact(n - 1);       // recursive step}// tail-recursive helper (may be optimized by some compilers)long long fact_tail(long long n, long long acc = 1) {    if (n < 0) throw std::invalid_argument("neg!");    return n == 0 ? acc : fact_tail(n - 1, n * acc);}

10. What is the difference between struct and class in C++?

The main difference between a struct and a class in C++ is the default access specifier for their members. Members of a struct are public by default, while members of a class are private by default. Besides that, they’re the same language feature. struct can do everything that class can do.

You can use a struct to group related data. Because its members are public by default, you can access them directly from outside the struct.

Here’s the syntax for each: struct S { int a; }; // a is public by default
class C { int a; }; // a is private by default

11. What are decltype and auto keywords used for?

  • auto deduces the variable type from the initializer. It’s great for long iterator types, lambdas, and templates.

  • decltype(expr) yields the exact type of an expression (including references and cv-qualifiers) without evaluating it.

cpp
auto i = 42;                 // intconst int& cref = i;decltype(cref) j = i;        // j is const int&template <class T, class U>auto add(T a, U b) -> decltype(a + b) { return a + b; }  // trailing return

Tip: Use auto for readability when the type is evident from the context. Use decltype in templates and metaprogramming.

12. What is a friend function?

A friend function (or class) has access to a class’s private and protected members. It can be used sparingly to implement symmetric operators or tightly coupled utilities without exposing internals publicly.

Here’s an example:

cpp
class Box {    double w{0}, h{0}, d{0};public:    Box(double w, double h, double d): w(w), h(h), d(d) {}    friend double volume(const Box& b);   // grant access};double volume(const Box& b) { return b.w * b.h * b.d; }

13. What is object slicing in C++? How can you avoid it?

Object slicing occurs when a derived object is copied by value into a base object, losing the derived part. During this process, the extra data members of the derived class are "sliced off" or lost, leaving only the base class's members.

cpp
struct Base { virtual ~Base() = default; virtual void f() const {} };struct Derived : Base { int extra{42}; void f() const override {} };void takesBaseByValue(Base b); // slicing riskDerived d;takesBaseByValue(d); // Derived part sliced away

Object slicing typically happens in two scenarios:

  1. Passing by value: When a derived class object is passed to a function that accepts a base class object by value

  2. Assignment: When a derived class object is assigned to a base class object

Avoid slicing by:

  • Pass and store by pointer or reference to base: void takesBaseRef(const Base& b);.

  • Use smart pointers (std::unique_ptr<Base>, std::shared_ptr<Base>) and ensure a virtual destructor in the base class.

  • For value semantics across a hierarchy, consider type erasure or std::variant (design dependent).

14. What is the difference between an abstract class and an interface in C++? How do you implement an interface-like behavior in C++?

An abstract class is a class with at least one pure virtual function (= 0). It can also have data members, constructors, non-virtual functions, and default implementations of virtual functions. You cannot instantiate it.

C++ has no interface keyword. In object-oriented programming, an interface is a contract that defines a set of methods that a class must implement, without providing any implementation details.

While C++ doesn’t have an interface, you can get interface-like behavior with the help of an abstract class with only pure virtual functions and a virtual destructor.

Other alternatives:

  1. C++20 concepts provide compile-time “interfaces” for templates (no runtime polymorphism).

  2. Type-erasure (e.g., std::function, custom wrappers) can provide a runtime “interface” without inheritance.

cpp
struct IShape {                   // interface-like: pure virtuals only    virtual ~IShape() = default;  // always virtual dtor in polymorphic base    virtual double area() const = 0;    virtual void draw() const = 0;};class Circle final : public IShape {    double r_;public:    explicit Circle(double r) : r_(r) {}    double area() const override { return 3.141592653589793 * r_ * r_; }    void draw() const override { /* draw circle */ }};

15. Explain the concept of encapsulation with an example.

Encapsulation is one of the core principles of OOP. It bundles data (member variables) with the methods (member functions) that operate on that data inside a single unit, called a class. It restricts direct access to some parts of an object and hides the data to protect its integrity. Encapsulation prevents external code from directly manipulating an object's internal state. Instead, you interact with the data through public methods (getters and setters), which provide a secure interface.

Here’s an example of a class BankAccount, where the member balance is private and encapsulated.

cpp
class BankAccount {public:    explicit BankAccount(double opening) { deposit(opening); }    void deposit(double amount) {        if (amount < 0) throw std::invalid_argument("neg deposit");        balance_ += amount;    }    void withdraw(double amount) {        if (amount < 0 || amount > balance_) throw std::invalid_argument("bad withdraw");        balance_ -= amount;    }    double balance() const { return balance_; }  // read-only accessorprivate:    double balance_ {0.0};  // hidden; only modified via methods};

16. What is multiple inheritance, and what problems can it cause?

Multiple inheritance (MI) lets a class inherit from more than one base: class D : public A, public B {}. Here are the common issues that result from MI:

  • Ambiguity: Two bases provide the same member; need qualification.

  • Diamond problem: Shared base is inherited twice; two subobjects, ambiguity and duplication.

  • Complexity: Increases coupling and maintenance burden.

cpp
struct A { int x = 1; };struct B { int x = 2; };struct D : A, B {    int f() { return A::x + B::x; } // disambiguate};// Diamond with virtual inheritance:struct VBase { int id = 7; };struct V1 : virtual VBase {};struct V2 : virtual VBase {};struct VDiamond : V1, V2 {    int g() { return id; } // only one VBase subobject};

Here’s how MI can be mitigated:

  • Use virtual inheritance to share a single base subobject in diamond patterns.

  • Favor composition over MI when possible.

Here’s an example:

cpp
#include <memory>#include <vector>struct Animal {    virtual ~Animal() = default;    virtual const char* speak() const { return "???"; }};struct Dog : Animal {    const char* speak() const override { return "woof"; }};struct Cat : Animal {    const char* speak() const override { return "meow"; }};int main() {    std::vector<std::unique_ptr<Animal>> zoo;    zoo.emplace_back(std::make_unique<Dog>());    zoo.emplace_back(std::make_unique<Cat>());    for (const auto& a : zoo) { /* prints woof, meow */ a->speak(); }}

Tips:

  • Always give polymorphic bases a virtual destructor.

  • Use override in derived methods; consider final to prevent further overrides.

  • Virtual calls don’t bind inline (can inhibit some optimizations), but are the right tool for runtime behavior variation.

17. What is the difference between static data members and normal data members?

  • Normal (non-static) member: Each object has its own copy.

  • Static data member: Shared among all instances; exists even with no instances. Define it out-of-class (until C++17) or as inline static (C++17+).

  • Static member function: Works without a this pointer and can only access static members directly.

Here are examples of both:

cpp
#include <string>class Session {public:    Session(std::string user) : user_(std::move(user)) { ++live_count; }    ~Session() { --live_count; }    static int live() { return live_count; }   // static member function    const std::string& user() const { return user_; }private:    std::string user_;              // normal: per-object    inline static int live_count=0; // C++17+: defined in-class};

18. What is the “this” pointer, and how is it used?

Inside a non-static member function, this is a pointer to the current object (ClassName*). It’s used to:

  • Disambiguate member names from parameters

  • Return *this to enable method chaining

  • Implement move/assignment operators safely (self-assignment check)

  • Obtain the object address for APIs expecting a pointer

In const member functions, the type of this is ClassName const*.

Example:

cpp
class Builder { int x_{0}, y_{0};public: Builder& setX(int x) { this->x_ = x; return *this; } // chaining Builder& setY(int y) { y_ = y; return *this; } // `this->` optional};class Resource { int* p_{nullptr};public: Resource& operator=(Resource&& other) noexcept { if (this != &other) { // self-assignment guard uses `this` delete p_; p_ = other.p_; other.p_ = nullptr; } return *this; // chaining }};

19. What is Function Overriding in C++ and How Does the Base Class Affect It?

In function overriding, a derived class provides a new implementation for a virtual function with the same signature as in the base. Resolution happens at runtime (dynamic dispatch). In function overloading, you have multiple functions with the same name but different parameter lists. Resolution happens at compile time.

Here’s an example of function overriding:

javascript
struct Base {    virtual ~Base() = default;    virtual void log(int x) const { /* base impl */ }};struct Derived : Base {    void log(int x) const override { /* derived impl */ } // overriding};// Overloading example (same scope):void print(int);void print(const char*);

20. What is a function pointer, and how do you use it?

A function pointer stores the address of a function with a specific signature, which lets you call it indirectly or pass it around (callbacks, table-driven code). In modern C++, use std::function or auto + lambdas for flexibility.

However, raw pointers are lightweight and ABI-stable.

cpp
#include <iostream>// a free functionint add(int a, int b) { return a + b; }// pointer to function taking (int,int) returning intint (*op)(int, int) = nullptr;int main() {    op = &add;                 // or just: op = add;    std::cout << op(2, 3);     // calls add(2,3)}

With arrays/dispatch tables:

cpp
int sub(int a, int b) { return a - b; }int mul(int a, int b) { return a * b; }int (*ops[])(int,int) = { add, sub, mul };int apply(int i, int a, int b) { return ops[i](a, b); }

Member function pointers have different syntax and require an object:

cpp
struct Calc {    int inc(int x) const { return x + 1; }};int (Calc::*mf)(int) const = &Calc::inc;Calc c;int y = (c.*mf)(41); // 42

21. How does inline expansion of functions affect performance?

Inline expansion replaces a function call with the function body, potentially reducing call overhead and enabling further optimizations (constant propagation, loop unrolling). However, it can increase code size (ICache pressure) and sometimes compromise performance due to code bloat.

inline int square(int x) { return x * x; } // may be inlined

22. How is exception handling implemented in C++?

Exception handling in C++ is implemented using three keywords: try, catch, and throw. This mechanism allows a program to deal with runtime errors in a structured way so that it doesn’t stop abruptly.

Example:

cpp
#include <iostream>#include <stdexcept>struct Guard { ~Guard(){ std::cout << "cleanup\n"; } };int compute(bool bad) {    Guard g; // will be destroyed during unwinding    if (bad) throw std::runtime_error("boom");    return 42;}int main() {    try {        std::cout << compute(true) << "\n";    } catch (const std::exception& e) {        std::cout << "caught: " << e.what() << "\n";    }}

23. What is the difference between std::exception and user-defined exceptions?

std::exception is the root of the standard hierarchy and provides a virtual what() string. Standard library errors derive from it (e.g., std::bad_alloc, std::runtime_error, std::out_of_range).

User-defined exceptions let you encode domain context (identifiers, file path, error code). As long as they ultimately derive from std::exception, callers can catch generically while still matching specific types when desired.

cpp
try {    // ...} catch (const std::exception& e) { // catches std + user exceptions (if derived)    // uniform handling}

24. How can RAII (Resource Acquisition is Initialization) help in exception safety?

RAII binds a resource’s lifetime to an object’s lifetime so that destructors perform cleanup automatically during normal execution and during exception unwinding. This prevents leaks and makes code exception-safe.

Without RAII, if an exception occurs after a resource has been acquired but before it is released, the resource would leak. You'd have to write manual cleanup code in catch blocks, which is tedious and prone to errors.

Example:

cpp
#include <fstream>#include <memory>#include <vector>std::vector<int> readAll(const char* path) {    std::ifstream in(path);                  // RAII for file    if (!in) throw std::runtime_error("open failed");    auto data = std::make_unique<std::vector<int>>(); // RAII memory    int x;    while (in >> x) data->push_back(x);    return *data; // NRVO/move; no leaks even if exceptions occur}// Strong guarantee via copy-then-swapstruct Buffer {    std::vector<char> bytes;    void append(const std::vector<char>& more) {        auto tmp = bytes;            // copy        tmp.insert(tmp.end(), more.begin(), more.end());        bytes.swap(tmp);             // commit    }};

Concurrency RAII: Lock guards unlock even on exceptions.

cpp
#include <mutex>std::mutex m;void safe_increment(int& x) {    std::lock_guard<std::mutex> lk(m); // unlocks on exit or exception    ++x;}

This section goes beyond the basics and focuses on how well you can apply C++ concepts in real-world situations. The questions often explore topics like performance optimization, memory management, and design patterns, which require hands-on experience to answer confidently.

You may also come across newer and less familiar C++ features, such as smart pointers, move semantics, and concurrency. These areas help interviewers see the difference between developers who have just learned the language and those who have used it in more advanced scenarios.

Advanced C++ Interview Questions

25. Explain templates in C++ and provide a simple example.

Templates enable generic programming by parameterizing code over types (and values, since C++11/14/17/20). The compiler generates concrete instantiations on use.

Here are the different templates and what they do:

  • Function templates generalize algorithms.

  • Class templates generalize data structures.

  • Non-type template parameters accept values (e.g., sizes).

  • Constraints or Concepts (C++20) restrict valid template arguments.

cpp
// function templatetemplate <class T>T max_of(T a, T b) { return (a < b) ? b : a; }// class template with non-type parametertemplate <class T, std::size_t N>struct FixedArray {    T data[N]{};    constexpr std::size_t size() const { return N; }};// C++20 constrained template#include <concepts>template <std::totally_ordered T>T min_of(T a, T b) { return (b < a) ? b : a; }

Mistakes to avoid: 

Beware of code bloat from many instantiations; prefer auto return or decltype(auto) and concepts for clearer diagnostics.

26. What is the Standard Template Library (STL)? Name some commonly used containers.

The STL is the generic algorithms, containers, iterators and function objects foundation of the C++ standard library.

Some common containers from STL include:

  • Sequence: std::vector, std::array, std::deque, std::list, std::forward_list

  • Associative (ordered, tree-based): std::set, std::multiset, std::map, std::multimap

  • Unordered (hash-based): std::unordered_set, std::unordered_map, std::unordered_multiset, std::unordered_multimap

  • Container adaptors: std::stack, std::queue, std::priority_queue

  • Algorithms (on iterator ranges): std::sort, std::find, std::accumulate, std::transform, std::binary_search, etc.

Here’s one example: 

javascript
#include <vector>#include <algorithm>#include <numeric>std::vector<int> v{3,1,4,1,5};std::sort(v.begin(), v.end());int sum = std::accumulate(v.begin(), v.end(), 0);

27. What are lambda expressions in C++11 and later?

Lambdas are inline, anonymous function objects with optional captures. They enable concise callbacks and algorithms.

Syntax: [capture] (params) mutable noexcept -> ret { body }

Here’s an example: 

javascript
#include <vector>#include <algorithm>#include <iostream>std::vector<int> v{5,2,9,1};int pivot = 4;// capture by reference:int count_lt = std::count_if(v.begin(), v.end(), [&](int x){ return x < pivot; });// init-capture (move):auto big = [buf = std::vector<int>(1000)](int x){ return x + buf.size(); };// generic lambda:auto add = [](auto a, auto b){ return a + b; };std::cout << add(1, 2.5) << "\n";

28. What are move semantics in C++11, and why are they important?

Move semantics allow resources to be transferred (moved) from temporaries or expiring objects instead of expensive deep copies, enabling performance gains and exception safety improvements.

  • Rvalue references (T&&) bind to temporaries.

  • Move constructor / move assignment steal resources.

  • std::move(x) casts to an rvalue to enable moving (does not move by itself).

  • Mark moves noexcept when possible (STL containers rely on this to provide strong guarantees during reallocation). 

Example:

javascript
#include <vector>#include <utility>struct Buffer {    std::vector<char> data;    Buffer() = default;    Buffer(const Buffer&) = default;                  // copy    Buffer& operator=(const Buffer&) = default;    Buffer(Buffer&& other) noexcept : data(std::move(other.data)) {}    Buffer& operator=(Buffer&& other) noexcept {        if (this != &other) data = std::move(other.data);        return *this;    }};Buffer make_buffer() {    Buffer b; b.data.resize(1'000'000);    return b; // NRVO or move}

29. What is lvalue and rvalue?

An lvalue is an object that occupies a specific memory location and can be referred to by name. It exists beyond a single expression, and you can usually take its address with the & operator.

javascript
int x = 5;   // x is an lvaluex = 10;      // assignment works because x refers to a memory locationint* p = &x; // ok, you can take the address of x

An rvalue is a temporary value that does not have a persistent memory address. It is usually produced during expression evaluation and often appears on the right-hand side of assignments. 

javascript
int y = x + 5;   // (x + 5) is an rvaluey = 42;          // 42 is an rvalue literal
  1. What is SFINAE? How does it relate to template specialization?

SFINAE stands for Substitution Failure Is Not An Error. It is a C++ template metaprogramming rule where, if template substitution fails, the compiler does not throw an error but instead removes that candidate from consideration. This feature is often used for template specialization to enable or disable certain functions depending on the types provided.

javascript
#include <type_traits>#include <iostream>// Overload only if T is an integral typetemplate <typename T>typename std::enable_if<std::is_integral<T>::value, void>::typeprint(T) {    std::cout << "Integral type\n";}// Overload only if T is a floating-point typetemplate <typename T>typename std::enable_if<std::is_floating_point<T>::value, void>::typeprint(T) {    std::cout << "Floating-point type\n";}int main() {    print(42);    // Integral type    print(3.14);  // Floating-point type}

In template specialization, you write explicit rules for how a template behaves with certain types.

javascript
template <typename T>struct Traits;   // Primary templatetemplate <>struct Traits<int> {   // Explicit specialization    static const char* name() { return "int"; }};template <>struct Traits<double> {    static const char* name() { return "double"; }};

Final thoughts

The C++ interview guide covers most of the key concepts at beginner, intermediate, and advanced levels. Review the fundamentals, go through the example programs in this guide, and most importantly, start working on the project ideas from the roadmap guide and share your solutions to get feedback from the community.

As always, practice makes perfect. Practicing with the flashcards or working on additional projects and exercises will help you stand out from other candidates. 

And don’t forget, it’s not just your coding skills they want to test, but your overall knowledge of the concepts, confidence, and soft skills like communication and teamwork. So present yourself as a well-rounded candidate who is ready to crush it. 

If you want a structured path to strengthen your skills further, check out the C++ roadmap for a step-by-step guide to mastering the language.

Join the Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Rank 6th out of 28M!

341K

GitHub Stars

Star us on GitHub
Help us reach #1

+90kevery month

+2.1M

Registered Users

Register yourself
Commit to your growth

+2kevery month

41K

Discord Members

Join on Discord
Join the community

RoadmapsBest PracticesGuidesVideosFAQsYouTube

roadmap.shby@kamrify

Community created roadmaps, best practices, projects, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh·Terms·Privacy·

ThewNewStack

The top DevOps resource for Kubernetes, cloud-native computing, and large-scale development and deployment.