Quiz Entry - updated: 2026.07.06
How do you create an object on the stack in C++?
Just declare it with constructor arguments: Vehicle v(4); — it lives on the stack and is destroyed automatically when it goes out of scope.
int main() {
Vehicle v(4); // create a Vehicle with 4 wheels
v.show(); // call a method with dot notation
return 0;
}
// v is automatically destroyed here, at end of scope
Output:
Vehicle has 4 wheels.
Key points:
- No
newkeyword needed - The object is allocated on the stack
- It's destroyed automatically at the end of its scope (this automatic cleanup is the heart of RAII)
- Use
.to access its members
Go deeper:
Stack-based memory allocation — Wikipedia — how automatic (stack) storage grows and unwinds.