Skip to content
  1. Home
  2. Guides
  3. C++ OOPs interview questions

C++ OOPs Interview Questions and Answers (2026)

By Abhishek Kumar Updated 2 December 2026 5 min read

Key takeaways

  • Every C++ interview has an OOPs round: the four pillars, constructors and destructors, and virtual functions with the vtable.
  • The most common traps: a missing virtual destructor, object slicing, the rule of 3/5/0 and the diamond problem.
  • Modern C++ interviewers expect RAII and smart pointers (unique_ptr, shared_ptr) instead of raw new/delete.

Object-oriented programming is the most predictable part of a C++ interview: whether you are a fresher or have 5 years of experience, you will be asked about classes, inheritance and — above all — virtual functions. These are the C++ OOPs interview questions asked most, with answers and code that compiles. For the rest of the language, see the full C++ interview questions page and the C++ STL interview questions.

The basics

What are the four pillars of OOP? Explain each with C++.

  • Encapsulation: bundle data and the methods that use it, and hide internals with private.
  • Abstraction: expose what an object does, not how — through public methods and abstract base classes.
  • Inheritance: a derived class reuses and extends a base class (class Dog : public Animal).
  • Polymorphism: one interface, many behaviours — compile-time (overloading, templates) and run-time (virtual functions).
C++
class BankAccount {                 // encapsulation: balance can't be changed directly
public:
    void deposit(double amount) { if (amount > 0) balance_ += amount; }
    double balance() const { return balance_; }
private:
    double balance_ = 0.0;
};

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

Only the default access: members and base classes are private by default in a class and public in a struct. Otherwise they are identical — both can have constructors, methods and inheritance. By convention, struct is used for plain data and class for types with invariants.

What are the types of constructors, and in what order are constructors and destructors called?

Default, parameterised, copy (T(const T&)), move (T(T&&)) and delegating constructors. Construction runs base → members (in declaration order) → derived; destruction runs in exactly the reverse order.

C++
#include <iostream>
struct Base    { Base()  { std::cout << "Base "; }    ~Base()    { std::cout << "~Base "; } };
struct Derived : Base {
    Derived()  { std::cout << "Derived "; }
    ~Derived() { std::cout << "~Derived "; }
};
int main() { Derived d; }   // prints: Base Derived ~Derived ~Base

What is the difference between function overloading and overriding?

  • Overloading: same name, different parameters, in the same scope — resolved at compile time.
  • Overriding: a derived class redefines a virtual function with the same signature — resolved at run time through the vtable. Mark it with override so the compiler catches signature mistakes.

Polymorphism and virtual functions

How do virtual functions work internally (vtable and vptr)?

Each class with virtual functions has a vtable — a table of function pointers to its implementations. Each object of such a class contains a hidden vptr pointing to its class's vtable. A virtual call through a base pointer follows the vptr to the vtable and calls the right override, which is dynamic dispatch. The cost is one pointer per object and an indirect call, which also blocks inlining.

C++
#include <iostream>
#include <memory>
#include <vector>

struct Shape {
    virtual double area() const = 0;   // pure virtual → Shape is abstract
    virtual ~Shape() = default;        // virtual destructor (see below)
};
struct Circle : Shape {
    double r;
    explicit Circle(double r) : r(r) {}
    double area() const override { return 3.14159 * r * r; }
};
struct Square : Shape {
    double s;
    explicit Square(double s) : s(s) {}
    double area() const override { return s * s; }
};

int main() {
    std::vector<std::unique_ptr<Shape>> shapes;
    shapes.push_back(std::make_unique<Circle>(1.0));
    shapes.push_back(std::make_unique<Square>(2.0));
    for (const auto& s : shapes) std::cout << s->area() << "\n"; // 3.14159 then 4
}

What is an abstract class, and how is it different from an interface?

A class with at least one pure virtual function (= 0) is abstract and can't be instantiated. C++ has no interface keyword — an "interface" is just an abstract class with only pure virtual functions and a virtual destructor. Because C++ supports multiple inheritance, a class can implement several such interfaces.

Why must a base class have a virtual destructor?

If you delete a derived object through a base pointer and the base destructor isn't virtual, only the base destructor runs. That's undefined behaviour, and in practice the derived part's resources leak.

C++
struct Base { ~Base() {} };                 // BUG: not virtual
struct Derived : Base {
    int* data = new int[100];
    ~Derived() { delete[] data; }           // never called below!
};
Base* b = new Derived;
delete b;                                    // UB: ~Derived skipped → leak
// Fix: virtual ~Base() = default;

What is object slicing?

Assigning or passing a derived object by value as a base type copies only the base part — the derived data and override behaviour are "sliced off":

