ProgrammingHardProduct-company placement · C++

Which lines fail to compile?

Which lines fail to compile?
struct B { virtual void f(int) { } virtual void g() const { } void h() { } };
struct D : B {
    void f(int) override { }   // L1
    void g() override { }      // L2
    void h() override { }      // L3
};
  1. A. L2 and L3
  2. B. L3 only
  3. C. L2 only
  4. D. L1, L2 and L3

Correct answer: A. L2 and L3

override requires that a virtual function with the same signature exists in a base. g() without const has a different signature from B::g() const, and h is not virtual, so both are errors.

More OOP with C++ and Java questions

Exams that ask this topic