Skip to content

Constructor / Destructor

Constructor: A special function that is automatically called when an object is created. Used to initialize the object.

Destructor: A special function that is automatically called when an object is destroyed. Used to clean up memory.

Key Rules

ConstructorDestructor
NameSame as classSame as class with ~
CalledObject createdObject destroyed
Return typeNoneNone
OverloadingYesNo
ArgumentsYesNo
CountMultipleOnly one

Basic Example

cpp
class Car {
public:
    string brand;
    Car(string b) { // Constructor
        brand = b;
        cout << brand << " created!" << endl;
    }
    ~Car() { // Destructor
        cout << brand << " destroyed!" << endl;
    }
};

int main() {
    Car car1("BMW");  // Constructor called automatically
    Car car2("Audi"); // Constructor called automatically
}
// Output:
// BMW created!
// Audi created!
// Audi destroyed! (destroyed in reverse order)
// BMW destroyed!

Types of Constructors

1. Default Constructor

cpp
class Car {
public:
    Car() { cout << "Default constructor" << endl; }
};

2. Parameterized Constructor

cpp
class Car {
public:
    string brand; int speed;
    Car(string b, int s) { brand = b; speed = s; }
};

3. Copy Constructor

cpp
class Car {
public:
    string brand;
    Car(string b) : brand(b) {}
    Car(const Car& c) { // copy constructor
        brand = c.brand;
        cout << "Copy constructor called" << endl;
    }
};
// Car car2 = car1; → copy constructor called

Initializer List (Preferred)

cpp
class Car {
public:
    string brand; int speed;
    Car(string b, int s) : brand(b), speed(s) {
        cout << "Car created!" << endl;
    }
};

Initializer list is faster than assigning inside the body — preferred in interviews.

Destructor with Dynamic Memory

cpp
class Car {
    int* data;
public:
    Car() {
        data = new int[100]; // heap memory allocated
    }
    ~Car() {
        delete[] data; // must free heap memory here!
    }
};

If you allocate memory with new inside a constructor, always free it in the destructor to avoid memory leaks.

Constructor / Destructor Order with Inheritance

// Output when B obj is created (B inherits A):
// A created    ← parent constructor first
// B created    ← child constructor second
// B destroyed  ← child destructor first
// A destroyed  ← parent destructor last

Constructor: Parent first, Child last. Destructor: Child first, Parent last.

Virtual Destructor — Important!

cpp
class Animal {
public:
    virtual ~Animal() { // virtual destructor — CORRECT
        cout << "Animal destroyed" << endl;
    }
};
// Without virtual: only Animal destructor called when deleting via base pointer
// With virtual: both Dog and Animal destructors called correctly

Rule: Always make the base class destructor virtual when using inheritance with pointers.

One-liner: "Constructor initializes an object when it's created, and destructor cleans up resources when the object is destroyed — both are called automatically by the compiler."