定义一个求圆形周长和面积的函数,并 调用。
时间: 2024-04-29 17:24:27 浏览: 9
以下是Python代码实现:
```python
import math
def circle(radius):
# 求圆形周长
circumference = 2 * math.pi * radius
# 求圆形面积
area = math.pi * radius ** 2
# 返回周长和面积
return circumference, area
# 调用函数
radius = 5
circumference, area = circle(radius)
print("半径为{}的圆形周长为{},面积为{}".format(radius, circumference, area))
```
输出结果:
```
半径为5的圆形周长为31.41592653589793,面积为78.53981633974483
```
相关问题
定义一个 Shape 抽象类,定义求面积、周长的纯虚函数,由它派生出 圆形类(Circle)和矩形类(Rectangle),各自定义自己的面积和周长函数。在主函 数可通过派生类对象调用相应的成员函数,
好的,下面是 Shape 抽象类的实现:
```cpp
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() const = 0; // 求面积的纯虚函数
virtual double perimeter() const = 0; // 求周长的纯虚函数
};
class Circle : public Shape {
private:
double radius; // 半径
public:
Circle(double r) : radius(r) {}
double area() const {
return 3.14159 * radius * radius;
}
double perimeter() const {
return 2 * 3.14159 * radius;
}
};
class Rectangle : public Shape {
private:
double width; // 宽度
double height; // 高度
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() const {
return width * height;
}
double perimeter() const {
return 2 * (width + height);
}
};
int main() {
Shape* s1 = new Circle(5.0);
cout << "Circle area = " << s1->area() << endl;
cout << "Circle perimeter = " << s1->perimeter() << endl;
Shape* s2 = new Rectangle(3.0, 4.0);
cout << "Rectangle area = " << s2->area() << endl;
cout << "Rectangle perimeter = " << s2->perimeter() << endl;
delete s1;
delete s2;
return 0;
}
```
这个程序定义了一个 Shape 抽象类,其中定义了求面积和周长的纯虚函数。然后从 Shape 类派生出 Circle 类和 Rectangle 类,各自定义自己的面积和周长函数。在主函数中通过派生类对象调用相应的成员函数进行测试。
定义一个包含两个纯虚函数的基类定义两个分别表示矩形和圆形的派生类分别计算出矩形和圆形的面积和周长。
### 回答1:
首先,基类可以定义如下:
```
class Shape {
public:
virtual double Area() = 0;
virtual double Perimeter() = 0;
};
```
纯虚函数是指不包含函数体的虚函数。它只有声明,没有实现。通常,我们使用纯虚函数来定义接口,即为了规定派生类必须实现的函数。
然后,可以使用基类来定义矩形和圆形的派生类:
```
class Rectangle : public Shape {
public:
Rectangle(double length, double width) : length_(length), width_(width) {}
double Area() override {
return length_ * width_;
}
double Perimeter() override {
return 2 * (length_ + width_);
}
private:
double length_;
double width_;
};
class Circle : public Shape {
public:
Circle(double radius) : radius_(radius) {}
double Area() override {
return 3.14 * radius_ * radius_;
}
double Perimeter() override {
return 2 * 3.14 * radius_;
}
private:
double radius_;
};
```
在这里,我们使用 `override` 关键字来声明派生类函数是覆盖基类纯虚函数的实现。
最后,我们可以使用派生类的对象来计算矩形或圆形的面积和周长:
```
Rectangle rect(10, 5);
Circle circle(2);
cout << "Rectangle area: " << rect.Area() << endl;
cout << "Rectangle perimeter: " << rect.Perimeter() << endl;
cout << "Circle area: " << circle.Area() << endl;
cout << "Circle perimeter: " << circle.Perimeter() << endl;
```
输出结果如下:
```
Rectangle area: 50
Rectangle perimeter: 30
Circle area: 12.56
Circle perimeter: 12.56
```
### 回答2:
基类的定义如下:
```cpp
class Shape {
public:
virtual float getArea() const = 0; // 纯虚函数,计算面积
virtual float getPerimeter() const = 0; // 纯虚函数,计算周长
};
```
派生类Rectangle(矩形)的定义如下:
```cpp
class Rectangle : public Shape {
private:
float length;
float width;
public:
Rectangle(float l, float w) : length(l), width(w) {}
float getArea() const override {
return length * width;
}
float getPerimeter() const override {
return 2 * (length + width);
}
};
```
派生类Circle(圆形)的定义如下:
```cpp
class Circle : public Shape {
private:
float radius;
public:
Circle(float r) : radius(r) {}
float getArea() const override {
return 3.14159 * radius * radius;
}
float getPerimeter() const override {
return 2 * 3.14159 * radius;
}
};
```
在派生类Rectangle中,我们利用构造函数接收矩形的长和宽,并且重写基类中的纯虚函数,计算出矩形的面积 `length * width` 和周长 `2 * (length + width)`。
在派生类Circle中,我们利用构造函数接收圆形的半径,并且重写基类中的纯虚函数,计算出圆形的面积 `3.14159 * radius * radius` 和周长 `2 * 3.14159 * radius`。
通过这样的设计,我们可以通过调用基类指针指向派生类对象,通过基类指针调用相应的虚函数,从而实现多态性,并且可以正确计算矩形和圆形的面积和周长。
### 回答3:
基类的定义可以如下所示:
```cpp
class Shape {
public:
virtual float getArea() const = 0;
virtual float getPerimeter() const = 0;
};
```
矩形类的派生类定义如下:
```cpp
class Rectangle : public Shape {
protected:
float length;
float width;
public:
Rectangle(float l, float w) : length(l), width(w) {}
float getArea() const override {
return length * width;
}
float getPerimeter() const override {
return 2 * (length + width);
}
};
```
圆形类的派生类定义如下:
```cpp
class Circle : public Shape {
protected:
float radius;
public:
Circle(float r) : radius(r) {}
float getArea() const override {
return 3.14 * radius * radius;
}
float getPerimeter() const override {
return 2 * 3.14 * radius;
}
};
```
这样,当你创建一个矩形或圆形对象时,可以通过调用`getArea()`方法计算出其面积,调用`getPerimeter()`方法计算出其周长。例如:
```cpp
Rectangle rect(5, 3); // 创建一个矩形对象,长度为5,宽度为3
cout << "矩形的面积:" << rect.getArea() << endl; // 输出:15
cout << "矩形的周长:" << rect.getPerimeter() << endl; // 输出:16
Circle circle(4); // 创建一个圆形对象,半径为4
cout << "圆形的面积:" << circle.getArea() << endl; // 输出:50.24
cout << "圆形的周长:" << circle.getPerimeter() << endl; // 输出:25.12
```