Question 8
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
struct Square : Shape {
double s = 2;
double area() const override { return s * s; }
};
Shape make_shape() { return Square{}; }
int main() {
std::cout << make_shape().area() << '\n';
return 0;
}
- A area must not be const
- B An abstract class cannot be a by-value return type; return std::unique_ptr<Shape> (or a reference to an existing object) instead Correct
- C Square must be final
- D s must be private
Correct Answer: Option B
Factories that hide the concrete type return smart pointers to the interface.
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!