问题及代码:
/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:lily.cpp
*作者:李莉
*完成日期:2015年5月18日
*版本号:v1.0
*
*问题描述:先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试
以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试
*程序输入:无输入
*程序输出:如结果图
*/
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(double x0,double y0):x(x0),y(y0) {}
void setPoint(double a,double b);
double getX();
double getY();
void display();
public:
double x;
double y;
};
void Point::setPoint(double a,double b)
{
x=a;
y=b;
}
double Point::getX()
{
return x;
}
double Point::getY()
{
return y;
}
void Point::display()
{
cout<<"Point:"<<"("<<x<<","<<y<<")"<<endl;
}
class Circle:public Point
{
public:
Circle(double x0,double y0,double r0);
void setR(double r0);
double getarea();
void display1();
protected:
double r;
};
Circle::Circle(double x0,double y0,double r0):Point(x0,y0),r(r0){}
void Circle::setR(double r0)
{
r=r0;
}
double Circle::getarea()
{
return 3.14159*r*r;
}
void Circle::display1()
{
display();
cout<<"Circle's x="<<x<<" "<<"y="<<y<<" "<<"radius="<<r<<" "<<"area="<<getarea()<<endl;
}
int main()
{
Circle p(1.2,5.2,2.0);
cout<<"The message of the Circle is:"<<endl;
p.display1();
return 0;
}
运行结果:
心得体会:
原谅我实在不知道设计的那个点的坐标有什么用。。。。