第十二周C++【任务4】类的组合与继承。


【任务4】类的组合与继承
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要的输入输出函数
(4)定义友元函数int locate,判断点p在圆c上、圆c内或圆c外,返回值<0圆内,==0圆上,>0 圆外;
(5)重载关系运算符(6种)运算符,使之能够按圆的面积比较两个圆的大小;
(6)给定一点p,求出该点与圆心相连成的直线与圆的两个交点并输出
//自行定义类

//用下面的main()函数测试
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;
}

 

 

#include <iostream>   
  
const double  PI = 3.1415926  ;
  
#include<Cmath>   
  
using namespace std;  
class point
{
public:
	point() {x=0;y=0;}
	~point();
	point (double x0,double y0){x=x0;y=y0;}
	void setx(double x0){x=x0;}
	void sety(double y0){y=y0;} 
	double getx(){return x;}
	double gety(){return y;}
	friend ostream & operator << (ostream &,point &);
protected:
	double x,y;

};
class Circle: public point
{
public:
	Circle(){r=0;}
	Circle(double x,double y,double r);
	~Circle();
	double setr(double r0){r=r0;}
	double getr(){return r;}
	friend ostream & operator << (ostream &,Circle &);
	friend double locate(point &p,Circle &c);
	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);
	double area0();
protected:
	double r;
};
point::~point(){}
Circle::~Circle(){}
ostream & operator <<(ostream &output,point &c)
{
	output<<"("<<c.x<<",  "<<c.y<<")"<<endl;
	return output;
}
ostream & operator << (ostream &output,Circle &c)
{
	output << "以" << "(" << c.getx() << "," << c.gety() << ")" << "为圆心, "  << "以" << c.r << "为半径的圆" << endl;        
	return output;
}
Circle::Circle(double x,double y,double r0):point(x,y),r(r0){}

double Circle::area0()
{
	double s;
	s=PI*r*r;
	return s;
}
bool Circle::operator > (Circle &t)
{
	if(area0()>t.area0())
		return true;
	else
		return false;
}

bool Circle::operator < (Circle &t)
{
	if(area0()<t.area0())
		return true;
	else
		return false;
}
bool Circle::operator >= (Circle &t)
{
	if(area0()<t.area0())
		return false;
	else
		return true;
}
bool Circle::operator <= (Circle &t)
{
	if(area0()>t.area0())
		return false;
	else
		return true;
}
bool Circle::operator == (Circle &t)
{
	if((area0() >= t.area0())&&(area0() <= t.area0()))
		return true;
	else
		return false;
}
bool Circle::operator != (Circle &t)
{
	if(area0()>t.area0()||area0()<t.area0())
		return true;
	else
		return false;
}
double locate (point &p, Circle &c)
{
	double  d;
	double f;
	d = (p.getx()-c.getx()) *(p.getx()-c.getx()) +(p.gety()-c.gety()) *(p.gety()-c.gety());
	f = d-c.getr()*c.getr();
	return f;
}
void crossover_point1(point & p1, Circle & c1, point & p4, point & p5)    
{    
     double d;  
    double x0 = (c1.getx() - p1.getx()) * c1.r / sqrt((c1.getx() - p1.getx()) * (c1.getx() - p1.getx()) + (c1.gety() - p1.gety()) * (c1.gety() - p1.gety()));    
    double y0 = (c1.gety() - p1.gety()) * c1.r / sqrt((c1.getx() - p1.getx()) * (c1.getx() - p1.getx()) + (c1.gety() - p1.gety()) * (c1.gety() - p1.gety()));    
    
    d = c1.getx() + x0;   
    p4.setx(d);  
    d = c1.gety() + y0;  
    p4.sety(d);  
    
    d = c1.getx() - x0;   
    p5.setx(d);  
    d = c1.gety() - y0;  
    p5.sety(d);  
}


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;  
}  


 

 

 

 

 

 

积累经验:求点和一个圆的交点

x = x0 +/- (x1 - x0)*r/sqrt((x1 - x0)^2 + (y1 - y0)^2)
y = y0 +/- (y1 - y0)*r/ sqrt((x1 - x0)^2 + (y1 - y0)^2)
其中,(x0, y0)是圆心坐标,(x1, y1)是已知点坐标,r是半径

 

 friend double locate(point &p,Circle &c);

友元函数在定义时不可以加类名和作用域名。
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值