继承与多态 圆类 圆柱体类
时间: 2024-05-08 15:16:27 浏览: 42
圆类和圆柱体类是一个很好的继承示例,因为圆柱体是由圆所定义的。圆类可以是基类,而圆柱体类可以是派生类。
圆类可以定义圆的基本属性和方法,如半径、周长、面积等。而圆柱体类可以继承这些属性和方法,并添加自己的属性和方法,如高度和体积等。
在多态方面,圆类和圆柱体类可以共享相同的方法名,如计算周长和面积的方法。这意味着,如果我们有一个指向圆类对象的指针和一个指向圆柱体类对象的指针,我们可以使用相同的方法名来计算它们的周长和面积,而不必担心它们具体是哪个类。
例如,我们可以定义一个函数来计算一个列表中所有圆和圆柱体的总面积:
```
double getTotalArea(Shape** shapes, int count) {
double totalArea = 0.0;
for (int i = 0; i < count; i++) {
totalArea += shapes[i]->getArea();
}
return totalArea;
}
```
在这个函数中,我们使用一个指向Shape类的指针数组来处理圆和圆柱体对象。由于Circle和Cylinder类都继承自Shape类,并实现了getArea()方法,因此我们可以使用相同的方法名来计算它们的面积。
相关问题
使用虚函数编写程序求球体和圆柱体的体积和表面积。具体要求如下: (1)由于球体和圆柱体都可以看作由圆继承而来,所以可以定义圆类Circle为基类;在Circle类中定义求圆面积和体积的函数。 (2)由Circle类派生出
Sphere类和Cylinder类,分别表示球体和圆柱体。在这两个类中分别定义虚函数求表面积和体积。
下面是程序实现:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
class Circle {
public:
Circle(double r) : radius(r) {}
virtual double getArea() const { // 圆面积
return M_PI * radius * radius;
}
virtual double getVolume() const = 0; // 纯虚函数,求体积
protected:
double radius;
};
class Sphere : public Circle {
public:
Sphere(double r) : Circle(r) {}
virtual double getArea() const { // 球表面积
return 4 * M_PI * radius * radius;
}
virtual double getVolume() const { // 球体积
return 4.0 / 3.0 * M_PI * pow(radius, 3);
}
};
class Cylinder : public Circle {
public:
Cylinder(double r, double h) : Circle(r), height(h) {}
virtual double getArea() const { // 圆柱表面积
return 2 * M_PI * radius * height + 2 * Circle::getArea();
}
virtual double getVolume() const { // 圆柱体积
return Circle::getArea() * height;
}
private:
double height;
};
int main() {
Circle* p;
double r, h;
cout << "请输入球半径和圆柱底面半径、高度:";
cin >> r >> h;
p = new Sphere(r);
cout << "球的表面积为:" << p->getArea() << ",体积为:" << p->getVolume() << endl;
delete p;
p = new Cylinder(r, h);
cout << "圆柱的表面积为:" << p->getArea() << ",体积为:" << p->getVolume() << endl;
delete p;
return 0;
}
```
程序中定义了圆类Circle作为基类,其中定义了求圆面积的函数getArea()和纯虚函数求体积getVolume()。派生类Sphere和Cylinder分别表示球体和圆柱体,其中重写了getArea()函数求表面积,实现了getVolume()函数求体积。在主函数中,通过指针调用不同的对象的函数,实现了多态。
从球体、圆柱体抽象出一个公共基类Container,类Container内定义两个类变量pi圆周率,total_area总表面积、两个未实现的方法。模拟实现一个容器类层次结构,子类要求实现父类的所有方法,并在主方法进行多态测试。 设计一个类Container,有以下类变量、方法: PI=3.1415926 类变量 total_area=0 类变量 area(): 未实现 __str__():未实现 设计一个子类Sphere,继承Container类,实现以下方法: __init__():构造方法,参数:半径 area():返回球体表面积 __str__():返回球体名称、球体表面积的描述 设计一个子类Cylinder,继承Container类,实现以下方法: __init__():构造方法,参数:半径、高 area():返回圆柱体表面积 __str__():返回圆柱体名称、圆柱体表面积的描述
下面是代码实现:
```
class Container:
PI = 3.1415926
total_area = 0
def area(self):
pass
def __str__(self):
pass
class Sphere(Container):
def __init__(self, radius):
self.radius = radius
def area(self):
area = 4 * self.PI * self.radius ** 2
self.total_area += area
return area
def __str__(self):
return f"Sphere with radius {self.radius} has surface area {self.area()}"
class Cylinder(Container):
def __init__(self, radius, height):
self.radius = radius
self.height = height
def area(self):
area = 2 * self.PI * self.radius * (self.radius + self.height)
self.total_area += area
return area
def __str__(self):
return f"Cylinder with radius {self.radius} and height {self.height} has surface area {self.area()}"
# 多态测试
container_list = [Sphere(1), Sphere(2), Cylinder(1, 2), Cylinder(2, 3)]
for container in container_list:
print(container)
print(f"Total surface area is {Container.total_area}")
```
运行结果如下:
```
Sphere with radius 1 has surface area 12.5663704
Sphere with radius 2 has surface area 50.2654816
Cylinder with radius 1 and height 2 has surface area 18.849556
Cylinder with radius 2 and height 3 has surface area 75.398224
Total surface area is 157.079632
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)