你的“上述程序”在哪里呢?
下面的是我写的,你参考一下吧:
#include
#include
using namespace std;
const double PI=3.1415926;
class Shape
{
public:
virtual float GetAea()=0;
virtual float Getperim()=0;
};
class Rectangle : public Shape
{
public:
Rectangle(float a=0, float b=0):lenth(a),width(b){}
float GetAea()
{
return lenth*width;
}
float Getperim()
{
return (lenth+width)*2;
}
private:
float lenth;
float width;
};
class Circle : public Shape
{
public:
Circle(float a=0):r(a){}
float GetAea()
{
return PI*r*r;
}
float Getperim()
{
return 2*PI*r;
}
private:
float r;
};
int main()
{
Rectangle R(3, 4);
Circle C(5);
cout << "矩形R的面积和周长分别为:" << R.GetAea() << " " << R.Getperim() << endl;
cout << "圆形C的面积和周长分别为:" << C.GetAea() << " " << C.Getperim() << endl;
return 0;
}