阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。【说明】以下程序的功能是计算正方体、球体和圆柱体的表面积和体积并输出。程序由4个类组成:类cube、sphere和cylinder分别表示正方体、球体和圆柱体;抽象类 container为抽象类,提供了两个纯虚拟函数surface_area()和volum(),作为通用接口。【C++程序】include<iostream.h>define pi 3.1416class container{protected:double radius;public:container(double radius) {container::radius=radius;}virtual double surface_area()=0;virtual double velum()=0;};class cube:(1){ //定义正方体类public:cube(double radius):container(radius){};double surface_area () {return 6 * radius * radius;}double volum() {return radius * radius * radius;}};class sphere:(2){ //定义球体类public:sphere(double radius): container(radius){};double surface_area() { return (3);}double volum() {return pi * radius * radius * radius * 4/3;}};class cylinder:(4){ //定义圆柱体类double height;public:cylinder(double radius,double height):container(radius){container::height=height;}double surface_are a () { return 2 * pi * radius * (height+radius); }double volum () {return (5);}};void main(){container * p;cube obj1 (5);sphere obj2(5);cylinder obj3(5,5);p=obj1;cout<<“正方体表面积”(<<p->surface_area()<<end1;cont<<“正方体体积”<<p->volume()<<end1;p=obj2;cout<<“球体表面积”<<p->surface_area()<<end1;cout<<“球体体积”<<p->volume()<<end1;p=obj3;cout<<“球体表面积”<<p->surface_area()<<end1;cout<<“球体体积”<<p->volume()<<end1;}
阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。
【说明】
以下程序的功能是计算正方体、球体和圆柱体的表面积和体积并输出。
程序由4个类组成:类cube、sphere和cylinder分别表示正方体、球体和圆柱体;抽象类 container为抽象类,提供了两个纯虚拟函数surface_area()和volum(),作为通用接口。
【C++程序】
include<iostream.h>
define pi 3.1416
class container{
protected:
double radius;
public:
container(double radius) {container::radius=radius;}
virtual double surface_area()=0;
virtual double velum()=0;
};
class cube:(1){ //定义正方体类
public:
cube(double radius):container(radius){};
double surface_area () {return 6 * radius * radius;}
double volum() {return radius * radius * radius;}
};
class sphere:(2){ //定义球体类
public:
sphere(double radius): container(radius){};
double surface_area() { return (3);}
double volum() {return pi * radius * radius * radius * 4/3;}
};
class cylinder:(4){ //定义圆柱体类
double height;
public:
cylinder(double radius,double height):container(radius)
{
container::height=height;
}
double surface_are a () { return 2 * pi * radius * (height+radius); }
double volum () {return (5);}
};
void main()
{
container * p;
cube obj1 (5);
sphere obj2(5);
cylinder obj3(5,5);
p=&obj1;
cout<<“正方体表面积”(<<p->surface_area()<<end1;
cont<<“正方体体积”<<p->volume()<<end1;
p=&obj2;
cout<<“球体表面积”<<p->surface_area()<<end1;
cout<<“球体体积”<<p->volume()<<end1;
p=&obj3;
cout<<“球体表面积”<<p->surface_area()<<end1;
cout<<“球体体积”<<p->volume()<<end1;
}