C++ Interview Questions and Answers | Tutorials Logic
Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials
C++

Top 25 C++ Interview Questions

Curated questions covering OOP, STL, memory management, templates, polymorphism, and modern C++ features.

01

What is C++ and how does it differ from C?

C++ is a general-purpose language that extends C with object-oriented programming, templates, the STL, exception handling, and references. C is procedural; C++ supports multiple paradigms (procedural, OOP, generic, functional).

02

What are the four pillars of OOP in C++?

  • Encapsulation — data hiding using access specifiers (private, protected, public).
  • Inheritance — deriving new classes from existing ones.
  • Polymorphism — same interface, different behaviour (compile-time and runtime).
  • Abstraction — hiding implementation details using abstract classes and interfaces.
03

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

In C++, the only difference is default access: struct members are public by default; class members are private by default. Conventionally, struct is used for plain data; class for objects with behaviour.

04

What are constructors and destructors?

A constructor initialises an object when it is created. A destructor cleans up when the object is destroyed. C++ supports default, parameterised, and copy constructors.

Example
class MyClass {
public:
    MyClass() { cout << "Constructor"; }
    MyClass(int x) : value(x) {}
    ~MyClass() { cout << "Destructor"; }
private:
    int value;
};
05

What is the difference between new/delete and malloc/free?

  • new/delete — C++ operators; call constructors/destructors; type-safe; throw exceptions on failure.
  • malloc/free — C functions; no constructor/destructor calls; return void*; return NULL on failure.
  • Always use new/delete in C++.
06

What is a virtual function?

A virtual function enables runtime polymorphism. When a base class pointer calls a virtual function, the derived class's version is called. Declared with the virtual keyword.

Example
class Animal {
public:
    virtual string speak() { return "..."; }
};
class Dog : public Animal {
public:
    string speak() override { return "Woof"; }
};
Animal* a = new Dog();
cout << a->speak(); // "Woof" � runtime polymorphism
07

What is a pure virtual function and abstract class?

A pure virtual function (= 0) has no implementation in the base class. A class with at least one pure virtual function is abstract and cannot be instantiated. Derived classes must implement all pure virtual functions.

Example
class Shape {
public:
    virtual double area() = 0; // pure virtual
};
class Circle : public Shape {
    double area() override { return M_PI * r * r; }
};
08

What is function overloading?

Function overloading allows multiple functions with the same name but different parameter lists. The compiler selects the correct version at compile time (static polymorphism).

Example
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
string add(string a, string b) { return a + b; }
09

What is operator overloading?

Operator overloading allows defining custom behaviour for operators (+, -, ==, etc.) for user-defined types.

Example
class Vector {
public:
    int x, y;
    Vector operator+(const Vector& v) const {
        return {x + v.x, y + v.y};
    }
};
10

What are references in C++?

A reference is an alias for an existing variable. Unlike pointers, references cannot be null, cannot be reassigned, and do not need dereferencing. Used for pass-by-reference and avoiding copies.

Example
int x = 10;
int& ref = x;  // ref is an alias for x
ref = 20;      // x is now 20
11

What is the difference between a pointer and a reference?

  • Pointer — can be null; can be reassigned; needs dereferencing (*ptr).
  • Reference — cannot be null; cannot be reassigned after initialisation; no dereferencing needed.
  • Use references when you always have a valid object; pointers when null is possible.
12

What are templates in C++?

Templates enable generic programming — writing code that works with any data type. Function templates and class templates are the two types.

Example
template<typename T>
T max(T a, T b) { return a > b ? a : b; }

template<typename T>
class Stack {
    vector<T> data;
public:
    void push(T val) { data.push_back(val); }
};
13

What is the STL (Standard Template Library)?

The STL provides generic containers, algorithms, and iterators. Key containers: vector, list, deque, map, set, unordered_map, stack, queue. Key algorithms: sort, find, transform, accumulate.

14

What is the difference between vector and array?

  • array — fixed size; stack-allocated; no bounds checking.
  • vector — dynamic size; heap-allocated; provides push_back, size, resize, and bounds checking with at().
Example
vector<int> v = {1, 2, 3};
v.push_back(4);
cout << v.size(); // 4
15

What is RAII (Resource Acquisition Is Initialisation)?

RAII ties resource management to object lifetime. Resources (memory, files, locks) are acquired in the constructor and released in the destructor. This ensures resources are always released, even if exceptions occur. Smart pointers implement RAII.

16

What are smart pointers in C++?

  • unique_ptr — exclusive ownership; automatically deleted when out of scope.
  • shared_ptr — shared ownership; reference counted; deleted when last owner is destroyed.
  • weak_ptr — non-owning reference to a shared_ptr; prevents circular references.
Example
auto p = make_unique<int>(42);
auto sp = make_shared<MyClass>();
17

What is the Rule of Three/Five?

  • Rule of Three — if you define a destructor, copy constructor, or copy assignment operator, define all three.
  • Rule of Five (C++11) — also define move constructor and move assignment operator.
  • Rule of Zero — prefer using smart pointers and STL containers to avoid manual resource management.
18

What is move semantics in C++11?

Move semantics allow transferring resources from a temporary (rvalue) object instead of copying, improving performance. Implemented with move constructor and move assignment operator using rvalue references (&&).

Example
vector<int> a = {1, 2, 3};
vector<int> b = move(a); // a is now empty; no copy made
19

What is the difference between stack and heap in C++?

  • Stack — automatic storage; local variables; fast allocation; limited size; LIFO.
  • Heap — dynamic storage; allocated with new; larger; must be freed with delete; slower allocation.
20

What is exception handling in C++?

C++ uses try/catch/throw for exception handling. Exceptions can be any type. Use catch(...) to catch all exceptions. Always catch by reference to avoid slicing.

Example
try {
    if (x < 0) throw runtime_error("Negative value");
    // ...
} catch (const runtime_error& e) {
    cerr << e.what();
} catch (...) {
    cerr << "Unknown error";
}
21

What is the difference between compile-time and runtime polymorphism?

  • Compile-time (static) — function overloading, operator overloading, templates. Resolved at compile time.
  • Runtime (dynamic) — virtual functions and inheritance. Resolved at runtime via vtable.
22

What is a vtable?

A vtable (virtual table) is a lookup table of function pointers for virtual functions. Each class with virtual functions has a vtable. Objects contain a vptr (pointer to vtable) used to dispatch virtual function calls at runtime.

23

What are lambda expressions in C++11?

Lambdas are anonymous functions. They can capture variables from the enclosing scope.

Example
auto add = [](int a, int b) { return a + b; };
int x = 10;
auto addX = [x](int n) { return n + x; }; // capture x by value
sort(v.begin(), v.end(), [](int a, int b) { return a > b; });
24

What is the difference between shallow copy and deep copy?

Shallow copy copies the pointer values — both objects point to the same memory. Deep copy allocates new memory and copies the data. The default copy constructor performs a shallow copy; you must define a copy constructor for deep copy.

25

What are the access specifiers in C++?

  • public — accessible from anywhere.
  • protected — accessible within the class and derived classes.
  • private — accessible only within the class.
  • Default for class is private; default for struct is public.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.