类和对象 创建一个圆类,并计算点在圆上还是圆内圆外

本文介绍了如何在C++中定义一个圆类和点类,以及实现一个函数isInCircle来判断一个点是否在给定圆内、圆上或圆外。通过实例展示了如何创建圆对象并测试点的位置关系。

#include<iostream>
using namespace std;
#include<string>
//类和对象  创建一个圆类,并计算点在圆上还是圆内圆外
//类的属性一般设置为私有
class point
{
public:
    void setx(int x)
    {
        m_x = x;
    };
    int getx()
    {
        return m_x;
    };
    void sety(int y)
    {
        m_y = y;
    };
    int gety()
    {
        return m_y;
    };
private:
    int m_x;
    int m_y;
};
class circle
{
public:
    void setr(int r)
    {
        m_r = r;
    };
    int getr()
    {
        return m_r;
    };
    void setc(point center)
    {
        m_center = center;
    };
    point getc()
    {
        return m_center;
    };
private:
    int m_r;
    point m_center;
};
void isincircle(circle& C, point& p)
{
    int a = (C.getc().getx() - p.getx()) * (C.getc().getx() - p.getx()) +
        (C.getc().gety() - p.gety()) * (C.getc().gety() - p.gety());
    int b = C.getr() * C.getr();
    if (a == b)
    {
        cout << "点在圆上";
    }
    else if (a < b)
    {
        cout << "点在圆内";
    }
    else if (a > b)
    {
        cout << "点在圆外";
    }
};
int main()
{
    circle C;
    C.setr(10);
    point A;
    A.setx(11);
    A.sety(0);
    C.setc(A);
    point p;
    p.setx(10);
    p.sety(10);
    isincircle(C, p);

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值