【C++】封装一个圆类,求周长面积,判断点相对于圆的位置

本文介绍了如何使用C++编程语言定义Point和Circle类,通过实例展示如何计算圆的周长、面积,并实现点与圆的位置关系判断,包括圆内、圆上和圆外的区分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#define PI 3.14
using namespace std;
class Point
{
public:
	float x;
	float y;
};
class circle
{
public:
	float radius;
	Point c_p;//圆心
	void perimeter()
	{
		float c = 2 * radius * PI;
		cout << "该圆的周长为:" << c << endl;
	}
	void area()
	{
		float s = radius * radius * PI;
		cout << "该圆的面积为:" << s << endl;
	}
	void centerpoint()
	{
		cout << "圆心坐标为:" << "(" << c_p.x << "," << c_p.y << ")" << endl;
		cout << "圆半径为:" << radius << endl;
	}
	void PointCircle(Point p1)
	{
		float d_x = (c_p.x - p1.x) * (c_p.x - p1.x);//两点间x距离的平方
		float d_y = (c_p.y - p1.y) * (c_p.y - p1.y);//两点间y距离的平方
		float d_r = radius * radius;//圆半径的平方
		cout << "点坐标为:" << "(" << p1.x << "," << p1.y << ")" << endl;
		if (d_r>d_x+d_y)//x^2+y^2=d^2点到圆心(0,0)的距离
		{
			cout << "点在圆内" << endl;
		}
		else if (d_r<d_x+d_y)
		{
			cout << "点在圆外" << endl;
		}
		else
		{
			cout << "点在圆上" << endl;
		}
	}
};
int main()
{
	Point center;//设置点center为(0,0)
	center.x = 0;
	center.y = 0;
	Point p1;//设置点p1
	//p1.x = 1;
	//p1.y = 1;
	cout << "请输出点坐标(x,y):";
	cin >> p1.x;
	cin >> p1.y;
	circle c1;//设置圆
	//c1.radius = 3;//半径为3
	cout << "请输入半径:";
	cin >> c1.radius;
	c1.c_p = center;//圆心为点center
	c1.centerpoint();//显示圆心、半径
	c1.perimeter();//求周长
	c1.area();//求面积
	c1.PointCircle(p1);//设置的点p1是否在圆内
	return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lindsey小姐月光加冕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值