上海大学C++2005-2006冬.doc_第1页
上海大学C++2005-2006冬.doc_第2页
上海大学C++2005-2006冬.doc_第3页
上海大学C++2005-2006冬.doc_第4页
上海大学C++2005-2006冬.doc_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

。上海大学 2005-2006年冬季学期试卷成绩课程名:面向对象程序设计C+ 学分: 5 学号: 姓名: 院系: 一 选择1 C+语言编程的优势是A是一种面向对象的语言,而生成的代码运行时开销小。B是一种面向过程的语言,用自顶向下设计方法进行设计。C易学,比用VB,Delphi,java语言编程容易。D最适合开发图形化的Windows应用程序。2 .NET框架类库的名空间是System,使用它编程,要求声明:Ausing namespace std;B#define namespace System;Cusing namespace System;Dusing 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. 类的构造函数返回类型是voidE.类的成员变量含有是指针变量时,应该考虑设计一个拷贝构造函数。F. 类的静态成员函数不能访问非静态成员。G. 设计Automobile类,它含有Engine类型的成员变量。H. 设计Automobile类,它从Engine类派生。5. 设计从输入流中读数据到CMyObject类型的对象中,正确的函数原型是:A. istream read( istream in, CMyObject obj); B. void read( istream in, CMyObject& obj); Cistream & operator( istream& in, CMyObject obj); Distream & operator( istream &in, CMyObject & obj); 6 (多选)对以下的函数声明,合法的调用方式有。void getstr(string &buff, char term=n);Astring str; getstr(str);Bstring buff; getstr(&buff);Cstring str; getstr(str, t);Dchar *buff=”abc”; getstr(buff, t);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)if(b =0 )_return a/b;int main( )_int x,y; cinxy;coutx/y=divide(x,y);catch(char *e)couteendl;return 0;6-10. 复数类代码填空class Complexdouble 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#includeusing namespace std;class Filmpublic:Film(string t,string d) title=t; director=d;string title;string director;_ ;/声明数据打印函数output;void Film:output() /实现数据打印函数outputcout” title:” title” director:” directorendl;class _ /声明导演剪辑片类DirectorCutpublic:DirectorCut(string t,string d, string c):_ changes=c;string changes;virtual void output( )_cout” changes:” changesendl;class _ /声明外国影片类ForeignFilmpublic:ForeignFilm(string t,string d, string l): Film(t,d)_ string language;_ /声明数据打印函数outputFilm:output();cout” language:” _ select;Film *pFilm;if(select=0)pFilm =new Film(“红高粱”,”张艺谋”); /实例化Filmelse 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( listi = = key )return i;_;三 阅读程序1写出输出结果: _string s = Object-Oriented Programming in ;s += C+;coutsn;2写出输出结果: _int *p1=new int;int *p2=new int;if(p1=p2)cout” p1与p2相等”end;else cout” p1与p2不相等”end;3写出输出结果: _class B protected: virtual void f( ) cout”B”endl; public:void g( ) f( ); ;class D : public Bprotected: virtual void f( ) cout”D”endl;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);double r,salary1,salary2;sinrsalary1salary2;double salary=age*r+salary1+salary2; ostringstream sout;sout姓名 name 工龄 age 工资 salary;string str=sout.str();coutstrendl;return 0;5学生数据库查询打印程序,写出输出结果:_#include stdafx.h #include #include #include #include #include #includeusing namespace std;class CStudentmap courseScores; / (1)public:string m_name;string m_ID;CStudent() CStudent(string id,string name)m_ID=id; m_name=name;void setCourseScore(string courseName,int score)/(2)courseScorescourseName=score;void printScore() for(map:iterator p=courseScores.begin();p!=courseScores.end(); p+)coutfirsttsecondendl; /(3);class Crow /行int n;deque cells; /(4)public:CRow(int n) : cells(n) / (5)this-n=n;string& operator(int index) /(6)return cellsindex;void load(istream &in)for(int i=0;icellsi;class CDBManagedeque m_stuTable;deque m_scoreTable;deque stus;public:void ReadDatabase(const char* filename) /(7)ifstream ifile(filename);while(ifile) string type;ifiletype;if(type=Student)CRow row(2);row.load(ifile);m_stuTable.push_back(row);else if(type=Score)CRow row(3);row.load(ifile);m_scoreTable.push_back(row);elsebreak;resetStuData();void Report(string studentID) / (8)CStudent stu;if(true= findStu(studentID,stu)coutstu.m_name成绩endl;stu.printScore();coutendl;void Report() /(9)coutt成绩表endl;sort(stus.begin(),stus.end(),IdCompFunc);deque:iterator it;for( it=stus.begin();it!=stus.end();it+)CStudent& stu=*it;coutstu.m_name stu.m_IDendl;stu.printScore();private:void resetStuData()stus.clear();for( int i=0;im_stuTable.size();i+)CRow stuRow=m_stuTablei;string studentID=stuRow0;CStudent stu=CStudent(studentID,stuRow1);for( int j=0;jm_scoreTable.size();j+)CRow scoreRow=m_scoreTablej;if( scoreRow1 = studentID)int score=atoi(scoreRow2.c_str();stu.setCourseScore(scoreRow0,score);stus.push_back(stu);bool findStu(string studentID,CStudent &stu)deque:iterator it;for( it=stus.begin();it!=stus.end();it+)if(it-m_ID = studentID)stu=*it;return true;return false;static bool IdCompFunc(CStudent& stu1,CStudent& stu2)return stu1.m_ID x; /7_ static void statictX(int a)x=a; /8_ int getC( )return c; /9_ static void setS( int a) s=a; /10_int getS()return this-s; /11_virtual void v(int a) protectedFunc (a); /12_void m(int a) this.privateFunc(a); /13_private:void privateFunc( int a) x=c*a; protected:void protectedFunc( int a) x=s*a; ;class DC : public BC /14._ public:DC( )DC(int a) BC( a ); / 15 _ DC( int* p) delete p; /16_virtual void v(int a) privateFunc(a); /17_void m(const char * a);void DC: m(const char * a) /18._BC:m(0); /19 privateFunc (1); /20_int BC:s=0; /21 _ int main( ) IObject obj;/22_DC dc;dc.m(10); /23._dc.m(abc); /24._int *px=dc.getX( ); /25_BC bc;bc.privateFunc(1); /26_tectedFunc(1); /27_BC:statictX(1); /28_IObject *pobj=new BC(2);pobj-v(1); /29 _ delete pobj; /30_ BC *pbc= (BC*)pobj;/31_ BC *pb=&dc; /32 _ BC:m(2); /33_ DC *pdc= dynamic_cast(pb); /34_pdc-v(1); /35_四 设计类(任选两题)1. 设计一个Profession类,包含如下数据成员:name,title,credentials,education,avgIncom。类的公有接口要求包括比较Profession的avgIncome的成员函数CompareIncome和至少有10其他成员函数。2. 设计队列模板类Queue,基本要求是要实现Push入队和Pop出队的先进先出操作,并编一个main() 函数测试它。3 . 实现一个Shape类层次,以Shape为抽象基类,从Shape派生出Point,Circle,Rectangle类,基本行为是MoveTo,Draw,要求每个类都覆盖draw函数,用来输出对象的数据,并编一个main() 函数测试它。/完/标准答案五 选择(每个2分,共14分。)-可编辑修改-1.A2.C3.B4.ACEFG5.D6.AC7.ACD六 填空1.封装继承多态(填空每空1分 ,共27分)2.类汽车3. 商品家电交通工具冰箱电视机自行车电动车4-5. throw er; try6-10. real=r; image=0; real=r; image=i;friend Complex Complex Complex(t.real-u.image,t.real-u.image);11-22 stringvirtual void output()DirectorCut : public FilmFilm(t,d)Film:output();ForeignFilm: public FilmLanguage = l;virtual void output( )languagepFilm = newpFilm =new ForeignFilmdelete23-27class Tint T list,T key,int nint i=0;in;i+return -1;七 阅读程序1. Object-Oriented Programming in C+(每题2分,共8分。)2. p1与p2不相等3.D4. 姓名王伟 工龄 40 工资 34005. (分别2分,3分,共5分。)李四成绩数学70物理60成绩表李四 010002数学70物理60张三020001数学80政治90四对有标号的行,有错标记X,无错标记,可以修改的修改。(-0.5分共9分X -1分(指出错误0.5分 修改0.5分)共17分,共26分。)1. 2. X;3. X public IObject4. 5 . X BC(int a): c(0) x=a; 6 . X BC( )7. 8. X9. 10. 11. X return s;12. 13. X privateFunc(a); 14. 15. X DC(int a) :BC( a ) 16 . X DC() 17 .X18. 19. 20. X21. 22.X23. X24. 25. X int x=dc.getX( );26.X27. X28. 29. 30. 31. 32. 33.X34. 35. 五。设计类(任选两题) (每题10分,共20分。)1. (总体、属性、方法、main、CompareIncome分别2分,共10分)参考程序#include stdafx.h#include #include using namespace std;class Professionstring name;string title;string credentials;string education;double avgIncom;public:Profession()Profession(string name) this-name=name;int CompareIncome( Profession &profes)if( avgIncom profes.getavgIncom()return -1;else if( avgIncom = profes.getavgIncom() )return 0;else return 1;static int CompareIncome( Profession &profes1,Profession &profes2)if( profes1.getavgIncom() profes2.getavgIncom() return 1;else return 0;string getname()return name;string gettitle()return title;string getcredentials()return credentials;string geteducation()return education;double getavgIncom()return avgIncom;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;int _tmain(int argc, _TCHAR* argv)Profession prof2;for(int i=0;inametitlecredentialseducationavgIncom;profi.setname(name);profi.settitle(title);profi.setcredentials(credentials);profi.seteducation(education);profi.setavgIncom(avgIncom);for(i=0;i2;i+)coutprofi.getname()tprofi.gettitle()tprofi.getcredentials()tprofi.geteducation()tprofi.getavgIncom()tendl;int comp=Profession:CompareIncome( prof0,prof1 );coutprof0.getname()avgIncom 比 prof0.getname();if( comp 0 )cout

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论