Exercise · Classes and OOP · lesson 12 of 13
Exercise: shapes with a virtual area
The abstract class Shape with the method virtual double area() const = 0 is already declared. Complete the classes Circle and Rect derived from it; add the fields yourself. Circle(double radius) is a circle of radius radius, and Rect(double w, double h) is a rectangle with sides w and h.
Also write the function double total_area(const std::vector<const Shape*>& shapes), which returns the sum of the areas of all the shapes. Take π from the constant kPi, which the file already defines.
Circle(1.0).area()→kPiRect(2.0, 3.5).area()→7.0total_area({&c, &r})forCircle c(1.0)andRect r(2.0, 3.0)→kPi + 6.0
Constraints: the sizes are non-negative; the same shape may appear in the vector several times; for an empty vector, total_area returns 0.