求助大神,在已有的Point类的基础上,定义一个“Circle”派生类,要求:新增一个半径成员;能计算并输出圆的周长及加圆面积
在已有的Point类的基础上,定义一个“Circle”派生类,要求:新增一个半径成员;能计算并输出圆的周长及加圆面积,用C++怎么写,我会写Circle类的可是不知道怎么使用继承
2017-05-30 21:00
2017-05-30 21:28
2017-05-30 21:30
程序代码:struct Point
{
double x, y;
Point() : x(), y()
{
}
Point( double x, double y ) : x(x), y(y)
{
}
};
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
struct Circle : protected Point
{
public:
Circle() : Point(), radius()
{
}
Circle( double x, double y, double r ) : Point(x,y), radius(r)
{
}
double perimeter() const
{
return M_PI*radius*2;
}
double area() const
{
return M_PI*radius*radius;
}
protected:
double radius;
};
#include <iostream>
using namespace std;
int main( void )
{
Circle circle( 10, 10, 100 );
cout << "perimeter = " << circle.perimeter() << '\n'
<< "area = " << circle.area() << endl;
return 0;
}
2017-05-31 08:20