您的位置:首页 > 编程语言 > C语言/C++

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

2018-09-01 16:10 183 查看

其属性包含圆心和半径

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

main.cpp

[code]#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 fi
4000
rst 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

[code]#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

[code]#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;
}
}

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