王桂林 C++基础与提高 练习题——按需求设计一个圆类

其属性包含圆心和半径

创建两个圆形对象,提示用户输入圆心和半径,判断两圆是是否相交

main.cpp

#include <iostream>
#include <math.h>
#include "fhnClassList.h"
using namespace std;


int main()
{
    double x = 0.0;
    double y = 0.0;
    double r = 0.0;
    cout<<"Please input the x coordinate of first sircle : ";
    cin>>x;
    cout<<"Please input the y coordinate of first sircle : ";
    cin>>y;
    cout<<"Please input the radius of first sircle : ";
    cin>>r;
    Point a(x, y);
    Circle c1(a, r);

    cout<<"Please input the x coordinate of second sircle : ";
    cin>>x;
    cout<<"Please input the y coordinate of second sircle : ";
    cin>>y;
    cout<<"Please input the radius of second sircle : ";
    cin>>r;
    Point b(x, y);
    Circle c2(b, r);
    c1.isIntersect(c2);
    return 0;

}

fhnClassList.h

#ifndef _FHN_CLASS_LIST_
#define _FHN_CLASS_LIST_

#pragma once
class Point
{
public:
    Point();
    Point(double a, double b);
    Point(const Point & anotherPoint);

    ~Point();
    double distance(Point anotherPoint);
private:
    double x;
    double y;
};

class Circle
{
public:
    Circle();
    Circle(Point center, double r);
    ~Circle();
    bool isIntersect(const Circle & anotherCircle);
private:
    Point circleCenter;
    double radius;
};
#endif

fhnClassList.cpp

#include "fhnClassList.h"
#include <iostream>
using namespace std;

Point::Point()
{
}

Point::Point(double a, double b):x(a),y(b){}

Point::Point(const Point & anotherPoint)
{
    this->x = anotherPoint.x;
    this->y = anotherPoint.y;
}

Point::~Point()
{
}
double Point::distance(Point anotherPoint)
{
    return sqrt(pow((this->x - anotherPoint.x), 2) + pow((this->y - anotherPoint.y), 2));
}



Circle::Circle()
{
}
Circle::Circle(Point center, double r):circleCenter(center),radius(r)
{
}
Circle::~Circle()
{
}
bool Circle::isIntersect(const Circle & anotherCircle)
{
    if(this->circleCenter.distance(anotherCircle.circleCenter) <= (this->radius + anotherCircle.radius))
    {
        cout<<"the tow sircle is intersected."<<endl;
        return true;
    }
    else
    {
        cout<<"the tow sircle is not intersected."<<endl;
        return false;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值