香港恒生银行网上转账:关于c++的两道编程题(大一)

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/05 02:40:22
1.用模板设计一个队列类,包含判队、读队、入队和出队功能,要求用成员
函数重载“()”实现判队功能,用友元函数重载“--”实现出队功能。

2利用基类、派生类和虚函数的概念编写一个程序计算三角形、矩形和圆形
的面积。

明天就要交~各位大虾快来help下啊~感激不尽~

#include <iostream>

const double PI = 3.1415926;

using namespace std;

struct Point
{
Point()
{
x = y = 0;
}
Point(double X, double Y)
:x(X), y(Y){};
double x;
double y;
};

class shape
{
public:
virtual double GetArea()=0; // 虚函数,纯虚函数,图形不具体的话,无法求面积
virtual void Print()=0; // 虚函数,纯虚函数,图形不具体的话,无法输出面积
};

// 三角形类,继承自图形类
class trigon : public shape
{
public:
trigon(Point &A, Point &B, Point &C)
: a(A), b(B), c(C){};
~trigon(){};

double GetArea()
{
// 自己找公式吧,根据三点计算面积的
area = 0;
return area;
}
void Print()
{
cout << "三角形【(" << a.x << "," << a.y << "):("
<< b.x << "," << b.y << "):("
<< c.x << "," << c.y << ")】的面积是:"
<< 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);
}

void Print()
{
cout << "矩形【(" << pa.x << "," << pa.y << "):("
<< pb.x << "," << pb.y << "):("
<< pc.x << "," << pc.y << "):("
<< pd.x << "," << pd.y << ")】的面积是:"
<< 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()
{
cout << "圆形【" << c.x << ":" << c.y << ":" << r << "】的面积是:"
<< 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;
}

我也是大一,可是我们学的C语言,我看了你的问题半天,也是摸不着头脑。。。呵呵,不好意识。。。