应用题
请使用VC6或使用【答题】菜单打开proj2下的工程proj2。其中有类Point(“点”)、Rectangle(“矩形”)和Circle(“圆”)的定义。在程序所使用的平面坐标系统中,x轴的正方向是水平向右的,y轴的正方向是竖直向下的。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应该是:
--圆形----------
圆心=(3,2)
半径=1
面积=3.14159
--外切矩形----------
左上角=(2,1)
右下角=(4,3)
面积=4
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
#include <cmath>
using namespace std;
//平面坐标中的点
//本题坐标系统中,x轴的正方向水平向右,y轴的正方向竖直向下。
class Point {
public:
Point(double x=0.0, double y=0.0): x_(x), y_(y) {}
double getX() const {return x_;}
double getY() const {return y_;}
void setX(double x) {x_=x;}
void setY(double y) {y_=y;}
private:
double x_; //x坐标
double y_; //y坐标
};
//矩形
class Rectangle {
public:
Rectangle (Point p, int w, int h)
: point (p), width (w), height (h) {}
double area() const //矩形面积
{
return width * height;
}
Point topLeft() const //左上角顶点
{
return point;
}
Point bottomRight() const
//右下角顶点(注:y轴正方向竖直向下)
{
// **********found**********
return Point(______);
}
private:
Point point; //左上角顶点
double width; //水平边长度
double height; //垂直边长度
};
//圆形
class Circle {
public:
Circle (Point p, double r):center(p), radius(r) {}
Rectangle boundingBox() const; //外切矩形
double area() const //圆形面积
{
// **********found**********
return PI *______;}
public:
static const double PI; //圆周率
private:
Point center; //圆心
double radius; //半径
};
const double Circle::PI = 3.14159;
Rectangle Circle::boundingBox() const
{
// **********found**********
Point pt(______);
int w, h;
// **********found**********
w = h =______;
return Rectangle(pt, w, h);
}
int main ()
{
Point p(3, 2);
Circle c(p, 1);
cout << --圆形---------- \n';
cout << '圆心 = (' << p.getX() << ',' << p.getY() << ')\n';
cout << '半径 =' << 1 << endl;
cout << '面积 =' << c.area() << endl << endl;
Rectangle bb = c.boundingBox();
Point t1 = bb.topLeft();
Point br = bb.bottomRight();
cout << '--外切矩形-----------\n';
cout << '左上角 = (' << t1.getX() << ',' << t1.getY() << ')\n';
cout << '右下角 = (' << br.getX() << ',' << br.getY() << ')\n';
cout << '面积 =' << bb.area() << endl;
return 0;
}