A destructor is a special member function that is called automatically when an object goes out of scope or is deleted. Its main purpose is to release resources and perform cleanup tasks like freeing memory or closing files.
Click to reveal answer
beginner
How do you declare a destructor in a C++ class?
A destructor is declared with a tilde (~) followed by the class name, and it has no return type or parameters. For example: <br> <code>~ClassName() { /* cleanup code */ }</code>
Click to reveal answer
beginner
When exactly is a destructor called?
A destructor is called automatically when an object is destroyed, which happens when: 1. A local object goes out of scope. 2. A dynamically allocated object is deleted. 3. A program ends and global/static objects are cleaned up.
Click to reveal answer
intermediate
Can you call a destructor explicitly in C++? Should you?
You can call a destructor explicitly, but it is not recommended because it can cause undefined behavior if the object is destroyed again automatically. It's best to let C++ handle destructor calls.
Click to reveal answer
intermediate
Why is it important to define a destructor when your class manages dynamic memory?
If your class allocates memory dynamically (e.g., with new), you must define a destructor to free that memory (e.g., with delete). Otherwise, your program will leak memory, which wastes resources and can cause crashes.
Click to reveal answer
What symbol is used to declare a destructor in C++?
A# (hash)
B* (asterisk)
C& (ampersand)
D~ (tilde)
✗ Incorrect
A destructor is declared with a tilde (~) followed by the class name.
When is a destructor automatically called?
AWhen a variable is declared
BWhen a function is called
CWhen an object goes out of scope or is deleted
DWhen a program starts
✗ Incorrect
Destructors run automatically when an object is destroyed, such as going out of scope or being deleted.
What happens if you forget to define a destructor for a class that uses dynamic memory?
AMemory leaks can occur
BThe program runs faster
CThe compiler adds a destructor automatically that frees memory
DNothing happens
✗ Incorrect
Without a destructor freeing dynamic memory, the program leaks memory, wasting resources.
Can a destructor have parameters or return a value?
ADestructors can have parameters but no return type
BNo, destructors have no parameters and no return type
CYes, destructors return an int
DYes, destructors can have parameters
✗ Incorrect
Destructors never have parameters or return values.
Is it good practice to call a destructor explicitly?
ANo, it can cause problems and should be avoided
BYes, always call destructors explicitly
COnly if the destructor is public
DOnly for static objects
✗ Incorrect
Explicit destructor calls can cause undefined behavior; let C++ handle it automatically.
Explain the role of a destructor in a C++ class and when it is called.
Think about what happens when an object is no longer needed.
You got /4 concepts.
Why must you define a destructor if your class uses dynamic memory allocation?
Consider what happens if memory is not freed.
You got /3 concepts.
Practice
(1/5)
1. What is the main role of a destructor in C++?
easy
A. To clean up resources when an object is destroyed
B. To create new objects
C. To initialize variables
D. To overload operators
Solution
Step 1: Understand the purpose of a destructor
A destructor is a special function that runs when an object is destroyed to free resources like memory or files.
Step 2: Compare with other options
Creating objects, initializing variables, and overloading operators are not roles of destructors.
Final Answer:
To clean up resources when an object is destroyed -> Option A
Quick Check:
Destructor role = cleanup [OK]
Hint: Destructor always cleans up when object ends [OK]
Common Mistakes:
Confusing destructor with constructor
Thinking destructor creates objects
Believing destructor initializes variables
2. Which of the following is the correct syntax for a destructor in a class named Car?
easy
A. Car~() {}
B. void ~Car() {}
C. ~Car() {}
D. void Car() {}
Solution
Step 1: Recall destructor syntax
A destructor has the same name as the class with a tilde (~) prefix and no return type.
Step 2: Check each option
~Car() {} matches the correct syntax. void ~Car() {} wrongly includes 'void'. Car~() {} has wrong order. void Car() {} lacks tilde and looks like a constructor.
Final Answer:
~Car() {} -> Option C
Quick Check:
Destructor syntax = ~ClassName() [OK]
Hint: Destructor name = ~ + class name, no return type [OK]
At end of main(), b is destroyed, destructor prints "Destroyed".
Final Answer:
Created\nDestroyed\n -> Option B
Quick Check:
Constructor then destructor output [OK]
Hint: Constructor runs first, destructor runs last [OK]
Common Mistakes:
Assuming destructor runs before constructor
Expecting no output
Confusing order of prints
4. Identify the error in this destructor code:
class Sample {
public:
~Sample(int x) { }
};
medium
A. Destructor cannot have parameters
B. Destructor must return int
C. Destructor name should not have ~
D. Destructor must be static
Solution
Step 1: Check destructor parameters
Destructors cannot have parameters; they must have empty parentheses.
Step 2: Verify other rules
Destructor has no return type, must have ~ prefix, and cannot be static.
Final Answer:
Destructor cannot have parameters -> Option A
Quick Check:
Destructor params = none [OK]
Hint: Destructor always has empty parentheses [OK]
Common Mistakes:
Adding parameters to destructor
Adding return type
Removing ~ from destructor name
5. Consider a class FileHandler that opens a file in its constructor. Which is the best way to ensure the file is closed properly when the object is no longer needed?
hard
A. Do nothing; files close automatically when program ends
B. Manually call a close method before deleting the object
C. Rely on the operating system to close the file automatically
D. Write a destructor that closes the file
Solution
Step 1: Understand resource management
Files opened by an object should be closed when the object is destroyed to avoid resource leaks.
Step 2: Use destructor for cleanup
Destructor runs automatically when object ends, so closing the file there ensures proper cleanup.
Final Answer:
Write a destructor that closes the file -> Option D
Quick Check:
Destructor cleans resources automatically [OK]
Hint: Use destructor to free resources automatically [OK]
Common Mistakes:
Forgetting to close files causing leaks
Relying on manual close calls
Assuming OS always cleans up immediately
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.