Recall & Review
beginner
What are the main stages of an object's lifecycle in C++?
The main stages are: creation (when the object is made), usage (when the object is used), and destruction (when the object is removed and its memory freed).
Click to reveal answer
beginner
What happens during the construction of an object?
During construction, the object's memory is allocated and its constructor function runs to set up initial values or states.
Click to reveal answer
beginner
What is the role of a destructor in C++?
A destructor is a special function that runs automatically when an object is destroyed. It cleans up resources like memory or files the object used.
Click to reveal answer
intermediate
How does scope affect an object's lifecycle?
An object created inside a scope (like inside a function) is destroyed automatically when the scope ends, meaning its destructor is called then.
Click to reveal answer
intermediate
What is the difference between automatic and dynamic object lifecycles?
Automatic objects are created and destroyed automatically based on scope. Dynamic objects are created with
new and must be manually destroyed with delete.Click to reveal answer
When is an object's destructor called in C++?
✗ Incorrect
The destructor runs when the object is no longer needed, either when it goes out of scope or when deleted if created dynamically.
Which keyword is used to create a dynamic object in C++?
✗ Incorrect
The
new keyword allocates memory and calls the constructor for dynamic objects.What happens if you forget to delete a dynamic object?
✗ Incorrect
Not deleting dynamic objects causes memory to stay allocated, leading to memory leaks.
Which function is automatically called when an object is created?
✗ Incorrect
The constructor sets up the object when it is created.
If an object is declared inside a function, when is it destroyed?
✗ Incorrect
Objects declared inside a function are destroyed when the function finishes and the scope ends.
Explain the lifecycle of an object in C++ from creation to destruction.
Think about what happens when you make an object and when it disappears.
You got /6 concepts.
Describe the difference between automatic and dynamic object lifecycles in C++.
Consider how and when objects are created and destroyed.
You got /5 concepts.