Model circles, squares and triangles as a std::variant (no base class), and write two visitors – one for area and one for a description – then process a list.
circle r=1 3.14
square s=3 9.00
triangle 4x5 10.00
total 22.14
The overloaded helper merges lambdas into one visitor. Adding a new operation is just another visitor; forgetting a shape in any visitor is a compile error – the strength of std::variant for closed sets of types.
More Polymorphism and Virtual Functions practice questions
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
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!