王桂林 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; } }
阅读更多
相关文章推荐
- C02-程序设计基础提高班(C++)第3周上机任务
- C02-程序设计基础提高班(C++)第11周上机任务-运算符重载
- C02-程序设计基础提高班(C++)第7周上机任务-指针
- C02-程序设计基础提高班(C++)第10周上机任务-类和对象之二
- C02-程序设计基础提高班(C++)第8周上机任务-结构体
- C++中提供了多种基本的数据类型。实际上,这些远不能满足我们的需求,如复数(第10章的例子大多是处理虚数的),再如分数。本任务将设计一个简单的分数类,完成对分数的几个运算。一则巩固基于对象编程的方法,
- C02-程序设计基础提高班(C++)第13周上机任务-多态与虚函数
- C02-程序设计基础提高班(C++)第4周上机任务
- C02-程序设计基础提高班(C++)学习安排
- C02-程序设计基础提高班(C++)第4周上机任务
- C02-程序设计基础提高班(C++)学习安排
- C02-程序设计基础提高班(C++)第9周上机任务-类和对象
- C02-程序设计基础提高班(C++)第5周上机任务
- C02-程序设计基础提高班(C++)第9周上机任务-类和对象
- C02-程序设计基础提高班(C++)第5周上机任务
- C02-程序设计基础提高班(C++)第9周上机任务-类和对象
- C02-程序设计基础提高班(C++)第6周上机任务-数组
- C02-程序设计基础提高班(C++)第5周上机任务
- C02-程序设计基础提高班(C++)第6周上机任务-数组
- C02-程序设计基础提高班(C++)第6周上机任务-数组