ProgrammingHardHCL · Tech Mahindra pattern

What happens when this is compiled?

What happens when this is compiled?
struct Base final {
    virtual void f() { cout << "Base"; }
};
struct Derived : Base {
};
  1. A. Compiles; Derived simply cannot override f()
  2. B. Compile-time error: Base is declared final, so no class may derive from it
  3. C. Compiles, but Derived has no constructor
  4. D. Compile-time error: a final class cannot have virtual functions

Correct answer: B. Compile-time error: Base is declared final, so no class may derive from it

Applying final to a class (as opposed to a member function) forbids any class from inheriting from it at all. struct Derived : Base is rejected at compile time regardless of what Derived would do with f().

More OOP with C++ and Java questions

Exams that ask this topic