Skip to main content
C++ Fundamentals for Beginners to Advanced
CHAPTER 16 Beginner

Constructors and Destructors in C++

Updated: May 17, 2026
5 min read

# CHAPTER 16

Constructors and Destructors

1. Introduction

When you create a variable like int x;, it contains garbage memory until you assign it a value. The same applies to Objects. To ensure an object is ready to be used the exact moment it is created, C++ uses Constructors. To clean up an object right before it is destroyed, C++ uses Destructors.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define a Default Constructor.
  • Use Parameterized Constructors for custom initialization.
  • Understand Constructor Overloading.
  • Explain the Copy Constructor.
  • Define a Destructor for memory cleanup.

3. The Constructor

A constructor is a special member function that is automatically called when an object is instantiated.
  • It MUST have the exact same name as the class.
  • It does NOT have a return type (not even void).

1. Default Constructor (Takes no arguments):

cpp
123456789101112131415161718192021
#include <iostream>
using namespace std;

class Player {
  public:
    int health;
    int score;

    // Default Constructor
    Player() {
        health = 100;
        score = 0;
        cout << "A new player has entered the game!" << endl;
    }
};

int main() {
    Player p1; // Constructor is called AUTOMATICALLY here!
    cout << "Health: " << p1.health << endl; // Prints 100
    return 0;
}

2. Parameterized Constructor: Allows you to pass arguments when creating the object to set custom values.

cpp
1234567891011121314151617
class Player {
  public:
    string name;
    int health;

    // Parameterized Constructor
    Player(string pName, int pHealth) {
        name = pName;
        health = pHealth;
    }
};

int main() {
    Player p1("Arthur", 150); // Calls parameterized constructor
    cout << p1.name << " has " << p1.health << " HP." << endl;
    return 0;
}

4. Constructor Overloading

Just like regular functions, constructors can be overloaded. You can have multiple constructors in a class, and C++ will call the correct one based on the arguments provided.
cpp
123456789101112131415161718192021
class Player {
  public:
    string name;
    int health;

    Player() { // Default
        name = "Unknown";
        health = 100;
    }

    Player(string pName) { // Overloaded
        name = pName;
        health = 100;
    }
};

int main() {
    Player p1;          // Calls Default
    Player p2("Lancelot"); // Calls Overloaded
    return 0;
}

5. The Copy Constructor

A copy constructor is used to create a new object as a copy of an existing object. C++ provides a default copy constructor, but you can define your own.
cpp
123456789101112131415161718
class Player {
  public:
    string name;

    Player(string pName) { name = pName; }

    // Copy Constructor
    Player(const Player &source) {
        name = source.name;
        cout << "Copy made!" << endl;
    }
};

int main() {
    Player p1("Arthur");
    Player p2 = p1; // Calls Copy Constructor! p2 is a clone of p1.
    return 0;
}

6. The Destructor

A destructor is a special function called automatically right before an object is destroyed (e.g., when it goes out of scope or is deleted from the Heap).
  • It has the same name as the class, preceded by a tilde (~).
  • No return type, and no parameters (meaning it cannot be overloaded).
  • Vital for freeing dynamically allocated memory!
cpp
12345678910111213141516171819202122232425
class GameLevel {
  private:
    int* enemyArray;

  public:
    // Constructor allocates memory
    GameLevel(int numEnemies) {
        enemyArray = new int[numEnemies];
        cout << "Level loaded." << endl;
    }

    // Destructor frees memory
    ~GameLevel() {
        delete[] enemyArray;
        cout << "Level destroyed, memory freed!" << endl;
    }
};

int main() {
    if (true) {
        GameLevel level1(5); // Constructor called
    } // level1 goes out of scope here. Destructor called AUTOMATICALLY!
    
    return 0;
}

7. Member Initializer Lists

A more efficient way to initialize class members, especially constants or references, is the Initializer List.
cpp
12345678910
class Player {
  public:
    int health;
    int mana;

    // Initializer list (faster than assignment inside the body)
    Player(int h, int m) : health(h), mana(m) {
        // Body can be empty
    }
};

8. Memory-Level Explanation

When Player p1; is executed, the OS allocates memory on the Stack for p1. Immediately after allocation, the CPU executes the Constructor's instructions to populate that memory. When main() ends, the CPU executes the Destructor's instructions *before* the OS reclaims the Stack memory.

9. Common Mistakes

  • Putting a return type on a constructor: void Player() is NOT a constructor; it's a regular function named Player.
  • Forgetting the Destructor when using new: If your class uses new to allocate heap memory, you MUST have a destructor that calls delete, otherwise every object you create will leak memory!

10. Exercises

  1. 1. Create a Car class with a constructor that takes make and year. Create an object and print its details.
  1. 2. Add a Destructor to the Car class that simply prints "Car destroyed" so you can observe when it runs.

11. MCQ Quiz with Answers

Question 1

What is the primary purpose of a Constructor?

Question 2

What is the return type of a Constructor?

Question 3

Which character precedes the name of a Destructor?

Q4. Can a Destructor be overloaded? a) Yes b) No Answer: b) No (Because it takes no parameters)
Question 5

When is a Destructor called?

Question 6

What does Player p2 = p1; invoke?

Question 7

If a class uses new to allocate memory for a pointer member, where must delete be called to prevent memory leaks?

Question 8

What happens if you don't write a constructor for your class?

Question 9

Which is the most efficient way to initialize members in a constructor?

Q10. Can you call a Constructor manually like a normal function (e.g., p1.Player();)? a) Yes b) No Answer: b) No (Constructors are called automatically upon object creation)

12. Interview Questions

  • Q: Explain the Rule of Three in C++. (Answer: If a class requires a custom Destructor, Copy Constructor, or Copy Assignment Operator, it almost certainly requires all three to manage dynamic memory correctly).
  • Q: What is the difference between shallow copy and deep copy? (Hint: Related to the Copy Constructor).
  • Q: Why use an initializer list instead of assigning values inside the constructor body?

13. Summary

Constructors guarantee that objects are initialized in a valid state the moment they are created. You can overload them to provide multiple ways to create an object. Destructors run automatically when an object dies, making them the perfect place to clean up dynamic memory and prevent leaks.

14. Next Chapter Recommendation

In Chapter 17: Inheritance in C++, we will learn how to create a hierarchy of classes, allowing new classes to absorb the properties and methods of existing classes.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·