📄 c12_2.cpp
字号:
#include<iostream.h>
class CContainer
{
protected:
float r;
public:
CContainer(float r1)
{
r=r1;
}
virtual float volume()=0; //纯虚函数
virtual float sarea()=0;
};
class Cylinder:public CContainer
{
protected:
float h;
public:
Cylinder(float r1,float h1):CContainer(r1)
{
h=h1;
}
float volume()
{
return 3.1416*r*r*h;
}
float sarea()
{
return 2*3.1416*r*(h+r);
}
};
class Global:public CContainer
{
public:
Global(float r1):CContainer(r1)
{}
float volume()
{
return 4.0/3*3.1416*r*r*r;
}
float sarea()
{
return 4*3.1416*r*r;
}
};
class Square:public CContainer
{
public:
Square (float a):CContainer(a){}
float volume()
{
return r*r*r;
}
float sarea()
{
return 6*r*r;
}
};
void main()
{
CContainer *ct;
ct=new Cylinder (1.0,2.0);
cout<<"圆柱:表面积 "<<ct->sarea()<<" 体积 "<<ct->volume()<<endl;
ct=new Global (1.0);
cout<<"球:表面积 "<<ct->sarea()<<" 体积 "<<ct->volume()<<endl;
ct=new Square (1.0);
cout<<"正方体:表面积 "<<ct->sarea()<<" 体积 "<<ct->volume()<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -