English

Explain the Destructor with an example. - Computer Science 1

Advertisements
Advertisements

Question

Explain the Destructor with an example.

Explain
Advertisements

Solution

A destructor is a special member function that is automatically called when an object is destroyed or goes out of scope. Its primary role is to deallocate memory and release resources (like closing files) that were acquired by the object during its lifetime.

Example:

class Sample {
public:
    // Constructor
    Sample() { 
        cout << "Object created"; 
    }

    // Destructor
    ~Sample() { 
        cout << "Object destroyed"; 
    }
};
shaalaa.com
  Is there an error in this question or solution?
2023-2024 (July) Official Board Paper

APPEARS IN

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×