Recall & Review
beginner
What is an object in C++?
An object is a specific instance of a class that holds data and can perform actions defined by the class.Click to reveal answer
beginner
How do you create an object of a class named <code>Car</code>?You create an object by declaring it with the class name, like: <code>Car myCar;</code>Click to reveal answer
intermediate
What is the difference between creating an object on the stack vs the heap in C++?
Stack objects are created with simple declarations and automatically destroyed when out of scope. Heap objects are created with
new and must be manually deleted.Click to reveal answer
beginner
What does the
new keyword do when creating an object?new allocates memory on the heap and returns a pointer to the newly created object.Click to reveal answer
beginner
How do you delete an object created with
new?You use the
delete keyword followed by the pointer to free the memory, like: delete myObject;Click to reveal answer
How do you create an object named
dog of class Animal on the stack?✗ Incorrect
Declaring
Animal dog; creates an object on the stack.Which keyword is used to create an object on the heap?
✗ Incorrect
The
new keyword allocates memory on the heap for an object.What must you do after creating an object with
new to avoid memory leaks?✗ Incorrect
You must use
delete to free memory allocated by new.What type of variable holds the address of an object created with
new?✗ Incorrect
Objects created with
new return a pointer to the object.Which of the following creates an object on the heap?
✗ Incorrect
Car* myCar = new Car(); creates an object on the heap and stores its address in a pointer.Explain how to create an object of a class in C++ both on the stack and on the heap.
Think about simple declaration vs using new keyword.
You got /3 concepts.
Describe why it is important to use delete with objects created using new.
Consider what happens if memory is not freed.
You got /3 concepts.