C++考试试题答案 下载本文

课程名称:面向对象的技术与方法 任课教师:唐宁九 学号: 姓名:

<< b.x << \

<< c.x << \】的面积是:\ << GetArea() << endl; }

private: trigon(){};

Point a, b, c; // 三角形三个点 double area; // 面积 };

// 矩形类,继承自图形类

class rect : public shape {

public:

rect(double &Top, double &Bottom, double &Left, double &Right) : top(Top), bottom(Bottom), left(Left), right(Right) {

// 填充四个点的坐标 pa.x = Left; pa.y = Top; pb.x = Left; pb.y = Bottom; pc.x = Right; pc.y = Top; pd.x = Right; pd.y = Bottom; }; ~rect(){};

double GetArea() // 计算矩形面积 { //

return (right - left) * (bottom - top); }

本题14页,本页为第11页

课程名称:面向对象的技术与方法 任课教师:唐宁九 学号: 姓名:

void Print() {

cout << \矩形【(\ << pb.x << \ << pc.x << \

<< pd.x << \】的面积是:\ << GetArea() << endl; } private: rect(){};

Point pa, pb, pc, pd; // 矩形左上、左下、右上、右下四个点坐标 double top, bottom, left, right; // 矩形上下左右四个线的坐标线 double area; // 面积 };

// 圆形类,继承自图形类 class circle : public shape {

public:

circle(Point &C, double &R) : c(C), r(R) {};

~circle(){};

double GetArea() { //

area = PI * r * r; return area; }

void Print() {

本题14页,本页为第12页

课程名称:面向对象的技术与方法 任课教师:唐宁九 学号: 姓名:

cout << \圆形【\ << \】的面积是:\ << GetArea() << endl; } private: circle(){}; Point c; // 圆心 double r; // 半径 double area; // 面积

};

int main(int argc, char *argv[]) {

double top, bottom, left, right, radius; top = 2; bottom = 1; left = 1; radius = 2;

radius = 5;

Point A(left,bottom), B(left,top), C(radius,top), D(radius,bottom); trigon Trigon(A, B, C);

rect Rect(top, bottom, left, right); circle Circle(A, radius);

shape *pS; pS = &Trigon; pS->Print(); pS = &Rect; pS->Print(); pS = &Circle; pS->Print();

return 0; }

本题14页,本页为第13页

课程名称:面向对象的技术与方法 任课教师:唐宁九 学号: 姓名: 本题14页,本页为第14页