/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称 :
*作 者 : 徐聪
*完成日期 : 2016年5月9号
*版 本 号 : v1.0
*问题描述:以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试;
*/
#include<iostream>
#include<cmath>
using namespace std;
#define PI 3.1415926
class Point
{
public:
void setPoint(float i,float j);
float getX(){return x;}
float getY(){return y;}
float dr(Point &p1,Point &p2);
private:
float x;
float y;
};
void Point::setPoint(float i,float j)
{
x=i;
y=j;
}
float Point::dr(Point &p1,Point &p2)
{
float dr=0,dx=0,dy=0;
dx=p1.x-p2.x;
dy=p1.y-p2.y;
dr=sqrt(dx*dx+dy*dy);
return dr;
}
class Circle :public Point
{
public:
Circle(float x1,float y1,float x2,float y2){midpoint.setPoint(x1,y1);aroundpoint.setPoint(x2,y2);}
float setR();
void circle_area();
float getR(){return r;}
private:
float area;
float r;
Point midpoint;
Point aroundpoint;
};
float Circle::setR()
{
r=dr(midpoint,aroundpoint);
return r;
}
void Circle::circle_area()
{
r=setR();
area=PI*r*r;
cout<<"圆的面积为:"<<area<<endl;
}
int main()
{
Circle c1(1,2,3,4);
c1.circle_area();
return 0;
}
运行结果