收藏 分销(赏)

上海大学c++-2015秋a---答案--t教程文件.doc

上传人:快乐****生活 文档编号:3786310 上传时间:2024-07-18 格式:DOC 页数:10 大小:143KB 下载积分:8 金币
下载 相关 举报
上海大学c++-2015秋a---答案--t教程文件.doc_第1页
第1页 / 共10页
上海大学c++-2015秋a---答案--t教程文件.doc_第2页
第2页 / 共10页


点击查看更多>>
资源描述
上海大学C++2014-2015秋A---答案--t 精品文档 序号 试卷 第 1 页 ( 共 5 页 ) 4.[ ]GetData函数功能是Get它动态创建的包含有数据的基地址,及数据个数,好的设计方案及客户对它的正确调用是: A. int  GetData( int * pp ); int *pd; int n = GetData( pd ); B. int  * GetData( int n ); int n=3; int *pd = GetData( n ); C. int  GetData( int pp ); int *pd; int n = GetData( pd ); D.int  GetData( int ** pp ); int *pd; int n = GetData(&pd ); 5. [ ] 已有定义 class K { private: K(); }; 修改错误: A.去掉private: B. K k1; 改为 K k1(); C.private 改为public D. }; 改为 } 6.[ ](多选)设计一个影片管理系统,包括一般影片、外国片,影片剪接,现有一部外国影片Titanic (泰坦尼克号),如下的解决方案正确的有: A.Film作为基类,ForeignFilm和 DirectorCut作为Film的派生类。 B.Titanic作为Film的的成员变量。 C.Titanic是ForeignFilm的一个实例。 D.Film作为类ForeignFilm和类DirectorCut的成员变量。 E.数据输出Output函数采用虚成员函数,Film、ForeignFilm、DirectorCut都实现它。 7. [ ] 已有定义: class BC{ public:int b; void mfunc(){cout<<"BC";} }; class DC : public BC { public: int d; void mfunc(){ BC::mfunc();cout<<"DC";} }; BC bc;       DC dc;  BC *pb=new DC; 以下代码正确的是: A. bc.d = 1; B. pb->d = 1; C. DC *p=pb;  p.d = 1; D. DC *pdc=static_cast<DC*>(pb); pdc->d = 1; 8.[ ] 重载右移操作符,从流输入数据到复数类对象,设计方案选择: A.istream & operator>>( istream &istm, Complex &c); B.istream & operator>>( istream istm, Complex c); C.istream & operator>>( istream& istm, Complex c); D.istream operator>>( istream istm, const Complex & c); E.Complex operator<<( istream istm); 2014 ~ 2015 学年 一. 选择 (16分,每题2分) 1.[ ] 以下面向对象程序设计(OOP)的基本概念不正确的是 A. 计算机并不仅仅是一台机器,它更像我们大脑的一部分。 B. 面向对象程序设计语言的目的是为了解决现实世界的模拟问题(如变电站系统). C. 对象类型的设计是面向对象程序设计的中心任务。 D. 继承表示了基本类型和派生类型之间的相似性,一个基类具有所有由它派生出来的类型所共有的特性和行为。 E. 当处理类型层次结构时,程序员常常希望不把对象看作是某一特殊类型,而把它看作基本类型,应用OOP的多态性,编写不依赖于特殊类型的代码。 F. C++语言中类实际上就是对象。 2.[ ]要访问名空间IEC中的标识符LPHD,C++的方法是 A.IEC::LPHD B.IEC .LPHD C.IEC:LPHD C.using namespace std; LPHD 3. [ ] 动态分配n个float元素的内存空间,C++编程方法是: A.float *p=new float[sizeof(float)]*n; B.float a[ ]=new float[n]; C.float *p=new float[n]; D.float a[n]; int main() { K k1; return 0; } 注:教师应使用计算机处理试题的文字、公式、图表等;学生应使用水笔或圆珠笔答题。 试卷 第 2 页 ( 共 5 页 ) 6.C++标准模板库的名空间的名称是 ___________ 7. 函数参数的设计其数据传递有三种方式,分别是传________________________ 8. 实现下面的类 class CPerson { protected: string name; public: void setname( string name ) { _____8.1___ =name;} string getname(){ return name;} }; class CStudent : ________8.2____________ { double score; public: _____8.3_____(){} //构造函数 ______8.4____________{ this->name=name;} //构造函数 ______8.5______ {} //析构函数 static int compareScore( Student &stu1, Student &stu2) { if( stu1.getscore () < stu2.getscore () ) return -1; else if( _____________8.6_____________ ) return 1; else return 0; } double getscore(){ return score;} void setscore( double score ) { this->score=score;} }; 改写getname成员函数为外联式实现___________8.7_____________________ 改写setscore成员函数为外联式实现_____8.8______________________________ 二.填空(26分,1,7题每题3分,其他每空1分) 1.面向对象程序设计语言的主要特征是______________________________ 2. 返回对象的引用,修改下面代码 int& dl( int n ) { ___________ int j= 2*n; return j; } 3. 下面代码CA和CB类型的对象分别有___3.1____个数据成员, 实现类的构造函数,初始化数据成员 class CA { private: int a; public: CA(int a) { _____3.2_______ } }; 4. 设计一个函数decode,功能是对字符串,用密钥解码,设计函数原型 (接口)是:___4.1__________________________________________ 其中相关参数和返回的说明:________4.2__________________________ 5. 异常处理代码填空 int div( int a,int b) { if(b ==0 ) ______5.1____“error:b==0”; return a/b; } class CB : public CA { int b; public: CB(int a,int b) ________3.3______ { _____3.4______ } }; int main( ) { try { int u,v; cin>>u>>v; cout<<"u/v="<<div(u,v); } _____5.2_______ { cout<<e<<endl; } return 0; } 试卷 第 3 页 ( 共 5 页 ) 2. 下面代码的输出结果__________________ class B { public: B() { cout<<"B"; } virtual ~B(){cout<<"~B";} }; void main() { B* p =new Z; delete p ; } 3. 下面的程序写入到data.txt文件中的内容___________3.1________________________ 屏幕显示的输出的内容_________________________3.2_____________________ #include <fstream> #include <sstream> using namespace std;; int main( ) { ostringstream ostm; string name="wang"; int age=20; ostm<<" 姓名 "<<name<<" 年龄 "<<age<<endl; ofstream ofile( "data.txt" ); ofile<<ostm.str(); //写入到文件 ofile.close(); ifstream ifile( "data.txt" ); if( ifile) { string str1,name,str2; int age; while( ifile>>str1>>name>>str2>>age ) { cout<<" 姓名 "<<name<<" 去年 "<<age<<" 今年 "<<age - 2<<endl; } ifile.close(); } return 0; } int main( ) { vector< ____8.9_______ > students; for(int i=0;i<10;i++) { string name; int score; cin>>name>> score; CStudent stu(name); stu. setscore(score); students.push_back(stu); } assert(students.size() > 0 ); cout<<”与” << students[0]. getname() <<”分数相同的学生有:”<<endl; for( int i=1; i< students.size() ;i++ ) { int comp=______________________8.10__________________________; if( comp==0) //i学生如果与0学生分数相同 { cout<< students[i]. getname()<<endl; } } return 0; } 三. 阅读程序 (18分,每小题2分) 1.写出输出结果: ________ class B {protected: virtual void f( ) {cout<<”B”<<endl;} public: void g( ){ f( ); } }; void main( ) { D d; d.g( ); } class Z: public B { public: Z(){ cout<<"Z "; } virtual ~Z(){ cout<<"~Z ";} }; class D : public B { protected: virtual void f( ) {cout<<”D”<<endl;} }; 试卷 第 4 页 ( 共 5 页 ) class IReportHandle { public: virtual void handleReport( string r ) = 0; ① }; class DevServiceClass { vector<IReportHandle *> m_RCBs; string m_r; void dchgReport() //数据已变化,报告订阅者 { for( auto it = m_RCBs.begin();it !=m_RCBs.end();it++) ② { IReportHandle * p = *it; p->handleReport( m_r ); } } public: void hookReport( IReportHandle *p) //订阅报告 { m_RCBs.push_back(p); ③ } void update(string r) //更新数据 { m_r = r; ④ dchgReport(); } }; class DevClientClass : public IReportHandle { string m_name; public: DevClientClass() { m_name = " DevClient";} ⑤ virtual void handleReport( string r ) //处理数据 { cout<<m_name<<" handleReport:"<<r<<endl; ⑥ } }; 4. 下面代码的输出结果__________________ class A {public: void m(const char *str){ cout<<”A”<<str<<endl; } }; class Z : public A {public: void m(){ cout<<"Z"<<endl; } }; int main() { Z z1; z1.m(); z1.A::m(“a”); return 0; } 5. 下面是设备监控系统采用事件驱动机制编写的一个简单示例程序,列出程序先后运行到断点位置(已用标号标记)的序号,或显示结果 键盘输入数据前,程序依次运行到的断点序列号_________5.1____________ 从键盘输入 MMXU1.PNV.phsA.cVal.mag.f=100.5A 回车后的断点位置: __5.2___ 断点位置⑧然后程序依次运行到的断点序列号: 5.3 程序显示的输出结果: 5.4 #include <string> #include <iostream> #include <vector> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { DevServiceClass dev; ⑦ DevClientClass cli; dev.hookReport( &cli ); char str[200]; cin.getline( str,sizeof(str) ); //采集数据 dev.update(str); ⑧ return 0; } 试卷 第 5 页 ( 共 5 页 ) 五.程序设计 (20分 , 第1题8分,第2题7分,第3题5分) 1. 设计一个模板容器类,功能是元素的压入和弹出在同一端进行,满足先进后出的要求. 并编一个main函数测试该模板容器。 模板容器类接口要求: 类名:Stack 模板参数表含模板类型参数T, 和实例类型参数size (表示容器最大容量) 成员函数: push: 压入元素; pop: 弹出元素 empty:容器是否空; full: 容器是否满 main()函数要求: 创建你的模板容器类对象,循环压入n个string字符串,然后弹出显示。 2. 使用MFC设计一个图形层次类 要求 1)抽象图形类 CShape 成员变量 m_x,m_y 成员函数 draw(CDC*), moveTo(x,y), CShape(x,y) CShape (x=0,y= 0) 2) 圆类 CCircle 成员变量 m_x,m_y,m_r 成员函数 draw(CDC*), moveTo(x,y),setR(r) , CCircle(x,y,r) 3) 矩形框类CRectBox 成员变量 m_x,m_y, m_x1,m_y1 成员函数 draw(CDC*), moveTo(x,y) , CRectBox(x,y, x1,y2) 4) 柱状图类 CBarChart 成员变量 m_x,m_y, m_x1,m_y1 //边框 m_datas //画柱状图的int数据容器 成员函数 moveTo(x,y), CBarChart(x,y, x1,y2) draw(CDC*) //画出边框,画出数据的柱状图, 设每根柱的宽度=元素个数/(m_x1-m_x) addData(int) //添加数据到 注:MFC 相关类CDC类相关的成员函数 bool Rectangle( int x1, int y1, int x2, int y2 ); //画矩形框(左上角,右下角) bool Ellipse( int x1, int y1, int x2, int y2 ); //画椭圆(左上角,右下角) 程序不要求考虑画笔及其填充色的问题。 类间关系要使用继承. 3. IEC61850 变电站通信网络与系统C++程序设计(课外项目代码) 四. 改错(对有标号的行,有错标记X,无错标记√)( 20分,每小题0.5分 ) class IObj {public: virtual void vfunc(int x) = 0;//1____ } //2 ___ class BC : IObj //3 ____ { int b; static int s; const int c; public: BC( ): c(0){ } //4_____ BC(int x) { b=x; c=0;} //5____ void ~BC( ){} //6 ____ int & getRef( ) { return this->b; } //7____ static void staticFuncSet(int x) { this->b = x; } //8_____ int getC( ) {return c;} //9_____ static void staticFuncSetS( int x) { s = x;} //10______ int getS() {return this->s;} //11______ virtual void vfunc(int x) { protectedFunc ( x ); } //12_______ void m(int x) { vfunc(x); } //13_____ private: void privateFunc(const char * str) { cout<<str<<b; } //14______ protected: void protectedFunc( int x){ b=s*x; } }; class DC : public BC //15._____ { public: int d; DC( ){} DC(int x,int y) : d(x) //16 _____ { BC( y ); } // 17 ______ ~DC( int* p){ delete p;} //18______ virtual ~DC(){} //19______ virtual void vfunc(int a) { d = a;} //20_______ void m(const char * str); }; void DC:: m(const char * str) //21.____ { BC::m(0); //22_____ privateFunc (str); //23____ protectedFunc( 0 ); //24_______ } int BC::s=0; //25____ int main( ) { IObj obj; //26_____ DC dc; //27_____ dc.m(10); //28______ dc.m("abc"); //29___ int &r=dc.getRef( ); //30____ BC bc; bc.privateFunc(“aaa”); //31_____ bc.protectedFunc(1); //32_____ BC::staticFuncSetS(1); //33_____ IObj *pobj=new BC(2); pobj->vfunc(1); //34 ____ BC *pbc= (BC*)pobj; //35_____ delete pobj; //36_____ BC *pb=&dc; //37 _____ BC::m(2); //38______ pb->vfunc(1); //39______ DC *pdc= dynamic_cast<DC*>(pb); //40____ } 答卷 第 1 页 ( 共 3 页 ) 成 绩 题号 第 二 题 填空题(续) 5.1(1分) throw 5.2(1分) catch 6(1分) std 7(3分) 值,地址,引用 8.1(1分) this->name=name 8.2(1分) public CPerson 8.3(1分) CStudent 8.4(1分) CStudent(string name) 8.5(1分) virtual ~ CStudent() 8.6(1分) stu1.getscore () > stu2.getscore () 8.7(1分) string CPerson::getname(){ return name;} 8.8(1分) void CStudent ::setscore( double score ) { this->score=score;} 8.9(1分) CStudent 8.10(1分) CStudent::compareScore(students[0], students[i] ); 小计 题号 第 三 题 阅读题 18分 1(2分) D 2 (2分) BZ~Z~B 3.1(2分) 姓名 wang年龄 20 3.2 (2分) 姓名 wang 去年20 今年18 4 (2分) Z A a 5.1(2分) 7,5, 3, 5.2(2分) 8 5.3(2分) 4,2,6 5.4(2分) DevClient handleReport: MMXU1.PNV.phsA.cVal.mag.f=100.5A 小计 上海大学 2014 ~ 2015 学年 秋 季学期答卷 A 课程名: 面向对象程序设计A 课程号:09366046学分: 4 应试人 应试人学号 应试人所在院系 题号 一 二 三 四 五 六 七 八 九 得分 题号 第 一 题 选择题 16分 每小题2分 1 F 2 A 3 C 4 D 5 C 6 ACE 7 D 8 A 小计: 题号 第二题 填空题 26 分 1(3分) 封装继承多态 2(1分) static 3.1(1分) 1,2 3.2(1分) this->a = a; 3.3(1分) : CA(a) 3.4(1分) this->b= b; 4.1(1分) void decode(const char *str, const char *key,char *tex ); 4.2(1分) 不返回,str:待解码的字符串,key:密钥,tex:解码后的文本 注:教师应使用计算机处理试题的文字、公式、图表等;学生应使用水笔或圆珠笔答题。 答卷 第 2 页 ( 共 3 页 ) 小计: 五.程序设计 20分 (1题8分,2题7分, 课外题5分) 1. 第 四 题 改错题 20分 每小题0.5分 题号 题号 题号 题号 题号 1 √ 2 X 3 X 4 √ 5 X 6 X 7 √ 8 X 9 √ 10 √ 11 √ 12 √ 13 √ 14 √ 15 √ 16 √ 17 X 18 X 19 √ 20 √ 21 √ 22 √ 23 X 24 √ 25 √ 26 X 27 √ 28 X 29 √ 30 √ 31 X 32 X 33 √ 34 √ 35 √ 36 √ 37 √ 38 X 39 √ 40 √ 小计: 答卷 第 3 页 ( 共 3 页 ) class CRectBox : public CShape 1分 {protected: int m_x1; int m_y1; public: CRectBox(int x,int y,int x1,int y1):CShape(x,y) { m_x1=x1; m_y1=y1; }; virtual void draw(CDC* pDC) { pDC->Rectangle(m_x,m_y,m_x1, m_y1 ); } }; class CBarChart : CRectBox 1分 { vector<int> m_datas; public: CBarChart(int x,int y,int x1,int y1) : CRectBox( x, y, x1, y1){} void addData(int a) 1分 { m_datas.push_back(a); } virtual void draw(CDC* pDC) 1分 { CRectBox::draw(pDC);//画边框 int n = m_datas.size(); for( int i=0;i<n;i++) { int w = n /(m_x1-m_x); int x = m_x1 + i*w; int x1 = m_x1 + i*w +w; int y = m_y1 - m_datas[i]; int y1 = m_y1; CRectBox bar( x,y,x1,y1); bar.draw(pDC); } } }; 总体结构 1 分 2. class CShape 1分 { protected: int m_x; int m_y; public: CShape(int x,int y) { m_x=x; m_y=y; } void moveTo(int x,int y) { m_x=x; m_y=y; } virtual void draw(CDC* pDC)=0; }; class CCircle : public CShape 1分 { public: int m_r; CCircle(int x,int y,int r):CShape(x,y) { m_r=r; } virtual void draw(CDC* pDC) { pDC->Ellipse(m_x-m_r,m_y-m_r,m_x+m_r,m_y+m_r); } }; 收集于网络,如有侵权请联系管理员删除
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服