R=r; }
float Area() {
return(float)(3.14159265*R*R); }
void SetData(float r,float) { R=r;
}
private: float R; };
class CArea { public: CArea(float triWidth,float
triHeight,float
rcHeight,float r) {
ppShape=new CShape*[3]; ppShape[0]=new CTriangle(triWidth,triHeight);
ppShape[1]=new CRect(rcWidth,rcHeight);
rcWidth,float
ppShape[2]=new CCircle(r);
} ~CArea() {
for(int i=0;i<3;i++)
delete ppShape[i];
delete []ppShape; }
void SetShapeData(int n,float f1,float f2=0) {
if((n>2)||(n<0))return;
ppShape[n]->SetData(f1,f2);
}
void CalAndPrint() {
float fSum=0.0;
char *str[3]={\三角\矩\圆\ for(int i=0;i<3;i++) {
float area=ppShape[i]->Area();
cout< } cout<<\总面积是:\ } private: CShape **ppShape; }; void main() { CArea a(10,20,6,8,6.5); a.CalAndPrint(); a.SetShapeData(0,20,30); a.CalAndPrint(); a.SetShapeData(2,11); a.CalAndPrint(); a.SetShapeData(1,2,5); a.CalAndPrint(); }