Interview Q&A · C++ · 10 questions in this topic

Classes, Objects & Constructors — C++ Interview Q&A

All C++ topics

How C++ classes are born and die — constructors, destructors, copy/move, member initialization and object lifetime. Part of Classes & OOP Pillars · 213 questions in the C++ bank.

What is a class and an object in C++?

Answer

A class is a user-defined type bundling data members and member functions with access control (private/protected/public). An object is an instance of a class with its own storage and lifetime — automatic (stack), static, thread-local or dynamic (heap).

Unlike Java, C++ objects can live directly on the stack with value semantics — no new required — and are destroyed deterministically when their scope ends.

Example

Example.cppcpp
class Account {
public:
    explicit Account(double b) : balance_(b) {}
    void deposit(double a) { balance_ += a; }
private:
    double balance_ = 0;
};
Account a(100.0); // stack object, value semantics
a.deposit(50);

💡 Stress value semantics and deterministic destruction — that is the C++ object model's headline difference from Java/Python.

What are the special member functions (rule of 0/3/5)?

Answer

The six special members are default constructor, destructor, copy constructor, copy assignment, move constructor and move assignment. The rule of 0 says: manage no raw resources — let members handle themselves and declare nothing. The rule of 5 says: if you define any of destructor/copy/move, define (or explicitly delete/default) all five. The rule of 3 is the C++98 predecessor (destructor, copy ctor, copy assign).

Modern code aims for rule of 0 via std::string, containers and smart pointers.

Key points

  1. 1Rule of 0 — no custom resource management; the default members are correct.
  2. 2Rule of 5 — custom destructor/copy/move come as a package.
  3. 3Declaring a destructor suppresses implicit move generation (a classic trap).

Example

Example.cppcpp
class Buffer {
public:
    Buffer() = default;
    ~Buffer();
    Buffer(const Buffer&);            // copy ctor
    Buffer& operator=(const Buffer&); // copy assign
    Buffer(Buffer&&) noexcept;        // move ctor
    Buffer& operator=(Buffer&&) noexcept;
};

Constructor vs destructor — what runs, when, and in what order?

Answer

Constructors initialize an object (bases then members in declaration order, then the ctor body); destructors tear it down in exact reverse (body, members reversed, bases reversed). Members initialize in declaration order regardless of the initializer-list order — mismatching them compiles but may warn and surprises readers.

Destructors of base classes intended for polymorphic deletion must be virtual, or derived parts leak.

Example

Example.cppcpp
struct Base { Base(); virtual ~Base() = default; };
struct Derived : Base {
    std::string s;
    Derived() : Base(), s("hi") {} // Base first, then members
};

💡 Say forward-construct, reverse-destroy in one sentence — interviewers accept that as the complete ordering rule.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits