ProgrammingHardProduct-company placement · GATE CS

Which lines fail to compile?

Which lines fail to compile?
struct B { void f() { } };
class  D1 : B { };
struct D2 : B { };
class  D3 : public B { };
int main() {
    D1 a; D2 b; D3 c;
    a.f();       // L1
    b.f();       // L2
    c.f();       // L3
    B* p = &a;   // L4
}
  1. A. L1 only
  2. B. L1, L2 and L4
  3. C. None of them
  4. D. L1 and L4

Correct answer: D. L1 and L4

Inheritance defaults to private for class and public for struct. D1 inherits B privately, so outside code can neither call a.f() nor convert D1* to the inaccessible base B*. D2 and D3 inherit publicly.

More OOP with C++ and Java questions

Exams that ask this topic