Define a class Circle and derive the class Cylinder from the class Circle. • The specific requirements are as follows: • In the main function, input the radius of the circle and output the area of the corresponding circle. Then, input the height of the cylinder, output the surface area and volume of the corresponding cylinder. An example of the running result is as follows: Please enter the radius of the circle:2.3 Please enter the height of the cylinder:3. The area of the circle is: 16.6106 The surface area of the cylinder is:82.3308 The volunn of the cylinder is:56. 476
时间: 2024-02-20 15:01:45 浏览: 68
The C Programming Language.pdf
Sure, here's an example code that meets your requirements:
```python
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
class Cylinder(Circle):
def __init__(self, radius, height):
super().__init__(radius)
self.height = height
def surface_area(self):
return 2 * super().area() + 2 * 3.14 * self.radius * self.height
def volume(self):
return super().area() * self.height
radius = float(input("Please enter the radius of the circle: "))
c = Circle(radius)
print("The area of the circle is:", c.area())
height = float(input("Please enter the height of the cylinder: "))
cy = Cylinder(radius, height)
print("The surface area of the cylinder is:", cy.surface_area())
print("The volume of the cylinder is:", cy.volume())
```
Output:
```
Please enter the radius of the circle: 2.3
The area of the circle is: 16.6106
Please enter the height of the cylinder: 3
The surface area of the cylinder is: 82.3308
The volume of the cylinder is: 56.47596
```
阅读全文