您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页上海大学C++2005-2006冬

上海大学C++2005-2006冬

来源:爱go旅游网
上海大学 2005-2006年冬季学期试卷

课程名:面向对象程序设计C++ 学分: 5

成 绩学号: 姓名: 院系:

一. 选择

1.[ ]C++语言编程的优势是

A.是一种面向对象的语言,而生成的代码运行时开销小。 B.是一种面向过程的语言,用自顶向下设计方法进行设计。 C.易学,比用VB,Delphi,java语言编程容易。 D.最适合开发图形化的Windows应用程序。

2.[ ].NET框架类库的名空间是System,使用它编程,要求声明: A.using namespace std; B.#define namespace System; C.using namespace System; D.using System;

3.[ ] 写一条语句,动态分配100个char 空间的内存,地址赋给ptr : A. char *ptr=new double*100; B. char *ptr=new char [100]; C. char char =new char (100); D. char *ptr=malloc( sizeof(char)*100);

4. [ ](多选)下面说法或设计正确的有:

A. 用class进行类的定义,类的成员在默认情况下访问权限是私有的。 B. C++语言的变量声明必须写在函数代码的开头地方。 C.一个抽象类,不能实例化。 D. 类的构造函数返回类型是void

E.类的成员变量含有是指针变量时,应该考虑设计一个拷贝构造函数。 F. 类的静态成员函数不能访问非静态成员。

G. 设计Automobile类,它含有Engine类型的成员变量。 H. 设计Automobile类,它从Engine类派生。

5. [ ]设计从输入流中读数据到CMyObject类型的对象中,正确的函数原型是: A. istream read( istream in, CMyObject obj); B. void read>>( istream in, CMyObject& obj);

C.istream & operator>>( istream& in, CMyObject obj); D.istream & operator>>( istream &in, CMyObject & obj);

1

6.[ ] (多选)对以下的函数声明,合法的调用方式有。 void getstr(string &buff, char term=‟\\n‟); A.string str; getstr(str); B.string buff; getstr(&buff); C.string str; getstr(str, „\‟); D.char *buff=”abc”; getstr(buff, „\‟);

7.[ ] (多选)阅读本试题三.5题中的程序,对有标注行的说明正确的是 A.(1)学生所有课程成绩的关联容器。(2)Set课程和成绩。(7)读数据文件 B.(3)第一个元素和第二个元素输出。(4)学生所有课程成绩的关联容器。 C.(5)参数n表示元素个数。(6)返回第index下标的元素。

二. 填空

1-2.面向对象程序设计语言的主要特征是_____________其基本模块是_______

3.对商场中的商品,冰箱,汽车,家电,电视机,交通工具,自行车,电动车设计类层次结构图(画出结构图)_________

4-5. 代码填空

int divide( int a,int b) { }

int main( ) { }

__________ { }

catch(char *e) { } return 0;

cout<cin>>x>>y;

cout<<\"x/y=\"<if(b ==0 )

____________________ return a/b;

2

6-10. 复数类代码填空 class Complex { double real; double image; public: Complex(){} Complex(double r) _________________________ Complex(double r,double i) ____________________________ ___________________operator - (Complex t, Complex u ); };

