LearnFrenzy
Sign in Take a free test

OOPS Concepts · Find the Error · Question 9

Error: deriving from a final class Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

class Token final {
public:
    int id = 1;
};

class SpecialToken : public Token {};

int main() {
    SpecialToken t;
    std::cout << t.id << '\n';
    return 0;
}
  • A id must be private
  • B Token is declared final, so no class may inherit from it Correct
  • C SpecialToken needs a constructor
  • D final can only be used on functions
Correct Answer: Option B

final on a class prevents derivation; on a virtual function it prevents further overriding.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links