What is the purpose of a destructor in OOP?

2025

What is the purpose of a destructor in OOP?

  1. A.

    To create instances of classes

  2. B.

    To handle exceptions

  3. C.

    To free up resources when an object is destroyed

  4. D.

    To define default values for properties

Attempted by 90 students.

Show answer & explanation

Correct answer: C

In Object-Oriented Programming (OOP), a destructor is a special member function that is automatically called when an object is destroyed. Its main purpose is to release resources such as memory, file handles, or network connections used by the object.

Example in C++:

class Test {
public:
    ~Test() {
        // cleanup code
    }
};

Explore the full course: Bpsc