// 文件名: main.cpp
#include <iostream>
#include <string> // 预处理命令
using namespace std; // 使用标准命名空间std
const double PI = 3.1415926; // 常量PI
// 形状类
class Shape
{
public:
virtual void ShowArea() const = 0;
static double sum;
};
class Circle: public Shape
{
private:
double radius;
public:
Circle(double r): radius(r) // 构造函数
{ sum += PI * radius * radius; }
virtual void ShowArea() const // 显示圆形相关信息
{
cout << "圆形:" << endl;
cout << "半径:" << radius << endl; // 显示半径
cout << "面积:" << PI * radius * radius << endl; // 显示面积
}
};
class Rectangle: public Shape
{
private:
double height; // 高
double width; // 宽
public:
Rectangle(double h, double w): height(h), width(w) // 构造函数
{ sum += height * width; }
virtual void ShowArea() const // 显示矩形相关信息
{
cout << "矩形:" << endl;
cout << "高:" << height << endl; // 显示高
cout << "宽:" << width << endl; // 显示宽
cout << "面积:" << height * width << endl; // 显示面积
}
};
class Square: public Shape
{
private:
double length;
public:
Square(double l):length(l)
{sum+=length*length;}
virtual void ShowArea() const
{
cout << "正方形:" << endl;
cout << "长度:" << length << endl;
cout << "面积:" << length * length << endl;
}
};
double Shape::sum = 0;
int main()
{
Circle c1(3.0);
Rectangle r1(4.2,5.5);
Square s1(1.2);
Shape *arr[3]={&c1,&r1,&s1};
for(int i=0;i<3;i++)
{
cout<<arr[i]->ShowArea()<<endl;
}
delete arr;
return 0;
}
运行有错 求指教
运行没错了 结果一闪而过.....是不是程序还是有问题啊?
追答按Ctrl+F5运行!
本回答被提问者采纳