C++
#include <iostream>
struct Animal { virtual const char* sound() const { return "..."; } virtual ~Animal() = default; };
struct Dog : Animal { const char* sound() const override { return "Woof"; } };

void byValue(Animal a)            { std::cout << a.sound() << "\n"; }   // "..." — sliced!
void byRef(const Animal& a)       { std::cout << a.sound() << "\n"; }   // "Woof"

int main() { Dog d; byValue(d); byRef(d); }

Fix: pass polymorphic objects by reference or pointer, or make base classes non-copyable.

Can a constructor be virtual? Can you call a virtual function in a constructor?

A constructor can't be virtual: the object's type (and its vptr) is set up by construction. Calling a virtual function inside a constructor or destructor is legal, but it calls the current class's version, not the derived override, because the derived part doesn't exist yet (or has already been destroyed). For "virtual construction", use a factory function or a clone() method.

Copying, moving and resources

What is the rule of three, five and zero?

  • Rule of three: if a class needs a custom destructor, copy constructor or copy assignment, it probably needs all three (it manages a resource).
  • Rule of five (C++11): add the move constructor and move assignment for efficient transfers.
  • Rule of zero (preferred): don't manage raw resources yourself — use members like std::string, std::vector and std::unique_ptr, and let the compiler generate everything correctly.
C++
#include <cstring>
#include <utility>

class Buffer {                       // rule of five, for a raw resource
public:
    explicit Buffer(std::size_t n) : size_(n), data_(new char[n]) {}
    ~Buffer() { delete[] data_; }
    Buffer(const Buffer& o) : size_(o.size_), data_(new char[o.size_]) {
        std::memcpy(data_, o.data_, size_);
    }
    Buffer& operator=(Buffer o) noexcept { swap(o); return *this; }   // copy-and-swap
    Buffer(Buffer&& o) noexcept
        : size_(std::exchange(o.size_, 0)), data_(std::exchange(o.data_, nullptr)) {}
    void swap(Buffer& o) noexcept { std::swap(size_, o.size_); std::swap(data_, o.data_); }
private:
    std::size_t size_;
    char* data_;
};

The by-value operator= handles both copy and move assignment (copy-and-swap), which gives strong exception safety.

What is RAII, and how do smart pointers use it?

RAII (Resource Acquisition Is Initialisation): a resource's lifetime is tied to an object's lifetime — acquire in the constructor, release in the destructor. Cleanup then happens automatically, even when exceptions are thrown.

  • std::unique_ptr: single ownership, zero overhead, move-only. The default choice.
  • std::shared_ptr: shared ownership with a reference count. It costs more (atomic counts, a control block).
  • std::weak_ptr: a non-owning reference to a shared_ptr object, used to break reference cycles.

Create them with std::make_unique / std::make_shared, and avoid raw new and delete in modern C++.

Inheritance questions

What is the diamond problem, and how does virtual inheritance solve it?

If B and C both inherit from A, and D inherits from both, then D contains two copies of A, and calls to A's members are ambiguous. Inheriting virtually makes B and C share a single A subobject:

C++
#include <iostream>
struct A { int value = 1; };
struct B : virtual A {};
struct C : virtual A {};
struct D : B, C {};

int main() {
    D d;
    d.value = 5;                 // OK: only one A thanks to virtual inheritance
    std::cout << d.value << "\n"; // 5
}

What do public, protected and private inheritance mean?

They limit the maximum access of inherited base members in the derived class. Public inheritance models "is-a" (base public stays public). Protected makes base public members protected. Private makes them private, and models "implemented in terms of" — composition is usually clearer for that.

What is a friend function, and does it break encapsulation?

A friend function or class can access a class's private members. It's commonly used for operators such as operator<<. It doesn't really break encapsulation when the friend is part of the class's own interface — the class itself grants the access — but overusing it is a design smell.

Keep practising

Frequently asked questions

What OOPs questions are asked in C++ interviews?
The four pillars (encapsulation, abstraction, inheritance, polymorphism), constructor types and order, destructors, virtual functions and the vtable, pure virtual functions and abstract classes, virtual destructors, function overloading vs overriding, access specifiers and friend, the rule of three/five/zero, copy vs move, object slicing, the diamond problem and virtual inheritance, and smart pointers.
How is C++ OOP different from Java OOP?
C++ supports multiple inheritance, has no built-in garbage collector (it uses RAII and destructors), makes functions non-virtual by default (you opt in with virtual), allows objects on the stack, and supports operator overloading. Java has single class inheritance with interfaces, garbage collection and virtual-by-default methods.
AK
Abhishek Kumar
Founder, InterviewPitch · 10+ years in IT. Writes InterviewPitch's guides from real interview experience.