C++2009-2010冬-t

发布时间:2013-11-21 08:57:27   来源:文档文库   
字号:

上海大学 2009 2010 学年 季学期试卷

面向对象程序设计A

一. 选择( 12分,每小题2 )

1[ ](多选)以下正确述是:

A.面向对象的程序设计把系统看成是对象的集合,它们通过交互来完成任务,每个对象用自己的方法管理数据。

B.在面向对象语言C++中一个类就是一个对象。

D.封装意味着对象内部是如何工作的对客户端来说是不可见的。

E多态性成员函数意味着调用的入口地址是运行期绑定

F.名空间的用途是防止标识符冲突,提供标识符的组织方式

2. [ ]根据下面getstr函数原型,正确的函数调用

bool getstr( char str[], int size );

A. char s[20]; getstr( s ); B. bool str = getstr( 20 );

C. char s[20]; getstr( s, sizeof(s) ); D. char str[]; getstr( &str ,20 );

3[ ]变量p1与变量p2值相等的是

A. int p1 = 5; int & p2 = p1;

Bint p1 = 5; int * p2 = &p1;

C. int *p1=new int[10]; int *p2=new int[10];

4[ ](多选)设计一个球类游戏,好的设计方案是:

A.类Ball为基类,类Football、类Basketball为派生类。

B.在A设计基础上,类Ball,类Football、类Basketball分别有公有的成员函数

playBall()playFootball ()playBasketbal ()

C.在A设计基础上,类Ball有纯虚成员函数play ()。类Football、类Basketball分别有相同签名的公有的虚成员函数play ()

E.以上的设计都是错误的。

5. [ ]重载操作符>>从输入流中读取数据到CObj类型的对象中正确的设计是

A istream & operator>>( istream& in, CObj obj);

Bistream & operator>>( istream &in, CObj & obj);

Cistream operator>>( istream in, CObj obj);

Dostream & operator<<( ostream in, CObj & obj);

Eostream & operator>>( ostream in, CObj & obj);

Fistream operator>>( istream &in, const CObj & obj);

Gistream & operator>>( istream in, CObj obj);

6. [ ](多选)根据以下定义,结果为true的表达式是

class B { public: virtual void vm(){…}; int b };

class D : public B{ };

B *pb=new D; D *pd=(D*) pb;

A. typeid( *pd) == typeid (D ) B. typeid( pd) == typeid (B *)

C. typeid( pb) == typeid (pd) D. typeid( pb) == typeid (B *)

E. typeid( pb) == typeid (D *) F. typeid( *pb) == typeid (B)

G. typeid( pb->b) == typeid (int) H. typeid( pb->b) == typeid (int*)

. 填空(21分,第1小题3分,其它每小题2)

1. 面向对象程序设计语言的主要特征是_______________________________________

3. 写一段代码 ____________________________________

动态分配一个类型为int的存储空间,并将它的地址赋给变量ptr.

2.指出下面代码设计的错误___________

int & db1( int i )

{

int j = 2*i;

return j;

}

3. int div0( int x,int y)

{

try

{

return x/y;

}

catch( ... )

{

cout<<"er:除以0";

}

}

class Student

{

string name;

double score;

public:

______1.1_____(){} //构造函数

______1.2____{ this->name=name;} //构造函数

~Student(){} //析构函数

int compareScore( Student &stu)

{

if( this->score < stu.getscore ()) return -1;

else if( ________________1.3_____________ )return 1;

else return 0;

}

string getname(){ return name;}

double getscore(){ return score;}

void setname( string name ) { _____1.7___ =name;}

void setscore( double score ) { this->score=score;}

};

Student stu;

改写setscore成员函数为外式实现

____________________________2________________________________

改写析构函数为外联式实现

____________________________3________________________________

设对象stu内存地址0x11234567,那么当执行stu.setscore(90.0); 调用进入成员setscore函数体中,this值是_______4.1_________ , score值是______

6. 下面程序,对象dc拥有_____6.1_______个数据成员.完成init函数体代码初始化他们。

class BC

{private:

int n1

public:

void set( int a ){ n1=a; }

};

class DC : public BC

{private:

int n2;

public:

void init( int num1,int num2 )

{ ________6.2________ }

};

DC dc;

流和文件的读写

void main()

{ ofstream ofile( "payroll.txt");

int a=40; double b=100.24; char *s="abc";

//格式化字符串

ostringstream sout;

sout<<"姓名"<< s <<" 年龄"<< a <<" 工资"<< b <

string str=sout.str();

//str写到文件

ofile<<str;

ofile.close(); //关闭文件

ifstream ifile( "payroll.txt");

char val[256] ;

double sum=0; //工资总和

while( ifile.getline( val ,sizeof(val)) ) //读文件

{

int age;double salary; string name,tempStr;

istringstream sstm( val );

sstm>>tempStr>>name>>tempStr>>age>>tempStr>>salary;

sum+=salary;

}

cout<<//显示工资总和

ifile.close();

}

. 阅读程序

4. 下面代码的输出 __________________

class B

{protected:

virtual void vf(){cout<<"B"<

public:

void gf(){ vf(); }

};

class D : public B

{protected:

virtual void vf(){ cout<<"D"< }

};

int main()

{ D d1; B* p=&d1; p->gf(); return 0;}

5.下面代码的输出结果__________________

class B

{protected:

void m(){ cout<<"B"< }

public:

void gm(){ m(); }

};

class D : public B

{protected:

void m(){ cout<<"D"< }

};

int main()

{ B* p = new D; p->gm(); return 0;}

//------------------------------------------------

.NET 事件编程

using namespace System;

delegate void Del(int, float);

ref class EventSource

{ public:

event Del^ E;

void fire(int i, float f) { E(i, f); }

};

ref class EventReceiver

{ public:

void Handler(int i , float f) { Console::WriteLine("Receive:{0},{1}",i,f); }

};

void main ()

{

EventSource ^ es = gcnew EventSource();

EventReceiver^ er = gcnew EventReceiver();

es->E += gcnew Del(er, &EventReceiver::Handler);

es -> fire(1, 3.14);

}

输出结果?Receive: 1, 3.14

//----------------------------------------------

#include

#include

#include

#include

using namespace std;

struct SDATA

{

string name;

int data;

};

class CApp

{

vector vec;

public:

void getdata( ) //输入数据

{

char c='y';

while( c == 'y' || c=='Y' )

{

SDATA d;

cin>>d.name; cin>>d.data;

vec.push_back( d );

cout<<"\n继续添加yn?";

cin>>c;

}

}

static bool cmp( SDATA &a, SDATA &b ) //比较数据,前面的数据比后面的大为真

{

if( a.data > b.data )

return true;

else

return false;

}

void printdata( ) //输出所有数据

{

sort( vec.begin(),vec.end(),cmp); //排序

vector::iterator it;

for(it = vec.begin() ; it != vec.end(); it++)

{

SDATA &d = *it;

cout<" "<< d.data<

}

}

};

int main()

{

CApp app;

app.getdata();

app.printdata();

return 0;

}

写一个包含2行输入数据的运行效果样例:

_________________

____________________

//-------------------------

class B{ public:virtual ~B(){cout<<"B";}};

class Z:public B{ public:virtual ~Z(){cout<<"Z ";}};

void main()

{

B* p =new Z; delete p ;

}

// Z B

//-----------------------

//---------------

//-------

. 改错(对有标号的行,有错标记X,无错标记√)

#include

using name std; //1

class CA

{public:

virtual CA( ){} //2

virtual void vm(int x)=0;//3

};

virtual void hi(); //4

class CB : public CA //5

{private:

int b; static int s;

void privateBFunc( int x)

{ this->s = x; } //6

protected:

void protectedBFunc( int x);

public:

void CB( ) : b(0) { } //7

void ~CB( ){} //8

void publicFunc( int x )

{ this->privateBFunc(x); } //9

int & getB( )

{ return &b; } //10

static void statictBFunc(int x)

{ s = x; } //11

virtual void vm(int x)

{ cout<<"CB "< //12

void m();

};

void virtual CB::m(){ /*..*/ } //13

void protectedBFunc( int x) //14

{ this->b = s*x; }

int CB::s =10 ; //15

class CD : public CB //16

{public:

int d;

virtual CD( ){ } //17

CD(int x):CB( x ),d(0) { } // 18

virtual ~CD( ){ } //19

virtual void vm(int x);

void publicFunc(const char * x);

void protectedDFunc()

{b=0;} //20

};

void CD:: vm(int x) //21

{ cout<<"CD "<

void CD:: publicFunc(const char * x) //22

{ CB::publicFunc(0); //23

cout<

void gb(CB *pB){ } void gd(CD* pD){ }

int main( )

{ CA aobj; //24

CB bobj; //25

CD dc (2); //26

dc.publicFunc(0); //27

dc.BC::publicFunc(0); //28

dc. protectedBFunc ( 0 ); //29

int & x=dc.getB( ); //30

bc.protectedBFunc(1); //31

CB::statictBFunc (1); //32

CA *pa=new CD(2); //33

pa->vm(1); //34

CD *pbc= (CD *)pa; //35

CB *pb=&dc; //36

CD *pdc= dynamic_cast( pb); //37

pdc->vm( 0); //38

gb( &dc); //39

gd( dc); //40

}

编程:

设计具有先进后出行为的容器栈模板类,main测试主函数.

要求

1.  模板类名: Stack

   模板参数:  T ,int size

   主要公有成员函数: push , pop, isEmpty, isFull

2. 输入输出样例

1 2 3

3 2 1

GDI+ Windows 图形设备接口 (GDI) 的高级实现

通过使用 GDI+,可以创建图形、绘制文本以及将图形图像作为对象操作。

使用Windows 图形设备接口GDI+设计一个图形类层次系统,测试程序如下

LRESULT CALLBACK WndProc(HWND hWnd, UINT message,

WPARAM wParam, LPARAM lParam)

{

HDC hdc; PAINTSTRUCT ps;

switch(message)

{ case WM_PAINT:

hdc = BeginPaint(hWnd, &ps);

Graphics graphics(hdc); //创建图形设备对象

CRectBox rect( 100,100,300,300);

rect.draw(graphics ); //画矩形框

CCircle cir( 20, 100,100 );

cir .draw(graphics ); //画园

CCurve cur(100,100,300,300);

cur. draw(graphics ); //sin曲线

EndPaint(hWnd, &ps);

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

}

要求:

1.设计并实现类的层次体系

1.1 抽象图形 Shape

成员变量 m_x,m_y

成员函数 draw(graphics), moveTo(x,y)

CPoint(x=0,y= 0)

1.2. CCircle

成员变量 m_x,m_y,m_r

成员函数 draw(graphics), moveTo(x,y),setR(r)

CCircle(r=0,x= 0,y=0 )

1.3矩形框类CRectBox

成员变量 m_x,m_y, m_x1,m_y1

成员函数 draw(graphics), moveTo(x,y)

CRectBox x ,y,x1,y1)

1.4. sin曲线类CCurve

成员变量 m_x,m_y, m_x1,m_y1

成员函数 draw(graphics), moveTo(x,y)

CCurve( x ,y,x1,y1)

相关资料:

GDI+ Windows 图形设备接口 (GDI) 高级实现XP以上版本适用),它通过C++封装,头文件gdiplus.h。本题涉及到的相关类及其成员函数如下:

颜色类Color 构造函数Color( BYTE , BYTE 绿, BYTE );

笔类Pen 构造函数Pen Color &颜色,int 宽度);

图形设备Graphics 成员函数如下

构造函数GraphicsHDC 设备句柄

画矩形框DrawRectanglePen * , int x,int y, int 宽度,int 高度);

画椭圆DrawEllipse(Pen * , int x,int y, int 宽度,int 高度);

画线 DrawLine(Pen * , int x1,int y1, int x2int y2);

要求:包含

Shape类层次,以Shape为抽象基类,Shape带有纯虚函数drawresize,从Shape派生出PointCircleRectangle类,实现draw(绘图,或输出其属性值)、resize(改变对象大小)函数

本文来源:https://www.2haoxitong.net/k/doc/34cc49d79b89680203d82580.html

《C++2009-2010冬-t.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式