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

Three Levels of Construction and Destruction

mediumInheritance

📄 Code

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

🎯 Your Answer