[求助]关于派生
定义一个Shape抽象类,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积,GetPerim()函数计算对象的周长。
#include<iostream.h>
#include<math.h>
class shape
{ public:
virtual void getarea()=0;
virtual void getperim()=0;
};
class rectangle:public shape
{ int a,b,c;
double s,p;
public:
rectangle(int a1,int b1,int c1) {a=a1;b=b1;c=c1;}
void getperim()
{ p=a+b+c; cout<<"周长 "<<p<<endl;}
void getarea()
{ p=(a+b+c)/2.0;
s=sqrt(p*(p-a)*(p-b)*(p-c));
cout<<"面积 "<<s<<endl;
}
};
class circle :public shape
{ float r,s,p;
public:
circle(float r1) {r=r1;}
void getperim() { p=2*r*3.1415926;cout<<"周长 "<<p<<endl;}
void getarea() { s=r*r*3.1415926; cout<<"面积 "<<s<<endl;}
};
void show(shape *p)
{ p->getarea();
p->getperim();
}
void main()
{ shape *p;
rectangle a(3,4,5);
circle b(10);
p=&a; show(p);
p=&b; show(p);
}
我做的对么???