______________________ operator - (Complex t, Complex u ) { //复数减实现 return _________________________________________________ }

11-22.影片跟踪管理程序填空 #include

#include< ___________ > using namespace std; class Film {

public: Film(string t,string d){ title=t; director=d;} string title; string director; _____________________________ ;//声明数据打印函数output };

void Film::output() //实现数据打印函数output

{ cout<<” title:”<< title<<” director:”<< director<class ____________________________ //声明导演剪辑片类DirectorCut {

public: DirectorCut(string t,string d, string c):____________ { changes=c;} string changes; virtual void output( ) { _______________________________ cout<<” changes:”<< changes<3

class ______________________________ //声明外国影片类ForeignFilm {

public: ForeignFilm(string t,string d, string l): Film(t,d){____________ } string language; ______________________ //声明数据打印函数output { Film::output(); cout<<” language:”<< _________________<void main( )

{ int select; cin>> select; Film *pFilm; if(select==0) pFilm =new Film(“红高粱”,”张艺谋”); //实例化Film

else if(select==1) //创建DirectorCut对象 _________ DirectorCut(“xxx”,”xxxx”,”xxxxxx”);//实例化DirectorCut else //创建ForeignFilm对象 ____________________(“HarryPotter”,”xxxx”,” English”); pFilm->output();

____________ pFilm; //销毁对象 }

23-27查找数组list中数据key,设计其摸板函数。成功返回其下标,否则返回-1。 template<_____________> ______ find( ___________________________ ) // 查找key { for( __________________________ ) if( list[i] = = key ) return i; ______________; }

4

三. 阅读程序

1.写出输出结果: _______ string s = \"Object-Oriented Programming in \"; s += \"C++\"; cout<2.写出输出结果: ________ int *p1=new int; int *p2=new int; if(p1==p2) cout<<” p1与p2相等”<3.写出输出结果: ________ class B {protected:

virtual void f( ) {cout<<”B”<public: void g( ){ f( ); } }; class D : public B

{protected:

virtual void f( ) {cout<<”D”<void main( ) { D d; d.g( ); }

4.写出输出结果: ________ #include \"stdafx.h\" #include #include using namespace std; int main() {

string val=\"10 1000.0 2000.0\"; int age=40; char* name=\"王伟\"; istringstream sin(val);

5

}

double r,salary1,salary2; sin>>r>>salary1>>salary2;

double salary=age*r+salary1+salary2; ostringstream sout;

sout<<\"姓名 \"<5.学生数据库查询打印程序,写出输出结果:______

#include \"stdafx.h\" #include #include #include

#include #include #include using namespace std;

6

class CStudent { };

class Crow //行 { };

class CDBManage {

deque m_stuTable; int n;

deque cells; //(4) CRow(int n) : cells(n) // (5) { }

string& operator[](int index) //(6) { }

void load(istream &in) { }

for(int i=0;iin>>cells[i]; return cells[index]; this->n=n;

map courseScores; // (1) string m_name; string m_ID; CStudent(){ }

CStudent(string id,string name) { }

void setCourseScore(string courseName,int score) { }

void printScore() { }

for(map::iterator p=courseScores.begin();p!=courseScores.end(); p++) { }

cout<first<<\"\\"<second<courseScores[courseName]=score;

//(2)

m_ID=id; m_name=name;

public:

public:

7

deque m_scoreTable; deque stus;

void ReadDatabase(const char* filename) //(7) { }

void Report(string studentID) // (8) { }

void Report() //(9) {

cout<<\"\成绩表\"<sort(stus.begin(),stus.end(),IdCompFunc); deque::iterator it; CStudent stu;

if(true== findStu(studentID,stu)) { }

cout<ifstream ifile(filename); while(ifile) { }

resetStuData();

string type; ifile>>type; if(type==\"Student\") { }

else if(type==\"Score\") { } else

break; CRow row(3); row.load(ifile);

m_scoreTable.push_back(row); CRow row(2); row.load(ifile);

m_stuTable.push_back(row);

public:

8

}

for( it=stus.begin();it!=stus.end();it++) { }

CStudent& stu=*it;

cout<private:

void resetStuData() { }

bool findStu(string studentID,CStudent &stu) { }

static bool IdCompFunc(CStudent& stu1,CStudent& stu2) {

return stu1.m_ID < stu2.m_ID; deque::iterator it;

for( it=stus.begin();it!=stus.end();it++) { }

return false;

if(it->m_ID == studentID) { }

stu=*it; return true;

stus.clear();

for( int i=0;iCRow stuRow=m_stuTable[i]; string studentID=stuRow[0];

CStudent stu=CStudent(studentID,stuRow[1]); for( int j=0;jstus.push_back(stu);

CRow scoreRow=m_scoreTable[j]; if( scoreRow[1] == studentID) { }

int score=atoi(scoreRow[2].c_str()); stu.setCourseScore(scoreRow[0],score);

9

};

}

int main() { }

数据文件DBData.txt中有数据如下: Student 020001 张三 Student 010002 李四 Score 数学 020001 80 Score 数学 010002 70 Score 物理 010002 60 Score 政治 020001 90

CDBManage manage;

manage.ReadDatabase(\"DBData.txt\"); manage.Report(\"010002\"); manage.Report(); return 0;

四.对有标号的行,有错标记X,无错标记√,可以修改的修改。

class IObject {public:

virtual void v(int x) = 0; //1_________

//3 _________

} //2 ________ class BC : IObject {

int getS()

{return this->s;} //11____________________ virtual void v(int a)

{ protectedFunc (a); } //12_______________________ void m(int a) int x; public:

BC( ): c(0){ } //4_________ BC(int a) { x=a; c=0;} //5_________________

void ~BC( ){} //6 ________ int & getX( )

{ return this->x; } //7______________ static void statictX(int a) {

int getC( )

{return c;} //9__________ static void setS( int a) { s=a;}

//10________________________

x=a; } //8___________

static int s; const int c;

10

};

{ this.privateFunc(a); } //13____________________ void privateFunc( int a){ x=c*a; } void protectedFunc( int a){ x=s*a; }

private: protected:

class DC : public BC //14.___________________ { public: };

void DC:: m(const char * a) //18._____________________ { }

int BC::s=0; int main( ) { IObject obj; }

DC dc; dc.m(10);

//23.___________________

dc.m(\"abc\"); //24.__________________ int *px=dc.getX( ); //25______________ BC bc;

bc.privateFunc(1); //26_____________________ bc.protectedFunc(1); //27______________________ BC::statictX(1); pobj->v(1); delete pobj; BC *pb=&dc; BC::m(2);

//28______________

//29 _________

//31____________

IObject *pobj=new BC(2);

//30________

//32 __________

//33__________ //22__________________

//21 _________

BC::m(0); //19

privateFunc (1); //20_________________________________ DC( ){}

DC(int a) { BC( a ); } // 15 ________________ ~DC( int* p){ delete p;} virtual void v(int a)

{ privateFunc(a);} //17_________________ void m(const char * a);

//16______________________

BC *pbc= (BC*)pobj;

DC *pdc= dynamic_cast(pb); //34_________ pdc->v(1); //35_____________

四. 设计类(任选两题)

1. 设计一个Profession类,包含如下数据成员:name,title,credentials,education,avgIncom。类的公有接口要求包括比较Profession

11

的avgIncome的成员函数CompareIncome和至少有10其他成员函数。

2. 设计队列模板类Queue,基本要求是要实现Push入队和Pop出队的先进先出操作,并编一个main() 函数测试它。

3 . 实现一个Shape类层次,以Shape为抽象基类,从Shape派生出Point,Circle,Rectangle类,基本行为是MoveTo,Draw,要求每个类都覆盖draw函数,用来输出对象的数据,并编一个main() 函数测试它。

/////////////////////////////////////////////////////完/////////////////////////////////////////////////

标准答案

五. 选择(每个2分,共14分。)1.A 2.C 3.B

4.ACEFG

5.D 6.AC 7.ACD

六. 填空

1.封装继承多态(填空每空1分 ,共27分) 2.类

商品 家电 冰箱 3.

12

交通工具 电视机 自行车 电动车

汽车 4-5. throw \"er\"; try

6-10. { real=r; image=0;}

{ real=r; image=i;} friend Complex

Complex Complex(t.real-u.image,t.real-u.image); 11-22 23-27

string class T virtual void output() int DirectorCut : public Film T list[],T key,int n Film(t,d) int i=0;i七. 阅读程序

1. Object-Oriented Programming in C++ (每题2分,共8分。) 2. p1与p2不相等 3.D

4. 姓名王伟 工龄 40 工资 3400 5. (分别2分,3分,共5分。)

李四成绩

数学 物理

70 60

成绩表

70 60 80 90

李四 010002 数学 物理 数学 政治

张三 020001

四.对有标号的行,有错标记X,无错标记√,可以修改的修改。

(√----0.5分 共9分 X --1分(指出错误0.5分 修改0.5分)共17分,共26分。)1. √ 6 . X ~BC( ){} 2. X ; 7. √ 3. X public IObject 8. X 4. √ 9. √ 5 . X BC(int a): c(0) { x=a; } 10. √

13

11. X {return s;} 12. √

13. X { privateFunc(a); } 14. √

15. X DC(int a) :BC( a ){ } 16 . X ~DC(){ } 17 .X 18. √ 19. √ 20. X 21. √ 22.X 23. X 24. √

25. X int x=dc.getX( ); 26.X 27. X 28. √ 29. √ 30. √ 31. √ 32. √ 33.X 34. √ 35. √

14

五。设计类(任选两题) (每题10分,共20分。)

1. (总体、属性、方法、main、CompareIncome分别2分,共10分) 参考程序

#include \"stdafx.h\" #include #include using namespace std; class Profession {

string name; string title; string credentials; string education; double avgIncom; Profession(){}

Profession(string name){ this->name=name;} int CompareIncome( Profession &profes) { }

static int CompareIncome( Profession &profes1,Profession &profes2) { }

string getname(){return name;} string gettitle(){return title;}

string getcredentials(){return credentials;} string geteducation(){return education;} double getavgIncom(){return avgIncom;}

if( profes1.getavgIncom() profes2.getavgIncom()) return 1; else return 0;

if( avgIncom < profes.getavgIncom())return -1; else if( avgIncom == profes.getavgIncom() )return 0; else return 1;

public:

15

};

int _tmain(int argc, _TCHAR* argv[]) {

cout<int comp=Profession::CompareIncome( prof[0],prof[1] );

cout<<<\"\\"<}

cin>>name>>title>>credentials>>education>>avgIncom; prof[i].setname(name); prof[i].settitle(title);

prof[i].setcredentials(credentials); prof[i].seteducation(education); prof[i].setavgIncom(avgIncom); for(int i=0;i<2;i++) {

string name; string title; string credentials; string education; double avgIncom; Profession prof[2];

void setname( string name ) { this->name=name;} void settitle( string title ) { this->title=title;}

void setcredentials( string credentials ) { this->credentials=credentials;} void seteducation( string education ) { this->education=education;} void setavgIncom( double avgIncom ) { this->avgIncom=avgIncom;}

16

}

cout<<\"少\"<else if( comp > 0 ) else

return 0;

2. (总体、template、方法、main、算法分别2分,共10分) 参考程序

template class Queue { public:

Queue() { }

virtual ~Queue() { }

void push( const T& e ); T pop() { }

T *elements; int head; int tail;

T tem= elements[head]; count--;

head=(head+1)%size; return tem; delete[] elements; elements=new T[size]; head=tail=0; count=0;

private:

17

};

int count;

template

void Queue:: push(const T&e) { }

void main( ) { }

Queue queue; //创建有100个元素的队列 queue.push(\"aaaa\"); queue.push(\"bbbbb\"); cout<tail=(tail+1)%size;

3. (Shape、Circle 、Point、Rectangle 、main分别2分,共10分) 参考程序

class Shape { public: };

class Point: public Shape {

protected:

int x; int y; Point(){}

Point(int x,int y) {

this->x=x;

this->y=y;

virtual void Draw() = 0;

virtual void MoveTo(int x,int y)=0;

public:

18

};

}

virtual void MoveTo(int x,int y) { }

virtual void Draw() { }

cout<<\"Point(\"<x=x;

this->y=y;

class Circle:public Point { };

class Rectangle:public Point {

int x1; int y1; Rectangle(){}

Rectangle(int x0,int y0,int x1,int y1) : Point(x0,y0) { }

void MoveTo(int x,int y) {

int dx=x-this->x;

this->x1=x1; this->y1=y1; int r; Circle(){}

Circle(int x,int y,int r) : Point(x,y) { }

virtual void Draw() { }

cout<<\"Circle(\"<r=r;

public:

public:

19

}

int dy=x-this->y; Point::MoveTo(x,y); this->x1=x1+dx;

this->y1=y1+dy;

virtual void Draw() { cout<<\"Rectangle[(\"<}

};

int main() { Shape* shapes[3];

int x=10,y=10,r=5,x1=20,y1=20; shapes[0]=new Point(x,y);

shapes[1]=new Circle(x,y,r); shapes[1]->MoveTo(20,20); shapes[2]=new Rectangle(x,y,x1,y1); shapes[2]->MoveTo(30,30); for(int i=0;i<3;i++) { shapes[i]->Draw();

} return 0;

}

20

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务