๐ Scenario: Imagine you are managing a small library system. Each book is represented as an object. You want to see when a book is created and when it is removed from the system.
๐ฏ Goal: You will create a simple C++ class Book that prints messages when a book object is created and destroyed. This will help you understand the flow of object creation and destruction.
๐ What You'll Learn
Create a class named Book with a constructor and destructor
Constructor should print "Book created"
Destructor should print "Book destroyed"
Create a main function that creates a Book object
Observe the order of printed messages to understand object lifecycle
๐ก Why This Matters
๐ Real World
Understanding object creation and destruction is important when managing resources like files, memory, or network connections in real applications.
๐ผ Career
Many software jobs require knowledge of how objects are created and destroyed to write efficient and bug-free code, especially in C++.
Progress0 / 4 steps
1
Create the Book class with constructor
Write a class named Book with a public constructor that prints "Book created" when called.
C++
Hint
Use std::cout inside the constructor to print the message.
2
Add destructor to Book class
Add a public destructor to the Book class that prints "Book destroyed" when called.
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.
C++
Hint
Inside main, declare Book myBook; to create the object.
4
Run and observe output
Run the program and print the output to see the messages from the constructor and destructor.
C++
Hint
When the program ends, the destructor is called automatically for myBook.
Practice
(1/5)
1. Which of the following statements about object creation and destruction in C++ is correct?
easy
A. Constructors run only when an object is destroyed.
B. Destructors are called before an object is created.
C. Constructors are called automatically when an object is created.
D. Destructors run only when an object is created.
Solution
Step 1: Understand constructor timing
Constructors are special functions that run automatically when an object is created to initialize it.
Step 2: Understand destructor timing
Destructors run when an object is destroyed or goes out of scope, not before creation.
Final Answer:
Constructors are called automatically when an object is created. -> Option C
A. Destructor has a return type which is not allowed.
B. Destructor name is incorrect; it should be 'destruct'.
C. Constructor is missing a return type.
D. Destructor should be public static.
Solution
Step 1: Check destructor declaration
Destructor must not have a return type. Here, 'void' before '~Sample()' is invalid syntax.
Step 2: Verify other options
Destructor name is correct with '~Sample'. Constructors have no return type but that is correct here. Destructors cannot be static.
Final Answer:
Destructor has a return type which is not allowed. -> Option A
Quick Check:
Destructor = no return type [OK]
Hint: Destructor never has a return type, not even void [OK]
Common Mistakes:
Adding void or other return types to destructor
Misnaming destructor
Making destructor static
5. You have a class Resource that opens a file in its constructor and closes it in its destructor. Which of the following code snippets correctly ensures the file is closed automatically when the object goes out of scope?
hard
A. void useFile() {
Resource* r = new Resource();
// use file
delete r;
} // file closed here automatically
B. void useFile() {
Resource* r = new Resource();
// use file
} // file closed here automatically
C. void useFile() {
Resource r;
// use file
delete &r;
} // file closed here
D. void useFile() {
Resource r;
// use file
} // file closed here automatically
Solution
Step 1: Understand automatic destruction
Objects created on stack (like 'Resource r;') are destroyed automatically when going out of scope, calling destructor to close file.
Step 2: Analyze dynamic allocation
Objects created with 'new' must be deleted manually; otherwise destructor won't run automatically, risking resource leaks.
Final Answer:
void useFile() {
Resource r;
// use file
} // file closed here automatically -> Option D
Quick Check:
Stack objects auto-destroyed, heap objects need delete [OK]
Hint: Use stack objects for automatic destructor calls [OK]
Common Mistakes:
Forgetting to delete heap objects
Trying to delete stack objects
Assuming destructor runs without delete on heap
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.