← Quiz: C++ Classes and Objects: all 10 questions
Quiz: C++ Classes and Objects · Question 6 of 10
What does this print? (code)
What does this print?
#include <iostream>
using namespace std;
struct T {
int id;
T(int i) : id(i) { cout << "C" << id; }
~T() { cout << "D" << id; }
};
int main() {
T a(1);
T b(2);
return 0;
}
- A.C1C2D1D2
- B.C1C2D2D1
- C.C1D1C2D2
- D.C2C1D1D2
Answer & explanation
Correct answer: C1C2D2D1
Local objects are destroyed in reverse order of construction, so b is destroyed before a.
Can you get the rest right without peeking?
Take the timed Quiz: C++ Classes and Objects quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →