中大实践考核面向对象(C++)程序设计试题和答案(上机考试) 下载本文

面向对象(C++)程序设计 (上机考试)

样题1. 下列Shape类是一个表示形状的抽象类,Area()为求图形面积的函数,Total()则是一个通用的用以求不同形状的图形面积总和函数。请从Shape类派生三角形类(triangle) 、矩形类(rectangle),并给出具体的求面积函数。编写程序验证求面积函数的正确性。Shape、 total的定义如下所示。

Class shape{ Pubilc:

Virtual float area()=0 };

float total (shape *s[ ], int n) {

float sum=0.0;

for(int i=0; iarea (); return sum; }

解答: #include

class shape{ public:

virtual float area()=0; };

float total(shape *s[], int n) {

float sum=0;

for(int i=0; i area(); return sum; }

class triangle : public shape{ protected: float H, W; public:

triangle(float h, float w) { H=h;

W=w;}

float area() { return H*W*0.5;} };

class rectangle : public triangle{ public:

rectangle(float h, float w) :

triangle(h, w) {}

float area() { return H*W;} };

void main() {

shape *s[4];

s[0] = new triangle( 3.0, 4.0 ); s[1] = new rectangle( 2.0, 4.0 ); s[2] = new triangle( 5.0, 8.0 ); s[3] = new rectangle( 6.0, 8.0 ); float sum = total(s,4);

cout << \

endl;

}

样题2. 以面向对象的概念设计一个类,此类包括

3个私有数据,unlead(无铅汽油), lead有铅汽油, total (当天总收入)。其中,无铅汽油价格是¥17/升,有铅汽油价格是¥16/升,请以构造函数的方式建立此值,并编写程序,该程序能够根据加油量,自动计算出油站当天的总收入。

解答: #include

class income { private:

float unlead, lead, total; public:

income(float ul, float l){unlead=ul; lead=l; total=0.0;}

float calculate(float, float); }; float income :: calculate(float unleadcontent, float leadcontent) {

total = unlead*unleadcontent + lead*leadcontent; return total; }

void main() {

float unleadcontent, leadcontent, total; income account(17, 16);

cout << \<< endl;

cin >> unleadcontent;

cout << \endl;

cin >> leadcontent;

total = account.calculate(unleadcontent, leadcontent);

1

cout << \<< endl; }

样题3. 编写一个计算两个给定长方形的面积的

程序,要求长方形用一个类(Rectangle)来表示,在该类中增加定义一个成员函数 add_area(), 该成员函数使用对象作为参数,用来计算两个给定长方形的面积。

解答: #include

class Rectangle { private:

float H, W; public:

Rectangle(float h, float w){H=h; W=w;}

float area(){ return H*W; } float add_area(Rectangle &); };

float Rectangle :: add_area(Rectangle &Rec) {

return area() + Rec.area(); }

void main() {

float h1, w1, h2, w2, totalarea;

cout << \H & W:\ cin >> h1 >> w1;

Rectangle rec1(h1,w1);

cout << \H & W:\ cin >> h2 >> w2;

Rectangle rec2(h2,w2);

totalarea = rec1.add_area(rec2);

cout << \total area of the two rectangle is:\ }

样题4. 定义一个复数类COMPLEX, 该类至少

提供加、减、赋值、输出等操作,所有操作均以友元形式实现。编写程序验证其功能。

解答: #include

class complex{ private:

float real; float imag; public:

complex(){};

complex(int r, int i) {real=r; imag=i;}

friend complex operator + (complex &, complex &);

friend complex operator - (complex &, complex &); void show(); };

complex operator + (complex & a, complex & b) {

float r, i;

r=a.real+b.real; i=a.imag+b.imag; return complex(r,i); }

complex operator - (complex & a, complex & b) {

float r, i;

r=a.real-b.real; i=a.imag-b.imag; return complex(r,i); }

void complex :: show() {

if (imag>0) {

if (imag == 1) cout << real << \

else cout << real << \<< imag << \ }

else if (imag<0) {

if (imag == -1) cout << real << \

else cout << real << imag << \ }

else cout << real; }

2

void main() {

complex a(4,5), b(2,3), x, y; x=a+b; y=a-b; a.show(); cout << \ b.show(); cout << \ x.show(); cout << endl; a.show(); cout << \ b.show(); cout << \ y.show(); cout << endl; }

样题5. 定义一个平面几何中点的位置类

POSITION, 它应该包含有移动、计算两点间的距离(包括到原点的距离),求X坐标、Y坐标等操作,其中计算两点间的距离以友元函数形式实现。编写程序验证其功能。

解答: #include

#include class Position{ private:

float X, Y; public:

Position(float xi, float yi){ X=xi, Y=yi;} void Move(float xo, float yo){ X+=xo, Y+=yo;}

float GetX(){return X;} float GetY(){return Y;} float distanceToOrigin();

friend float distance(Position &, Position &); };

float Position :: distanceToOrigin() {

return sqrt( X*X + Y*Y ); }

float distance(Position & a, Position & b) {

float dx = a.X - b.X; float dy = a.Y - b.Y;

return sqrt( dx*dx + dy*dy); }

void main() {

Position p1(1.5, 3.5), p2(4.5, 6.5); p1.Move(3.5, 5.5);

float dis0 = p1.distanceToOrigin(); float dis = distance( p1, p2 );

cout << \distance p1(\<< p1.GetX() << \

<< \to origin is: \<< dis0 << endl;

cout << \p1.GetX() << \

<< \\ << dis << endl; }

样题6. 利用类和对象,编制出一个卖瓜的程序。

每卖一个瓜要计出该瓜的重量,还要计算所卖出瓜的总重量及总个数,同时卖瓜时还允许退瓜。(提示:将每个瓜设为对象;用静态成员变量分别统计卖出瓜的总重量和总个数; 卖瓜行为用构造函数模拟,退瓜行为用析构函数模拟。)

解答: #include

class Watermelon{ private:

static int n;

static float totalWeight; float weight; public:

Watermelon(float w) {

n++;

weight=w;

totalWeight += w; }

Watermelon(Watermelon & wa) {

n++;

weight=wa.weight; totalWeight += weight; }

3

~Watermelon() { n--;

totalWeight -= weight; }

int getWeight() {

return weight; }

static int getNum() {

return n; }

static int getTotal() {

return totalWeight; } };

int Watermelon :: n = 0;

float Watermelon :: totalWeight = 0; void main() {

float w;

cout << \initial weight of watermelon: \

<< Watermelon :: getTotal() << endl;

cout << \input weight of watermelon:\ cin >> w;

Watermelon wa1(w);

cout << \input weight of watermelon:\ cin >> w;

Watermelon wa2(w);

cout << \input weight of watermelon:\ cin >> w;

Watermelon wa3(w);

cout << Watermelon :: getNum() << \ cout << \total weight is: \<< Watermelon :: getTotal() << endl; wa3.Watermelon::~Watermelon();

cout << \one watermelon was withdrawed!\

cout << Watermelon :: getNum() << \ cout << \total weight is: \<< Watermelon :: getTotal() << endl; }

样题6. 编写一个程序,用于计算三角形、矩形

和圆的总面积。(提示:由于尚不能确定该程序计算的具体形状,可以先定义一个抽象的类shape, 对于具体种类的形状,通过从shape派生一个类来对其进行描述。)

解答: #include

class shape{ public:

virtual float area()=0; };

float total(shape *s[], int n) {

float sum=0;

for(int i=0; i area(); return sum; }

class triangle : public shape{ protected: float H, W; public:

triangle(float h, float w) { H=h; W=w;}

float area() { return H*W*0.5;} };

class rectangle : public triangle{ public:

rectangle(float h, float w) : triangle(h, w) {}

float area() { return H*W;} };

class circle : public shape{ protected: float radius; public:

circle(float r) { radius=r; } float area() { return radius*radius*3.14; } };

4