5. Inheritance and class hierarchy

5. Inheritance and class hierarchy#

警告

忘了原题, 这个难度和考点类似而要写的代码更多.

建议直接做 计软智 2022 年试题 最后 1 题, 考点和做法是一样的.

Implement the Shape hierarchy designed in the picture below.

  • Each TwoDimensionalShape should contain a member function get_area to calculate the area of the 2D shape.

  • Each ThreeDimensionalShape should have member functions get_area and get_volume to calculate the surface area and volume of the 3D shape, respectively.

Create a program that uses a vector of Shape pointers to objects of each concrete class in the hierarchy. The program should print the object to which each vector element points.

  • If a shape is a TwoDimensionalShape, display its area.

  • If a shape is a ThreeDimensionalShape, display its area and volume.

../../_images/class_hierarchy.svg

Class Hierarchy#