#include<bits/stdc++.h>
using namespace std;
class Shape{
public:
virtual double Area();
protected:
double area;
};
class Circle:public Shape{
public:
Circle(int a);
double Area();
protected:
int radius;
double PI=3.14159;
};
class Rectangle:public Shape{
public:
Rectangle(int width1,int height1);
double Area();
protected:
int width;
int height;
};
double Shape::Area(){return area;}
Circle::Circle(int a){
radius=a;
}
double Circle::Area(){
area=PI*radius*radius;
}
Rectangle::Rectangle(int width1,int height1){
width=width1;
height=height1;
}
double Rectangle::Area(){
area=width*height;
}
int main(){
Shape *p[4];
p[0]=new Circle(1);
p[1]=new Circle(2);
p[2]=new Rectangle(1,2);
p[3]=new Rectangle(3,4);
for(int i=0;i++;i<4){
cout<<p[i]->Area()<<endl;
}
delete p;
}
样例输出
3.14
12.56
2
12