/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:类的组合与继承
* 作 者: 雷恒鑫
* 完成日期: 2012 年 04月 29 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:由下面派生类Student1对基类Student的继承……
* 问题描述:(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;(3)编写上述两类中的构造、析构函数及必要的输入输出函数(4)定义友元函数int locate,判断点p在圆c上、圆c内或圆c外,返回值<0圆内,==0圆上,>0
圆外;(5)重载关系运算符(6种)运算符,使之能够按圆的面积比较两个圆的大小;(6)给定一点p,求出该点与圆心相连成的直线与圆的两个交点并输出//自行定义类
* 程序输出:
* 程序头部的注释结束
*/
#include<iostream>
#include<Cmath>
using namespace std;
const double pi=3.1415926;
class Point //定义坐标点类
{
public:
Point(){x=0;y=0;}
Point(double x0,double y0) {x=x0; y=y0;}
~Point ()
{
cout<<"Point类析构函数执行完毕(Destructor function performs finished)"<<endl;
}
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){}//构造函数
~Circle()
{
cout<<"Circle类析构函数执行完毕(Destructor function performs finished)"<<endl;
}
friend ostream &operator << (ostream & output, Circle & c);
friend double locate(Point &,Circle &);
friend void crossover_point1(Point &p1,Circle &c1,Point &p4,Point &p5);
//double get_d(){return d;}
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 ()+((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 ()-((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 ()+((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 ()-((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应该大于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;
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;
}
运行结果:
以上程序有一点小小的问题:
void crossover_point1(Point &p1,Circle &c1,Point &p4,Point &p5)
{
double n;
n=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 ()-((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 ()+((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 ()-((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);
}
应该这样改一下:
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);
}
虽然运行结果一样,但是还是改后的程序比较鲁棒。
以下是改后的源代码:
#include<iostream>
#include<Cmath>
using namespace std;
const double pi=3.1415926;
class Point //定义坐标点类
{
public:
Point(){x=0;y=0;}
Point(double x0,double y0) {x=x0; y=y0;}
~Point ()
{
cout<<"Point类析构函数执行完毕(Destructor function performs finished)"<<endl;
}
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){}//构造函数
~Circle()
{
cout<<"Circle类析构函数执行完毕(Destructor function performs finished)"<<endl;
}
friend ostream &operator << (ostream & output, Circle & c);
friend double locate(Point &,Circle &);
friend void crossover_point1(Point &p1,Circle &c1,Point &p4,Point &p5);
//double get_d(){return d;}
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应该大于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;
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;
}
运行结果:
分享到:
相关推荐
Java程序设计课程实验9-继承和抽象类Java程序设计课程实验9-继承和抽象类Java程序设计课程实验9-继承和抽象类Java程序设计课程实验9-继承和抽象类Java程序设计课程实验9-继承和抽象类Java程序设计课程实验9-继承和...
C++面向对象程序设计实验3继承与派生 需要的哦朋友可以下载
实验12--继承与派生(实验思考答案).doc
面向对象原理与Java实践课程实验-继承与接口
本资源为C++实验1-13,包含完整程序、注释、报告非常适合初学者学习、研究,具体如下: 实验1-熟悉Visual C++编程环境 实验2-利用函数编程 实验3-指针与引用的编程 ...实验12-利用文件编程 实验13- 面向对象综合编程
天轰穿系列教程之-28类--继承天轰穿系列教程之-28类--继承天轰穿系列教程之-28类--继承天轰穿系列教程之-28类--继承天轰穿系列教程之-28类--继承天轰穿系列教程之-28类--继承天轰穿系列教程之-28类--继承天轰穿系列...
C++实验报告--继承和派生.pdf
java 实验 继承与多态rectAngle 定义矩形类,用户输入矩形的长与宽,程序计算其面积和周长;派生子类正方形类,定义一个接口Printable源代码
c++实验8-继承与派生上机练习题.doc
武汉理工大学-Java面向对象与多线程综合实验-(1)封装、继承与多态(IDEA实现)
实验4-6.继承的相关实验
C++课程实验指导书 目 录 第一部分 VC++开发环境 1 一、Visual C++6.0开发环境简介 1 二、控制台应用程序的创建 5 三、程序调试 9 第二部分 C++实验项目 14 实验一 基本数据类型与输入输出 14 实验二 表达式和语句 ...
第十二章Python继承和多态习题与答案--中文
是关于Java学习类的继承方面的内容,大家可以根据这个实验更加理解继承。
实验代码 实验报告 心得 相关习题回答
华中科技大学C++课程实验报告-继承对象的整型队列编程
c++类的继承与派生--实验报告.doc
全部为C++代码 包含面向对象程序设计实验lab1、lab2、lab3、lab4、lab5、lab6、lab7、lab8、lab9、lab10 分别是:1-实验一-通讯录条目非oop版 2-实验二-通讯录条目oop版 ...10-实验十 -复数类-重载运算符
《Android移动应用开发》 实验指导书 课程代码: 总课时数: 适用专业: 院(系)名称: Android移动应用开发实验指导书全文共13页,当前为第1页。 Android移动应用开发实验指导书全文共13页,当前为第1页。 实验一...