#include #include #include #include using namespace std; class Figure { public: Figure (string const& name) : name{name} {} virtual ~Figure() = default; virtual double calc_volume () = 0; virtual string to_string () { ostringstream oss{}; oss << "Figure " << name << "s volume is " << calc_volume() << "\n"; //oss << "Figure " << name; return oss.str(); } private: string name; }; class Pyramid : public Figure { public: Pyramid (string const& name, int height, int width, int breadth) : Figure{name}, height{height}, width{width}, breadth{breadth} {} double calc_volume () override { return (width * breadth * height) / 3.0; } string to_string () override { ostringstream oss{}; oss << Figure::to_string() //<< "s volume is " << calc_volume() << "\n" << "The dimensions are:\n" << "height = " << height << "\n" << "width = " << width << "\n" << "breadth = " << breadth << "\n"; return oss.str(); } private: int height; int width; int breadth; }; class Sphere : public Figure { public: Sphere (string const& name, int radius) : Figure{name}, radius{radius} {} double calc_volume () override { return (4 * radius * radius * radius) / 3.0; } string to_string () override { ostringstream oss{}; oss << Figure::to_string() //<< "s volume is " << calc_volume() << "\n" << "The dimensions are: \n" << "radius = " << radius << "\n"; return oss.str(); } private: int radius; }; int main () { vector figures{}; figures.push_back(new Pyramid{"Pyramid", 4, 4, 4}); figures.push_back(new Sphere{"Sphere", 4}); for (Figure* figure : figures) { cout << figure -> to_string() << endl; } for (Figure* figure : figures) { delete figure; } }