ProgrammingModerateMicrosoft/Amazon SDE MCQ · GATE CS

What is printed?

What is printed?
struct B { int x = 5; };
struct D : B { int x = 7; };
int main() {
    D d;
    B& b = d;
    cout << d.x << b.x << d.B::x;
}
  1. A. 777
  2. B. 757
  3. C. 755
  4. D. 775

Correct answer: C. 755

Data members are never virtual. D::x hides B::x for expressions of type D (7), while access through B& uses B's member (5), as does the qualified d.B::x (5).

More OOP with C++ and Java questions

Exams that ask this topic