#include <cstdio>
#include <iostream>
#include <cstdlib>
class Circle//圆
{
public:
double r;
public:
void print()
{
cout<<"r="<<r<<endl;//输出半径
}
Circle(double x)//圆的构造函数
{
r=x;
}
double GetR()//获取圆的半径
{
return r;
}
};
class Sphere:public Circle//球类
{
public:
Sphere(double x):Circle(x)//球的构造函数
{
}
double GetTheSphereArea()//获取球的表面积
{
double R=GetR();
return (4*3.14*R*R);
}
double GetTheSphereVolume()//获取球的体积
{
double R=GetR();
return ((3.14*R*R*R)*3/4);
}
};
class Cylinder:public Circle//圆柱类
{
public:
double h;
Cylinder(double x ,double y):Circle(x)//圆柱类的构造函数
{
h=y;
}
double GetTheCylinderArea()//获圆柱类的表面积
{
double R=GetR();
return (2*3.14*R*R+2*3.14*R*h);
}
double GetTheCylinderVolume()//获圆柱类的体积
{
double R=GetR();
return (3.14*R*R*h);
}
};
void main()
{
Sphere Sphere1(2.5);
double a=Sphere1.GetTheSphereArea();
cout<<"表面积:"<<a<<endl;
a=Sphere1.GetTheSphereVolume();
cout<<"体积:"<<a<<endl;
}
【C++】用类的方法求圆柱等表面积和体积
发布于 2022-09-23 922 次阅读 预计阅读时间: 1 分钟
Comments NOTHING