定义基类Point和派生类Circle,求圆的周长

定义基类Point(点)和派生类Circle(圆),求圆的周长。Point类有两个私有的数据成员float x,y;Circle类新增一个私有的数据成员半径float r和一个公有的求周长的函数getCircumference();主函数已经给出,请编写Point和Circle类。

输入格式:

输入圆心和半径,x y r中间用空格分隔。

输出格式:

输出圆的周长,小数点后保留2位有效数字。

输入样例:

1 2 3

输出样例:

在这里给出相应的输出。例如:

Point constructor called
Circle constructor called
18.84
Circle destructor called
Point destructor called
#include<iostream>
#include<iomanip>
using namespace std;
class Point {
public:
	Point() {
		cout << "Point constructor called" << endl;
	}
	~Point() {
		cout << "Point destructor called" << endl;
	}
private:
	float x;
	float y;
};
class Circle :public Point{
public:
	Circle(float x,float y,float r){
		this->x = x;
		this->y = y;
		this->r = r;
		cout << "Circle constructor called" << endl;
	}
	~Circle() {
		cout << "Circle destructor called" << endl;
	}
	double getCircumference()
	{
		S = 2 * pi * r;
		return S;
	}
	double S;
private:
	float x, y;
	float r;
	float pi = 3.14;
};
int main(){
	float x, y, r;
	cin >> x >> y >> r;
	Circle c(x, y, r);
	cout << fixed << setprecision(2) << c.getCircumference() << endl;
	return 0;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: point是一个表示点的类,包含两个属性x和y,表示点的横纵坐标。 派生circle是一个表示圆的类,继承自point,包含一个属性radius,表示圆的半径。 求圆周长可以使用公式C=2πr,其中r为圆的半径,π为圆周率,约等于3.14。 因此,圆的周长可以表示为C=2π*radius。在circle类中,可以定义一个方法get_circumference(),用于计算圆的周长。具体实现如下: class point: def __init__(self, x, y): self.x = x self.y = y class circle(point): def __init__(self, x, y, radius): super().__init__(x, y) self.radius = radius def get_circumference(self): return 2 * 3.14 * self.radius 假设有一个圆c,其半径为5,则可以通过以下代码计算其周长: c = circle(, , 5) circumference = c.get_circumference() print(circumference) 输出结果为31.4,即圆的周长约为31.4。 ### 回答2: 定义point派生circle,可以使用面向对象的思想来设计程序。在这个程序中,point作为类,主要用来存储点的坐标信息。circle作为派生类,继承了point所有的属性和方法,并添加了一个特有的属性——半径。 在circle中,定义一个求圆周长的方法circumference(),在该方法中通过调用父类point中获取坐标信息的方法,以及自身的半径属性来计算出圆的周长。具体的实现可以参照以下代码示例: ```python class point: def __init__(self, x, y): self.x = x self.y = y def get_x(self): return self.x def get_y(self): return self.y class circle(point): def __init__(self, x, y, r): super().__init__(x, y) self.r = r def get_r(self): return self.r def circumference(self): import math return 2 * math.pi * self.r ``` 在上述代码中,point类有一个构造方法__init__(),用来初始化坐标信息,以及两个获取坐标信息的方法get_x()和get_y()。而circle类继承了point类,并添加了一个半径属性,以及一个求周长的方法circumference()。 使用该程序,可以创建一个圆对象并调用circumference()方法来求圆周长。如下面这个示例: ```python c = circle(1, 2, 3) c_circumference = c.circumference() print("圆的周长是:", c_circumference) ``` 在上述示例中,我们创建了一个圆对象c,它的圆心坐标为(1, 2),半径为3。然后,调用c的circumference()方法,将计算出的周长保存在变量c_circumference中,并打印出来。 通过这种面向对象的程序设计方式,我们不仅可以方便地求出圆的周长,而且还可以扩展程序,添加更多的图形对象和方法。 ### 回答3: point定义Point是一个类,用来表示平面上的一个点,其中包含两个变量:横坐标x和纵坐标y。point中包含以下成员函数: - 构造函数:用来初始化Point对象的x和y坐标。 - getX():返回Point对象的横坐标x。 - getY():返回Point对象的纵坐标y。 - setX(double x):设置Point对象的横坐标x。 - setY(double y):设置Point对象的纵坐标y。 派生circle定义Circle是一个派生类,用来表示平面上的一个圆,其中包含一个圆心点的引用(point对象)和一个半径radius。派生circle中包含以下成员函数: - 构造函数:用来初始化Circle对象,需要传递一个圆心点和半径。 - getCenter():返回Circle对象的圆心点。 - getRadius():返回Circle对象的半径。 - setCenter(Point center):设置Circle对象的圆心点。 - setRadius(double radius):设置Circle对象的半径。 - getPerimeter():计算Circle对象的周长。 计算圆的周长: 根据圆的定义可知,圆的周长公式是2πr,即半径(radius)乘以2π。而我们在派生circle中已经定义了一个getRadius()函数,可以用来获取圆的半径;因此,我们只需要在派生circle中再定义一个getPerimeter()函数,用来计算圆的周长即可。 getPerimeter()函数的实现: double getPerimeter() { return 2 * 3.14159 * getRadius(); } 总结: 在上述代码中,我们定义了一个point和一个派生circle,用来表示平面上的点和圆。通过在派生circle定义getPerimeter()函数,我们可以方便地计算圆的周长。这种面向对象的编程方式具有良好的可维护性和扩展性,可以方便地添加新的方法和属性,同时避免了代码冗余。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值