C++第十二周【任务4】类的组合与继承
来源:互联网 发布:酷狗直播伴侣for mac 编辑:程序博客网 时间:2024/06/03 09:01
/** 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved.* 文件名称:C++第十二周【任务4】 * 作 者: 李洪悬 * 完成日期: 2012 年 5 月 8 日
* 对任务及求解方法的描述部分* 输入描述:圆的基本数据
* 问题描述:类的组合与继承
* 程序输出:圆的各种信息
*/
【任务4】类的组合与继承(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;(3)编写上述两类中的构造、析构函数及必要的输入输出函数(4)定义友元函数int locate,判断点p在圆c上、圆c内或圆c外,返回值<0圆内,==0圆上,>0 圆外;(5)重载关系运算符(6种)运算符,使之能够按圆的面积比较两个圆的大小;(6)给定一点p,求出该点与圆心相连成的直线与圆的两个交点并输出
BB平台的代码:
//用下面的main()函数测试
int main( ){Circle c1(3,2,4),c2(4,5,5); //c2应该大于c1Point 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;cout<<endl; cout<<"圆c1: "<<c1;if(c1>c2) cout<<"大于"<<endl;if(c1<c2) cout<<"小于"<<endl; if(c1>=c2) cout<<"大于等于"<<endl;if(c1<=c2) cout<<"小于等于"<<endl; if(c1==c2) cout<<"等于"<<endl; if(c1!=c2) cout<<"不等于"<<endl; cout<<"圆c2: "<<c1;cout<<endl; Point p4,p5;crossover_point1(p1,c1, p4, p5);cout<<"点p1: "<<p1;cout<<"与圆c1: "<<c1;cout<<"的圆心相连,与圆交于两点,分别是:"<<endl;cout<<"交点: "<<p4;cout<<"交点: "<<p5;cout<<endl; system("pause");return 0;}
程序源代码:
#include<iostream>#include<Cmath>using namespace std;const double pi=3.14; class Point //定义坐标点类 { public: Point(){x=0;y=0;} Point(double x0,double y0){x=x0;y=y0;} double get_x(){return x;} double get_y(){return y;} void set_x(double n){ x=n;} void set_y(double n){y=n;} friend ostream &operator << (ostream & output, Point & c); private: double x,y; //点的横坐标和纵坐标 }; ostream &operator << (ostream & output, Point & c) { output<<"该点的横坐标为:"<<c.x<<" "<<"纵坐标为:"<<c.y<<endl; return output; } class Circle: public Point //利用坐标点类定义圆类, 其基类的数据成员表示圆的中心 { public: Circle(double xx,double yy,double dd): Point(xx,yy) ,d(dd){}//构造函数 friend ostream &operator << (ostream & output, Circle & c); friend double locate(Point &,Circle &); friend void crossover_point1(Point &p1,Circle &c1,Point &p4,Point &p5); bool operator > (Circle &t); bool operator < (Circle &t); bool operator >= (Circle &t); bool operator <= (Circle &t); bool operator == (Circle &t); bool operator != (Circle &t); private: double d; }; void crossover_point1(Point &p1,Circle &c1,Point &p4,Point &p5) { double n; n=c1.get_x()+(sqrt((p1.get_x()-c1.get_x())*(p1.get_x()-c1.get_x ()))*c1.d )/(sqrt((p1.get_x ()-c1.get_x ())*(p1.get_x ()-c1.get_x ())+((p1.get_y ()-c1.get_y ())*(p1.get_y ()-c1.get_y ())))); p4.set_x (n); n=c1.get_x ()-(sqrt((p1.get_x ()-c1.get_x ())*(p1.get_x ()-c1.get_x ()))*c1.d )/(sqrt((p1.get_x ()-c1.get_x ())*(p1.get_x ()-c1.get_x ())+((p1.get_y ()-c1.get_y ())*(p1.get_y ()-c1.get_y ())))); p5.set_x (n); n=c1.get_y ()+(sqrt((p1.get_y ()-c1.get_y ())*(p1.get_y ()-c1.get_y ()))*c1.d )/(sqrt((p1.get_x ()-c1.get_x ())*(p1.get_x ()-c1.get_x ())+((p1.get_y ()-c1.get_y ())*(p1.get_y ()-c1.get_y ())))); p4.set_y (n); n=c1.get_y ()-(sqrt((p1.get_y ()-c1.get_y ())*(p1.get_y ()-c1.get_y ()))*c1.d )/(sqrt((p1.get_x ()-c1.get_x ())*(p1.get_x ()-c1.get_x ())+((p1.get_y ()-c1.get_y ())*(p1.get_y ()-c1.get_y ())))); p5.set_y (n); } ostream &operator << (ostream & output, Circle & c) { output<<"圆的半径为:"<<c.d<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl; return output; } double locate(Point &p,Circle &c) { double s,d,m; s=(c.get_x()-p.get_x () )*(c.get_x()-p.get_x () )+(c.get_y ()-p.get_y () )*(c.get_y ()-p.get_y () ); m=sqrt(s); d=m-c.d ; return d; } bool Circle::operator > (Circle &t) { double s1,s2; s1=pi*d*d; s2=pi*t.d*t.d; if(s1>s2 ) return true; else return false; } bool Circle::operator < (Circle &t) { double s1,s2; s1=pi*d*d; s2=pi*t.d*t.d; if(s1<s2 ) return true; else return false; } bool Circle::operator >= (Circle &t) { double s1,s2; s1=pi*d*d; s2=pi*t.d*t.d; if (s1<s2) return false; return true; } bool Circle::operator <= (Circle &t) { double s1,s2; s1=pi*d*d; s2=pi*t.d*t.d; if (s1>s2) return false; return true; } bool Circle::operator == (Circle &t) { double s1,s2; s1=pi*d*d; s2=pi*t.d*t.d; if (s1<s2) return false; if (s1>s2) return false; return false; } bool Circle::operator != (Circle &t) { double s1,s2; s1=pi*d*d; s2=pi*t.d*t.d; if (s1==s2) return false; return true; } int main( ){Circle c1(3,2,4),c2(4,5,5); //c2应该大于c1Point 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;cout<<endl; cout<<"圆c1: "<<c1;if(c1>c2) cout<<"大于"<<endl;if(c1<c2) cout<<"小于"<<endl; if(c1>=c2) cout<<"大于等于"<<endl;if(c1<=c2) cout<<"小于等于"<<endl; if(c1==c2) cout<<"等于"<<endl; if(c1!=c2) cout<<"不等于"<<endl; cout<<"圆c2: "<<c1;cout<<endl; Point p4,p5;crossover_point1(p1,c1, p4, p5);cout<<"点p1: "<<p1;cout<<"与圆c1: "<<c1;cout<<"的圆心相连,与圆交于两点,分别是:"<<endl;cout<<"交点: "<<p4;cout<<"交点: "<<p5;cout<<endl; system("pause");return 0;}
运行结果:
圆c1: 圆的半径为:4圆的圆心为(3,2)
点p1: 该点的横坐标为:1 纵坐标为:1
点p1在圆c1之内
点p2: 该点的横坐标为:3 纵坐标为:-2
点p2在圆c1之上
点p3: 该点的横坐标为:7 纵坐标为:3
点p3在圆c1之外
圆c1: 圆的半径为:4圆的圆心为(3,2)
小于
小于等于
不等于
圆c2: 圆的半径为:4圆的圆心为(3,2)
点p1: 该点的横坐标为:1 纵坐标为:1
与圆c1: 圆的半径为:4圆的圆心为(3,2)
的圆心相连,与圆交于两点,分别是:
交点: 该点的横坐标为:6.57771 纵坐标为:3.78885
交点: 该点的横坐标为:-0.577709 纵坐标为:0.211146
请按任意键继续. . .
经验积累:理清思路,自顶向下,逐步求精!
- 第十二周C++【任务4】类的组合与继承。
- C++第十二周【任务4】类的组合与继承
- 第十二周任务四类的组合与继承
- 第十二周任务四之类的组合与继承
- 第十二周实验指导--任务四--类的组合与继承
- 《C++第十二周实验报告4-1》---类的组合与继承
- 第十二周 任务二 类的多重继承
- 第十二周任务二 多重继承类
- 第十二周C++【任务1】理解基类中成员的访问限定符和派生类的继承方式
- Objective-C的继承与组合
- 类的继承与组合
- 类的继承与组合
- 类的组合与继承
- 类的继承与组合
- 类的继承与组合
- 类的组合与继承
- 类的继承与组合
- 第十二周任务1:理解成员的访问限定符和派生类的继承方式
- 到底哪里出了问题?
- 数组的循环右移
- 数据库多值询
- GCC使用
- validator框架
- C++第十二周【任务4】类的组合与继承
- 小论万花筒法则(二)
- Entity Framework 教程
- GDB使用入门
- 如何制作高质量的PPT
- makefile入门
- 2012C++程序设计实验报告【12.3】
- 关于性能测试中的并发
- Java文件读写操作大全