/*
* 程序的版权和版本声明部分
* Copyright (c)2014, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称:a.cpp
* 作 者:孔云
* 完成日期:2014年5月25日
* 版 本 号: v1.0
* 输入描述:主函数已给出。
* 问题描述:(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出);
(4)设计一种方案,输出给定一点p与圆心相连成的直线与圆的两个交点。
*/
#include <iostream>
#include <Cmath>
using namespace std;
class Circle;//提前声明Point类中提到的Circle类
class Point
{
protected:
double x,y;
public:
Point():x(0),y(0) {}//构造函数
Point(double,double);//赋值函数
friend ostream&operator<<(ostream&,Point&);//输出运算符重载函数
friend void jiaodian(Point&p1,Circle&c,Point&p2,Point&p3);
~Point();//析构函数
};
Point::Point(double a,double b)
{
x=a;
y=b;
}
ostream&operator<<(ostream& out,Point& c)
{
out<<"("<<c.x<<","<<c.y<<")";
return out;
}
Point::~Point() {}
class Circle:public Point
{
protected:
double r;
public:
Circle():Point(),r(0) {}
Circle(double,double,double);
~Circle();
friend ostream&operator<<(ostream&,Circle&);//输出运算符重载
friend void jiaodian(Point&p1,Circle&c,Point&p2,Point&p3);
};
Circle::Circle(double a,double b,double c):Point(a,b),r(c) {}
Circle::~Circle() {}
void jiaodian(Point&p,Circle&c,Point&p1,Point&p2)
{
p1.x = (c.x + sqrt(c.r*c.r/(1+((c.y-p.y)/(c.x-p.x))*((c.y-p.y)/(c.x-p.x)))));
p2.x = (c.x - sqrt(c.r*c.r/(1+((c.y-p.y)/(c.x-p.x))*((c.y-p.y)/(c.x-p.x)))));
p1.y = (p.y + (p1.x -p.x)*(c.y-p.y)/(c.x-p.x));
p2.y = (p.y + (p2.x -p.x)*(c.y-p.y)/(c.x-p.x));
}
ostream&operator<<(ostream& out,Circle& k)
{
out<<"圆心:("<<k.x<<","<<k.y<<"),半径"<<k.r;
return out;
}
int main()
{
Circle c1(3,2,4);
Point p1(1,1),p2,p3;
jiaodian(p1,c1,p2,p3);
cout<<"点p1: "<<p1;
cout<<"与圆c1: "<<c1;
cout<<"的交点分别是:"<<endl;
cout<<"交点1:"<<p2<<endl;
cout<<"交点2:"<<p3<<endl;
return 0;
}
心得体会:为什么将基类的数据成员设置私有权限然后建立其接口函数做起来不行呢,那个大神告诉额,为何?