ProgrammingVery hardTCS NQT Prime · Infosys SP
What is the result? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
What is the result? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct A { void f(int) { cout << "Ai"; } void f(double) { cout << "Ad"; } };
struct B : A { void f(char*) { cout << "Bs"; } };
int main() { B b; b.f(1.5); }- A. Compile error
- B. Ad
- C. Ai
- D. Bs
Correct answer: A. Compile error
B::f hides every A::f by name, so only f(char*) is visible and a double cannot convert to char*: compile error (a using-declaration would be needed).
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual int g(int x = 10) { return…
- 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)…