CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #60

Members Are Built Before the Body and Destroyed After

hardClasses

📄 Code

Read carefully — what does this print?
1#include <iostream>
2using namespace std;
3 
4class Engine {
5public:
6 Engine() { cout << "Engine" << endl; }
7 ~Engine() { cout << "~Engine" << endl; }
8};
9 
10class Wheel {
11public:
12 Wheel() { cout << "Wheel" << endl; }
13 ~Wheel() { cout << "~Wheel" << endl; }
14};
15 
16class Car {
17 Wheel m_wheel;
18 Engine m_engine;
19public:
20 Car() : m_engine(), m_wheel() { cout << "Car" << endl; }
21 ~Car() { cout << "~Car" << endl; }
22};
23 
24int main() {
25 Car c;
26 cout << "driving" << endl;
27 return 0;
28}

🎯 Your Answer