在上一个练习中,您使用了 C++ 可视开发工具来查看 C++ Shapes 项目的层次结构。还可以使用 C++ 可视开发工具对项目添加类或者对类添加属性和方法。在本练习中,将对 circle 和 sphere 类添加 getCircumference 方法。getCircumference 方法根据半径来计算形状和圆的周长。
对 circle 类添加 getCircumference 方法
圆面积的计算公式是 πr²,其中 r 是圆的半径。全局常量 π 存储在基类 shape 中。
要对 circle 类添加 getCircumference 方法:
要对 sphere 类添加 getCircumference 方法:
Enter the radius of the sphere: 10 Enter the color of the sphere: Blue Sphere: Radius = 10 Circumference = 62.8319 Area = 1256.64 Volume = 4188.79 Color = Blue
此程序将显示当前形状的大小和颜色并提示您指定新形状的值。新形状的属性将显示在“控制台”视图中。您可以修改 main.cpp 中的代码以运行 circle 类。