帮忙看看这样做能算合格吗?
题目要求:使用纯虚函数来完成.
定义一个共用接口,计算正方体,长方体,球体的体积和表面积
提示:
class shape //创建一个图形类,作为父类
{
public:
virtual double tj()=0; //方法1:计算体积
virtual double bmj()=0; //方法2:计算表面积
};
本人答案:
#include<iostream.h>
#define Pi 3.1415926
class shape
{
public:
double x;
double y;
double z;
double getx(double a)
{
x=a;
return x;
}
double gety(double a,double b)
{
x=a;y=b;
return x,y;
}
double getz(double a,double b,double c)
{
x=a;y=b;z=c;
return x,y,z;
}
virtual double tj()=0;
virtual double bmj()=0;
};
class zft:public shape
{
public:
double tj()
{
cout<<"正方体体积:"<<x*x*x<<endl;
return x*x*x;
}
double bmj()
{
cout<<"正方体表面积:"<<6*x*x<<endl;
return 6*x*x;
}
};
class cft:public shape
{
public:
double tj()
{
cout<<"长方体体积:"<<x*y*z<<endl;
return x*y*z;
}
double bmj()
{
cout<<"长方体表面积:"<<2*(x*y+y*z+z*x)<<endl;
return 2*(x*y+y*z+z*x);
}
};
class qt:public shape
{
public:
double tj()
{
cout<<"球体体积:"<<(4.0/3)*Pi*x*y*z<<endl;
return (4.0/3)*Pi*x*y*z;
}
double bmj()
{
cout<<"球体表面积:"<<4*Pi*x*y<<endl;
return 4*Pi*x*y;
}
};
void main()
{
shape *p;
zft obj;
p=&obj;
p->getx(3);
p->tj();
p->bmj();
cft obj2;
p=&obj2;
p->getz(3,4,5);
p->tj();
p->bmj();
qt obj3;
p=&obj3;
p->getz(6,6,6);
p->tj();
p->gety(6,6);
p->bmj();
}
[[it] 本帖最后由 happycplusplus 于 2008-10-10 19:05 编辑 [/it]]