/*
【任务4】类的组合与继承
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要的输入输出函数
(4)定义友元函数int locate,判断点p在圆c上、圆c内或圆c外,返回值<0圆内,==0圆上,>0 圆外;
(5)重载关系运算符(6种)运算符,使之能够按圆的面积比较两个圆的大小;
(6)给定一点p,求出该点与圆心相连成的直线与圆的两个交点并输出
*/
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: vehicle.cpp
* 作 者: 计114-3 王兴锋
* 完成日期: 2012 年 5 月 7 日
* 版 本 号: V 1.0
*/
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.1415926
//自行定义类
class Point //定义坐标点类
{
public:
double x, y;//点的横坐标和纵坐标
Point(){x = 0; y = 0;}
~Point(){}
Point(double x0, double y0) {x = x0; y = y0;}
void setXY(double x0, double y0){x = x0; y = y0;}
double getX(){return x;}
double getY(){return y;}
friend ostream& operator << (ostream&, Point&);
friend istream& operator >> (istream&, Point&);
};
ostream& operator << (ostream& output, Point& p)
{
output << "(" << p.x << "," << p.y << ")" << endl;
return output;
}
istream& operator >> (istream& input, Point& p)
{
cout << "输入点的横纵坐标:(x y)";
input >> p.x >> p.y;
return input;
}
class Circle : public Point
{
protected:
double r;
public:
Circle(){}
Circle(double x, double y, double r = 0):Point(x, y){this->r = r;}
~Circle(){}
void setR(double r){this->r = r;}
double getR(){return r;}
double getL(){return 2*PI*r;}
double getAre(){return PI*r*r/2;}
friend ostream& operator << (ostream&, Circle&);
friend istream& operator >> (istream&, Circle&);
friend double locate(Point p, Circle c);
bool operator > (Circle &c);
bool operator < (Circle &c);
bool operator >= (Circle &c);
bool operator <= (Circle &c);
bool operator == (Circle &c);
bool operator != (Circle &c);
void crossover_point1(Point p, Circle c, Point &p1, Point &p2);
};
void crossover_point1(Point p, Circle c, Point &p1, Point &p2)
{
double len = c.getR()/(locate(p, c) + c.getR());
double x = (p.x-c.x)*len;
double y = (p.y-c.y)*len;
p1.x = c.x + x;
p1.y = c.y + y;
p2.x = c.x - x;
p2.y = c.y - y;
}
bool Circle::operator > (Circle &c)
{
if (getAre() > c.getAre())
return true;
else
return false;
}
bool Circle::operator < (Circle &c)
{
if (getAre() < c.getAre())
return true;
else
return false;
}
bool Circle::operator >= (Circle &c)
{
if (getAre() >= c.getAre())
return true;
else
return false;
}
bool Circle::operator <= (Circle &c)
{
if (getAre() <= c.getAre())
return true;
else
return false;
}
bool Circle::operator == (Circle &c)
{
if (getAre() == c.getAre())
return true;
else
return false;
}
bool Circle::operator != (Circle &c)
{
if (getAre() != c.getAre())
return true;
else
return false;
}
double locate(Point p, Circle c)
{
return (sqrt((c.x - p.x)*(c.x - p.x) + (c.y - p.y)*(c.y - p.y)) - c.r);
}
ostream& operator << (ostream& out, Circle& c)
{
out << "圆心:" << "(" << c.x << "," << c.y << ")" << endl;
out << "半径:" << c.r << endl;
return out;
}
istream& operator >> (istream& in, Circle& c)
{
cout << "输入圆心坐标:(x y)";
in >> c.x >> c.y;
cout << "请输入圆的半径:(r)" ;
in >> c.r;
return in;
}
//用下面的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;
}
/*
圆c1: 圆心:(3,2)
半径:4
点p1: (1,1)
点p1在圆c1之内
点p2: (3,-2)
点p2在圆c1之上
点p3: (7,3)
点p3在圆c1之外
圆c1: 圆心:(3,2)
半径:4
小于
小于等于
不等于
圆c2: 圆心:(3,2)
半径:4
点p1: (1,1)
与圆c1: 圆心:(3,2)
半径:4
的圆心相连,与圆交于两点,分别是:
交点: (-0.577709,0.211146)
交点: (6.57771,3.78885)
请按任意键继续. . .
*/
分享到:
相关推荐
本资源是C++继承与派生类实验报告,欢迎大家下载阿!
C++继承与多态性实验报告
C++ 程序设计实验报告----车辆信息管理系统
C++实验报告--继承和派生.pdf
C++实验五 继承与派生的应用 课程 实验报告 作业参考的良品!
c++类的继承与派生--实验报告.doc
Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-C++Dev-...
c++程序设计 继承与派生实验(二) 1. 进一步掌握类的派生与继承的概念、应用方法 2. 掌握继承方式对成员访问权限的影响 3. 掌握虚基类的用法
华中科技大学C++课程实验报告-继承对象的整型队列编程
本资源为C++实验1-13,包含完整程序、注释、报告非常适合初学者学习、研究,具体如下: 实验1-熟悉Visual C++编程环境 实验2-利用函数编程 实验3-指针与引用的编程 实验4-类与对象的编程 实验5-构造函数与析构函数的...
C++实验课程书中的实验------小小图书馆源代码下载
C++大型实验报告--图书管理系统.docx
c++程序设计语言---7-继承.ppt
多重继承 面向对象 实验报告
c++派生类与继承实验报告
C++学生信息管理系统实验报告-示例.docx
C++上机实验报告-实验六全文共11页,当前为第1页。C++上机实验报告-实验六全文共11页,当前为第1页。实验六 多态性 C++上机实验报告-实验六全文共11页,当前为第1页。 C++上机实验报告-实验六全文共11页,当前为第1...
实验五 继承与派生 C++上机实验报告-实验五全文共7页,当前为第1页。 C++上机实验报告-实验五全文共7页,当前为第1页。 实验目的 1.学习定义和使用类的继承关系,定义派生类 2.熟悉不同继承方式下对基类成员的访问...
C++高级程序设计实验实验八-派生与继承 实验目的: 1.理解多重派生的定义; 2.理解多重派生中构造函数与析构函数的调用顺序; 3.理解多重派生中虚拟基类的作用; 实验内容: 1.理解下面的程序,并在VS下运行查看...
C++-GUI-Programming-with-Qt-4-1st-ed.pdf 英文第一版,正版,非扫描版。 几乎描述了所有QT控件的试用方法,从简单到复杂,通俗易懂。前提:C++过关,C++没过关的同学请先学习C++,不要被此书挫败。