0
0
C++programming~5 mins

Creating objects in C++ - Quick Revision & Summary

Choose your learning style9 modes available
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?
Anew Animal dog;
BAnimal* dog = new Animal();
CAnimal dog;
DAnimal dog = new Animal();
Which keyword is used to create an object on the heap?
Anew
Balloc
Ccreate
Dmalloc
What must you do after creating an object with new to avoid memory leaks?
AUse <code>free()</code> function
BCall the destructor manually
CNothing, it cleans up automatically
DUse <code>delete</code> to free memory
What type of variable holds the address of an object created with new?
APointer
BReference
CInteger
DArray
Which of the following creates an object on the heap?
ACar myCar;
BCar* myCar = new Car();
CCar myCar = Car();
DCar myCar = &Car();
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.