上海大学 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<3class ______________________________ //声明外国影片类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