ProgrammingVery hardInfosys DSE · Wipro Elite
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct A { virtual int g(int x = 10) { return x; } };
struct B : A { int g(int x = 20) override { return x * 2; } };
int main() {
B b; A& r = b; B& s = b;
cout << r.g();
cout << " ";
cout << s.g();
}- A. 10 20
- B. 20 40
- C. 20 20
- D. 40 40
Correct answer: B. 20 40
r.g() runs B::g with A's static default 10 giving 20; s.g() uses B's default 20 giving 40.
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct V { V(int x) { cout << "V" << x; } V()…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { A() { cout << "A"; } }; struct B {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct I { int v; I& operator++() { ++v…
- What is printed? (also assume <typeinfo>) (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct G { const char n; G(const char s)…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void f() { cout << "A"; }…