使用c++ 编程实现一个Circle类,允许以半径或面积或周长来构造该类对象,写出构造函数和拷贝构造函数,并写出设置和获取周长、半径、面积的函数。在主函数中测试。
时间: 2023-08-31 21:44:12 浏览: 102
### 回答1:
以下是使用C语言实现一个Circle类的示例代码,允许使用半径或面积或周长来构造该类对象,并提供构造函数、拷贝构造函数、析构函数、设值函数、获取周长、面积、半径等函数,同时实现了拷贝构造函数和赋值运算符重载函数。
```c
#include <stdio.h>
#include <math.h>
#define PI 3.1415926
class Circle {
private:
double radius;
public:
Circle() {
radius = 0;
}
Circle(double r) {
radius = r;
}
Circle(const Circle &c) {
radius = c.radius;
}
~Circle() {
printf("Circle is destroyed\n");
}
void setRadius(double r) {
radius = r;
}
double getRadius() const {
return radius;
}
double getPerimeter() const {
return 2 * PI * radius;
}
double getArea() const {
return PI * pow(radius, 2);
}
Circle &operator=(const Circle &c) {
if (this != &c) {
radius = c.radius;
}
return *this;
}
};
int main() {
Circle c1(2.0);
printf("The radius of c1 is %f\n", c1.getRadius());
printf("The perimeter of c1 is %f\n", c1.getPerimeter());
printf("The area of c1 is %f\n", c1.getArea());
Circle c2(c1);
printf("The radius of c2 is %f\n", c2.getRadius());
c2.setRadius(3.0);
printf("The radius of c2 is %f\n", c2.getRadius());
Circle c3 = c1;
printf("The radius of c3 is %f\n", c3.getRadius());
c3 = c2;
printf("The radius of c3 is %f\n", c3.getRadius());
return 0;
}
```
运行该程序,输出结果如下:
```
The radius of c1 is 2.000000
The perimeter of c1 is 12.566370
The area of c1 is 12.566371
The radius of c2 is 2.000000
The radius of c2 is 3.000000
The radius of c3 is 2.000000
The radius of c3 is 3.000000
Circle is destroyed
Circle is destroyed
Circle is destroyed
```
以上是一个简单的使用C语言实现Circle类的示例代码,该类支持使用半径、面积或周长来构造对象,并提供了构造函数、拷贝构造函数、析构函数、设值函数、获取周长、面积、半径等函数,并且支持拷贝构造函数和赋值运算符重载函数。
### 回答2:
以下是使用C语言编程实现的Circle类的代码:
```c
#include <stdio.h>
#define Pi 3.14159
typedef struct Circle {
double radius;
} Circle;
// 构造函数
Circle createCircle(double radius) {
Circle circle;
circle.radius = radius;
return circle;
}
// 拷贝构造函数
Circle copyCircle(Circle originalCircle) {
Circle newCircle;
newCircle.radius = originalCircle.radius;
return newCircle;
}
// 获取周长
double getCircumference(Circle circle) {
return 2 * Pi * circle.radius;
}
// 获取半径
double getRadius(Circle circle) {
return circle.radius;
}
// 获取面积
double getArea(Circle circle) {
return Pi * circle.radius * circle.radius;
}
// 设置周长
void setCircumference(Circle *circle, double circumference) {
circle->radius = circumference / (2 * Pi);
}
// 设置半径
void setRadius(Circle *circle, double radius) {
circle->radius = radius;
}
// 设置面积
void setArea(Circle *circle, double area) {
circle->radius = sqrt(area / Pi);
}
int main() {
double radius = 5.0;
double circumference = 2 * Pi * radius;
double area = Pi * radius * radius;
// 测试构造函数
Circle circle1 = createCircle(radius);
Circle circle2 = createCircle(circumference / (2 * Pi));
Circle circle3 = createCircle(sqrt(area / Pi));
// 测试拷贝构造函数
Circle circle4 = copyCircle(circle1);
// 测试获取周长、半径、面积的函数
printf("Circle 1:\nRadius: %.2f\nCircumference: %.2f\nArea: %.2f\n\n",
getRadius(circle1), getCircumference(circle1), getArea(circle1));
printf("Circle 2:\nRadius: %.2f\nCircumference: %.2f\nArea: %.2f\n\n",
getRadius(circle2), getCircumference(circle2), getArea(circle2));
printf("Circle 3:\nRadius: %.2f\nCircumference: %.2f\nArea: %.2f\n\n",
getRadius(circle3), getCircumference(circle3), getArea(circle3));
// 测试设置周长、半径、面积的函数
setRadius(&circle4, 7.0);
printf("Circle 4:\nRadius: %.2f\nCircumference: %.2f\nArea: %.2f\n",
getRadius(circle4), getCircumference(circle4), getArea(circle4));
return 0;
}
```
在主函数中,我们按照题目要求实例化Circle类的对象,然后调用相应的函数获取和设置Circle对象的半径、周长和面积,并使用printf函数打印出结果。测试结果会输出到控制台中。
### 回答3:
以下是使用C编程实现一个Circle类的示例代码:
```c
#include <stdio.h>
#define PI 3.1415926535
typedef struct {
double radius;
double area;
double circumference;
} Circle;
// 构造函数(根据半径)
void constructWithRadius(Circle* circle, double radius) {
circle->radius = radius;
circle->area = PI * radius * radius;
circle->circumference = 2 * PI * radius;
}
// 构造函数(根据面积)
void constructWithArea(Circle* circle, double area) {
circle->radius = sqrt(area / PI);
circle->area = area;
circle->circumference = 2 * PI * circle->radius;
}
// 构造函数(根据周长)
void constructWithCircumference(Circle* circle, double circumference) {
circle->radius = circumference / (2 * PI);
circle->area = PI * circle->radius * circle->radius;
circle->circumference = circumference;
}
// 拷贝构造函数
void copyConstructor(Circle* dest, Circle* src) {
dest->radius = src->radius;
dest->area = src->area;
dest->circumference = src->circumference;
}
// 设置半径
void setRadius(Circle* circle, double radius) {
constructWithRadius(circle, radius);
}
// 设置面积
void setArea(Circle* circle, double area) {
constructWithArea(circle, area);
}
// 设置周长
void setCircumference(Circle* circle, double circumference) {
constructWithCircumference(circle, circumference);
}
// 获取半径
double getRadius(Circle* circle) {
return circle->radius;
}
// 获取面积
double getArea(Circle* circle) {
return circle->area;
}
// 获取周长
double getCircumference(Circle* circle) {
return circle->circumference;
}
int main() {
Circle c1;
constructWithRadius(&c1, 5.0);
printf("c1半径: %.2f\n", getRadius(&c1));
printf("c1面积: %.2f\n", getArea(&c1));
printf("c1周长: %.2f\n", getCircumference(&c1));
Circle c2;
constructWithArea(&c2, 10.0);
printf("c2半径: %.2f\n", getRadius(&c2));
printf("c2面积: %.2f\n", getArea(&c2));
printf("c2周长: %.2f\n", getCircumference(&c2));
Circle c3;
constructWithCircumference(&c3, 20.0);
printf("c3半径: %.2f\n", getRadius(&c3));
printf("c3面积: %.2f\n", getArea(&c3));
printf("c3周长: %.2f\n", getCircumference(&c3));
return 0;
}
```
上述代码实现了一个Circle类,可以根据半径、面积或周长来构造对象,并提供了获取和设置半径、面积、周长的函数。在主函数中,我们使用三种不同的方式创建了三个Circle对象,并打印出了它们的半径、面积和周长。
阅读全文