C++写一个球体表面积的代码
时间: 2023-12-01 11:07:50 浏览: 41
以下是使用C++计算球体表面积的代码:
```c++
#include <iostream>
using namespace std;
int main()
{
double radius, surface_area;
const double PI = 3.14159265358979323846;
cout << "Enter the radius of the sphere: ";
cin >> radius;
surface_area = 4 * PI * radius * radius;
cout << "The surface area of the sphere is " << surface_area << endl;
return 0;
}
```
代码中使用了常量PI来存储圆周率的值,然后通过用户输入的半径计算球体表面积。
相关问题
c++使用虚函数求球体和圆柱体的体积和表面积
以下是使用虚函数求球体和圆柱体的体积和表面积的C++代码示例:
```
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double getVolume() = 0;
virtual double getSurfaceArea() = 0;
};
class Sphere : public Shape {
private:
double radius;
public:
Sphere(double r) {
radius = r;
}
double getVolume() {
return (4.0/3.0) * M_PI * pow(radius, 3);
}
double getSurfaceArea() {
return 4 * M_PI * pow(radius, 2);
}
};
class Cylinder : public Shape {
private:
double radius;
double height;
public:
Cylinder(double r, double h) {
radius = r;
height = h;
}
double getVolume() {
return M_PI * pow(radius, 2) * height;
}
double getSurfaceArea() {
return 2 * M_PI * radius * (radius + height);
}
};
int main() {
Shape* shape;
Sphere sphere(2.0);
Cylinder cylinder(2.0, 5.0);
shape = &sphere;
cout << "Sphere Volume: " << shape->getVolume() << endl;
cout << "Sphere Surface Area: " << shape->getSurfaceArea() << endl;
shape = &cylinder;
cout << "Cylinder Volume: " << shape->getVolume() << endl;
cout << "Cylinder Surface Area: " << shape->getSurfaceArea() << endl;
return 0;
}
```
在上面的代码中,我们定义了一个抽象基类`Shape`,包含两个纯虚函数`getVolume`和`getSurfaceArea`。然后,我们定义了两个派生类`Sphere`和`Cylinder`,分别表示球体和圆柱体。这两个派生类继承了`Shape`类,并实现了`getVolume`和`getSurfaceArea`函数。
在`main`函数中,我们首先创建了一个`Sphere`对象和一个`Cylinder`对象,并将它们的地址分别赋给一个`Shape`指针变量`shape`。然后,我们通过`shape`指针调用了`getVolume`和`getSurfaceArea`函数,分别输出了球体和圆柱体的体积和表面积。
需要注意的是,我们在`Shape`类中将`getVolume`和`getSurfaceArea`函数声明为纯虚函数,这意味着我们不能直接创建`Shape`对象,而只能通过派生类来创建对象。这种技术称为抽象类,它可以帮助我们实现多态性,即在运行时根据对象的实际类型来调用相应的函数。
用c++写一个程序,定义抽象基类Container,由它派生出3个派生类,Sphere(球体)、Cylinder(圆柱体)、Cube(正方体)。用虚函数分别计算几种图形的表面积和体积
好的,下面是 C++ 代码实现:
```c++
#include <iostream>
using namespace std;
// 抽象基类 Container
class Container {
public:
virtual double getSurfaceArea() = 0; // 计算表面积的虚函数
virtual double getVolume() = 0; // 计算体积的虚函数
};
// 派生类 Sphere(球体)
class Sphere : public Container {
public:
Sphere(double r) : radius(r) {}
double getSurfaceArea() { return 4 * 3.14159 * radius * radius; } // 计算表面积
double getVolume() { return 4.0 / 3.0 * 3.14159 * radius * radius * radius; } // 计算体积
private:
double radius; // 球体半径
};
// 派生类 Cylinder(圆柱体)
class Cylinder : public Container {
public:
Cylinder(double r, double h) : radius(r), height(h) {}
double getSurfaceArea() { return 2 * 3.14159 * radius * height + 2 * 3.14159 * radius * radius; } // 计算表面积
double getVolume() { return 3.14159 * radius * radius * height; } // 计算体积
private:
double radius; // 圆柱体底面半径
double height; // 圆柱体高
};
// 派生类 Cube(正方体)
class Cube : public Container {
public:
Cube(double a) : sideLength(a) {}
double getSurfaceArea() { return 6 * sideLength * sideLength; } // 计算表面积
double getVolume() { return sideLength * sideLength * sideLength; } // 计算体积
private:
double sideLength; // 正方体边长
};
int main() {
Container* c1 = new Sphere(2.0);
Container* c2 = new Cylinder(2.0, 3.0);
Container* c3 = new Cube(2.0);
cout << "球体表面积:" << c1->getSurfaceArea() << ",体积:" << c1->getVolume() << endl;
cout << "圆柱体表面积:" << c2->getSurfaceArea() << ",体积:" << c2->getVolume() << endl;
cout << "正方体表面积:" << c3->getSurfaceArea() << ",体积:" << c3->getVolume() << endl;
delete c1;
delete c2;
delete c3;
return 0;
}
```
上面的代码中,抽象基类 Container 定义了纯虚函数 getSurfaceArea() 和 getVolume(),由三个派生类 Sphere、Cylinder 和 Cube 分别实现了这两个函数。在主函数中,我们创建了三个不同的对象,并调用他们的表面积和体积计算函数。请注意,为了实现多态,我们使用了基类指针指向派生类对象。在程序结束时,我们需要手动删除这三个对象,以释放动态分配的内存。