LearnFrenzy
Sign in Take a free test

Polymorphism and Virtual Functions · Find the Error · Question 1

Error: override with a different parameter type Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

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

main.cpp
#include <iostream>

struct Discount {
    virtual double apply(double price) const { return price; }
    virtual ~Discount() = default;
};

struct Festival : Discount {
    double apply(float price) const override { return price * 0.8; }
};

int main() {
    Festival f;
    std::cout << f.apply(100) << '\n';
    return 0;
}
  • A 0.8 must be a float
  • B apply(float) has a different parameter type from apply(double), so it overrides nothing – and override makes that a compile error Correct
  • C Festival must be final
  • D apply cannot be const
Correct Answer: Option B

Without override this would compile and silently hide the base function. Use the exact signature: double apply(double price) const override.

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