应用题
1. 请使用VC6或使用【答题】菜单打开
考生文件夹proj2下的工程proj2,此工程中包含一个头文件shape.h,其中包含了类Shape、Point和Triangle的声明;包含程序文件shape.cpp,其中包含了类Triangle的成员函数和其他函数的定义;还包含程序文件proj2.cpp,其中包含测试类Shape、Point和Triangle的程序语句。请在程序中的横线处填写适当的代码并删除横线,以实现上述功能。此程序的正确输出结果应为:
此图形是一个抽象图形,周长=0,面积=0
此图形是一个三角形,周长=6.82843,面积=2
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
//shape.h
class Shape {
public:
virtual double perimeter() const {return 0;} //返回形状的周长
virtual double area() const {return 0;} //返回形状的面积
virtual const char * name() const {return "抽象图形";} //返回形状的名称
};
class Point{ //表示平面坐标系中点的类
double x;
double y;
public:
// **********found**********
Point (double x0, double y0):
______{} //用x0、y0初始化数据成员x、y
double getX() const {return x;}
double getY() const {return y;}
};
class Triangle: public Shape{
// **********found**********
______;
//定义3个私有数据成员
public:
Triangle (Point p1, Point p2, Point p3): point1 (p1), point2 (p2), point3 (p3) {}
double perimeter () const;
double area() const;
const char * name() const {return "三角形";}
};
//shape.cpp
#include "shape.h"
#include <cmath>
double length (Point p1, Point p2)
{
return sqrt ((p1.getX() -p2.getX()) * (p1.getX() -p2.getX()) + (p1.getY() -p2.getY()) * (p1.getY() -p2.getY()));
}
double Triangle:: perimeter () const
{//一个return语句,它利用length函数计算并返回三角形的周长
// **********found**********
______;
}
double Triangle::area() const
{
double s = perimeter()/2.0;
return sqrt (s * (s - length (point1, point2)) * (s - length (point2, point3)) * (s - length (point3, point1)));
}
//proj2.cpp
#include "shape.h"
#include <iostream>
using namespace std;
// **********found**********
______ //show函数的函数头(函数体以前的部分)
{
cout << "此图形是一个" << shape.name() << ",周长=" << shape.perimeter() << ",面积=" << shape.area() << endl;
}
int main()
{
Shape s;
Triangle tri(Point(0,2), Point(2,0), Point(0,0));
show(s);
show(tri);
return 0;
}