#include<iostream>
#include<cmath>
using namespace std;
#define PI 3.14
class Base{
protected:
double r;
public:
Base(double a=1)
{
r=a;
}
};
class Sphere:public Base{
public:
Sphere(double a=1):Base(a){}
double getSpheres()
{
return 4*PI*r*r;
}
double showSpheres()
{
cout<<"The area of the sphere:"<<getSpheres();
}
};
int main()
{
double a1;
cout<<"Input the radius of the sphere:";
cin>>a1;
Sphere x(a1);
cout<<x.showSpheres()<<endl;
#include<cmath>
using namespace std;
#define PI 3.14
class Base{
protected:
double r;
public:
Base(double a=1)
{
r=a;
}
};
class Sphere:public Base{
public:
Sphere(double a=1):Base(a){}
double getSpheres()
{
return 4*PI*r*r;
}
double showSpheres()
{
cout<<"The area of the sphere:"<<getSpheres();
}
};
int main()
{
double a1;
cout<<"Input the radius of the sphere:";
cin>>a1;
Sphere x(a1);
cout<<x.showSpheres()<<endl;
}

本文展示了一个使用C++编程语言实现的简单程序,该程序通过定义基类和派生类来计算球体的表面积。用户可以输入球体的半径,程序将输出对应的表面积。
6122





