๐ Scenario: Imagine you have a simple program that manages a book in a library. When the book object is no longer needed, it should clean up by announcing it is being removed.
๐ฏ Goal: You will create a class Book with a destructor that prints a message when the object is destroyed. This shows how destructors help clean up or notify when an object is removed.
๐ What You'll Learn
Create a class named Book
Add a constructor that sets the book's title
Add a destructor that prints "Destroying book: <title>"
Create a Book object with the title "C++ Basics"
Allow the destructor message to show when the object goes out of scope
๐ก Why This Matters
๐ Real World
Destructors help manage resources like memory or files by cleaning up automatically when objects are no longer needed.
๐ผ Career
Understanding destructors is important for writing safe and efficient C++ programs, especially in systems programming and software development.
Progress0 / 4 steps
1
Create the Book class with a title
Write a class named Book with a public string variable title. Add a constructor that takes a string parameter bookTitle and sets title to it.
C++
Hint
Use class Book and add a constructor that sets the title variable.
2
Add a destructor to the Book class
Inside the Book class, add a destructor ~Book() that prints "Destroying book: " followed by the title and a newline.
C++
Hint
Destructor has the same name as the class but with a tilde ~ before it.
3
Create a Book object in main
Write a main function that creates a Book object named myBook with the title "C++ Basics".
C++
Hint
Create the myBook object inside main() using the constructor.
4
Run the program to see the destructor message
Add a return 0; statement at the end of main(). Run the program and observe the output showing the destructor message.
C++
Hint
The destructor message appears when myBook goes out of scope at the end of main().
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.