ProgrammingVery hardGATE CS · Accenture
What is printed? (C++, assume <iostream> and using namespace std)
What is printed? (C++, assume <iostream> and using namespace std)
struct A { A() { cout << "A"; } };
struct B : virtual A { };
struct C : virtual A { };
struct D : B, C { };
int main() { D d; }- A. AA
- B. A
- C. AAA
- D. Nothing
Correct answer: B. A
With virtual inheritance the most derived class constructs one shared A sub-object, so A's constructor runs once.
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void f(int x = 1) { cout <<…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void p() { cout << "A"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct X { string n; X(const char s) : n(s) {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { A() { f(); } virtual void f() {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual ~A() { f(); } virtual void…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct T { T() { cout << "d"; } T(const T&) {…