Question 1
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#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!