ProgrammingVery hardGATE CS · TCS Digital · Advanced
What is printed? (C++17, assume <iostream> and using namespace std)
What is printed? (C++17, assume <iostream> and using namespace std)
struct B {
void f(int) { cout << "Bi"; }
void f(double) { cout << "Bd"; }
};
struct D : B {
using B::f;
void f(char) { cout << "Dc"; }
};
int main() {
D d;
d.f(65);
d.f(2.5f);
d.f('a');
}- A. BiBdBi
- B. BiDcDc
- C. BiBdDc
- D. DcDcDc
Correct answer: C. BiBdDc
The using-declaration puts B::f(int), B::f(double) and D::f(char) in one overload set. 65 is an exact int match; float to double is a promotion (better than the conversions to int or char); 'a' is an exact char match.
More OOP with C++ and Java questions
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct T { T() { cout << "T"; } ~T() { cout << "~"; } }; void g()…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct R { const char n; R(const char s) : n(s) { cout << "+" <<…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp template <class T> void f(T) { cout << "T"; } template <class T>…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct V { V() { cout << "V"; } }; struct A : virtual V { A() {…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct C { int v = 0; C& operator++() { ++v; return this; } C…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct T { T() { cout << "c"; } ~T() { cout << "d"; } }; int…