课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759。内有完整教学方案及资源链接

【项目1 - 点、圆的关系】
(1)先建立一个Point(点)类。包括数据成员x,y(坐标点);
(2)以Point为基类。派生出一个Circle(圆)类,添加数据成员(半径)。基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出)。
(4)定义友元函数int locate,推断点p与圆的位置关系(返回值<0圆内。==0圆上,>0 圆外);

int main( )
{
Circle c1(3,2,4),c2(4,5,5); //c2应该大于c1
Point p1(1,1),p2(3,-2),p3(7,3); //分别位于c1内、上、外 cout<<"圆c1: "<<c1; cout<<"点p1: "<<p1;
cout<<"点p1在圆c1之"<<((locate(p1, c1)>0)? "外":((locate(p1, c1)<0)?"内":"上"))<<endl; cout<<"点p2: "<<p2;
cout<<"点p2在圆c1之"<<((locate(p2, c1)>0)?"外":((locate(p2, c1)<0)?"内":"上"))<<endl; cout<<"点p3: "<<p3;
cout<<"点p3在圆c1之"<<((locate(p3, c1)>0)?"外":((locate(p3, c1)<0)?"内":"上"))<<endl;
return 0;
}

參考解答:

#include <iostream>
#include<Cmath>
using namespace std;
class Point
{
public:
Point(double a=0,double b=0):x(a),y(b) {} //构造函数
double distance(const Point &p) const; //求距离
friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<”
protected: //受保护成员
double x,y;
}; double Point::distance(const Point &p) const //求距离
{
double dx = x-p.x;
double dy = y-p.y;
return sqrt(dx*dx+dy*dy);
} ostream & operator<<(ostream &output,const Point &p)
{
output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
} class Circle:public Point //circle是Point类的公用派生类
{
public:
Circle(double a=0,double b=0,double r=0) :Point(a,b),radius(r) { }; //构造函数
friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<”
friend int locate(const Point &p, const Circle &c); //推断点p在圆上、圆内或圆外。返回值:<0圆内,==0圆上。>0 圆外
protected:
double radius;
}; //重载运算符“<<”,使之按规定的形式输出圆的信息
ostream &operator<<(ostream &output,const Circle &c)
{
output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius<<endl;
return output;
} //推断点p在圆内、圆c内或圆c外
int locate(const Point &p, const Circle &c)
{
const Point cp(c.x,c.y); //圆心
double d = cp.distance(p);
if (abs(d - c.radius) < 1e-7)
return 0; //相等
else if (d < c.radius)
return -1; //圆内
else
return 1; //圆外
} int main( )
{
Circle c1(3,2,4);
Point p1(1,1),p2(3,-2),p3(7,3); //分别位于c1内、上、外 cout<<"圆c1: "<<c1; cout<<"点p1: "<<p1;
cout<<"点p1在圆c1之"<<((locate(p1, c1)>0)? "外":((locate(p1, c1)<0)? "内":"上"))<<endl; cout<<"点p2: "<<p2;
cout<<"点p2在圆c1之"<<((locate(p2, c1)>0)?"外":((locate(p2, c1)<0)?"内":"上"))<<endl; cout<<"点p3: "<<p3;
cout<<"点p3在圆c1之"<<((locate(p3, c1)>0)?"外":((locate(p3, c1)<0)?"内":"上"))<<endl;
return 0;
}

=================== 迂者 贺利坚 CSDN博客专栏=================
|== IT学子成长指导专栏 专栏文章的分类文件夹(不定期更新) ==|
|== C++ 课堂在线专栏  贺利坚课程教学链接(分课程年级) ==|
|== 我写的书——《逆袭大学——传给IT学子的正能量》    ==|
===== 为IT菜鸟起飞铺跑道,和学生一起享受快乐和激情的大学 =====

C++第13周(春)项目1 - 点、圆的关系的更多相关文章

  1. 2013级C++第13周(春)项目——继承的进一步话题与GUI应用开发

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 第一部分 程序阅读:阅读以下类的定义,请说出在 ...

  2. 2013级C++第15周(春)项目——输入输出流及文件文件操作

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 本周程序阅读及程序调试中须要的文件,请到htt ...

  3. C++第11周(春)项目2 - 职员有薪水了

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目2 - 职员有薪水了]定义一个名为CPe ...

  4. C++第15周(春)项目3 - OOP版电子词典(一)

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序中须要的相 ...

  5. C++第11周(春)项目1 - 存储班长信息的学生类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目1 - 存储班长信息的学生类] clas ...

  6. 2013级C++第14周(春)项目——多态性、虚函数和抽象类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 第一部分 阅读程序1.阅读.改动和执行关于交通 ...

  7. C++第15周(春)项目2 - 用文件保存的学生名单

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 本程序中须要的相关文件.请到http://pa ...

  8. C++第12周(春)项目2 - &quot;双肩挑&quot;教师

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目2 - 教师兼干部类](第11章习题9) ...

  9. C++第15周(春)项目3 - OOP版电子词典(二)

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序须要的相关 ...

随机推荐

  1. Effective C++ 第二版 10) 写operator delete

    条款10 写了operator new就要同时写operator delete 写operator new和operator delete是为了提高效率; default的operator new和o ...

  2. HDU 4727 The Number Off of FFF

    The Number Off of FFF Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  3. [译]SSRS 编写带参数的MDX报表

    编写MDX报表长久以来对于报表人员来说都比较痛苦. 当然如果你用查询设计器(Query Designer) 直接拖拉数据集那就很方便,但是你们有没有想过查询设计器是怎么创建MDX的.或者创建的参数是如 ...

  4. runtime的概念,message send如果寻找不到相应的对象,如何进行后续处理

    运行时刻是指一个程序在运行(或者在被执行)的状态.也就是说,当你打开一个程序使它在电脑上运行的时候,那个程序就是处于运行时刻.在一些编程语言中,把某些可以重用的程序或者实例打包或者重建成为“运行库”. ...

  5. BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节( 单调栈 )

    维护一个h严格递减的栈 , 出栈时计算一下就好了.. ------------------------------------------------------------------------- ...

  6. 安装CDH

    Cloudera Manager 4.8.2 http://www.idefs.com/recordsubuntu-12-04-cloudera-installation-manager-4-8-2- ...

  7. QT通过IP地址定位地址(用get方法取数据)

    通过IP地址定位地址,是要通过查询数据库,如果自己做一个这样的数据库工作量就比较大,所以在网上找了一个查询IP地址的网址,通过调用这个网址查询来实现,但是这个有一定的弊端,如果没有网络或者这个网址不可 ...

  8. hibernate 单元测试 5.2

     单元测试 测试 dao service action package com.kaishengit.test; import org.hibernate.Session; import com.ka ...

  9. 关于异或(Xor)的一点笔记

    因为博弈论里,尤其实在求sg函数时,经常会用到异或运算,所以我就把网上搜到的一些相关知识和自己的一些理解记下来. 如果出现差错,还请指出,谢谢! 异或:可以简称Xor,可以用数学符号⊕表示,计算机就一 ...

  10. Girls' research(manacher)

    Girls' research Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...