在继承与派生中的类型问题
#include <iostream.h>#include <math.h>
double pi=3.14;
class shape
{
public:
float x,y,z;
double s;
shape(float x1,float y1,float z1)
{
x=x1;
y=y1;
z=z1;
}
};
class circle: public shape
{
public:
circle(float x2,float y2,float z2):shape(x2,y2,z2)
{}
double getarea()
{
s=pi*z*z;
return s;
}
};
class rectangle : public shape
{
public:
float p;
rectangle(float x2,float y2,float z2):shape(x2,y2,z2)
{ p=(x+y+z)/2;}
float getarea()
{
s=sqrt(p*(p-x)*(p-y)*(p-z));
return s;
}
};
/*class squre: public rectangle
{ public:
int i;
squre(float x3,float y3,float z3):rectangle(x3,y3,z3){i=9;}
};*/
void main()
{
circle c1(1.12,2.1,3.5);
rectangle t1(3,4,5);
cout<<t1.getarea()<<endl<<c1.getarea()<<endl;
}
为什么这样可以运行,但当去掉注释符号,老是说类型不对,这是怎么回事呢,高手解释下了!
[[it] 本帖最后由 whl20888 于 2008-12-13 22:36 编辑 [/it]]