收藏 分销(赏)

OOP技术:面向对象程序设计期末复习指导.doc

上传人:xrp****65 文档编号:5630323 上传时间:2024-11-15 格式:DOC 页数:24 大小:152KB 下载积分:10 金币
下载 相关 举报
OOP技术:面向对象程序设计期末复习指导.doc_第1页
第1页 / 共24页
OOP技术:面向对象程序设计期末复习指导.doc_第2页
第2页 / 共24页


点击查看更多>>
资源描述
OOP技术:面向对象程序设计期末复习指导 面向对象程序设计课程系中央电大开放教育本科计算机科学与技术专业统设必修课程,本课程的期末复习以中央电大下发的资料和网上复习指导为准。为了加强期末复习,下面市电大给出一套综合练习题,仅作参考。 综合练习题 一、单选题 1、 C++源程序文件的默认扩展名为( )。 A:cpp B:exe C:obj D:lik 2、 用new运算符创建一个含10个元素的一维整型数组的正确语句是( )。 A: int *p=new a[10]; B: int *p=new float[10]; C: int *p=new int[10]; D: int *p=new int[10]={1,2,3,4,5} 3、 假定有“struct BOOK{char title[40]; float price;}; BOOK *book=new BOOK;”,则正确的语句为( )。 A: strcpy((*book)->title,”Wang Tao”); B: strcpy(*book.title,”Wang Tao”); C: strcpy(book->title,”Wang Tao”); D: strcpy(book.title,”Wang Tao”); 4、 假定指针变量p定义为“int *p=new int[30];”,要释放p所指向的动态内存,应使用语句( )。 A: delete p; B: delete *p; C: delete &p; D: delete []p; 5、 关于消息,下列说法中不正确的是( )。 A: 发送消息的对象请求服务,接受消息的对象提供服务 B: 消息的发送者必须了解消息的接收者如何相应消息 C: 在C++中,消息的发送具体体现为对接收消息的对象的某个函数的调用 D: 每个对象只能接收某些特定格式的消息 6、 对于任一个类,用户所能定义的析构函数的个数至多为( )。 A: 0 B: 1 C: 2 D: 任意个 7、 do语句能够改写为( )语句。 A: 复合 B: if C: switch D: while 8、 假定AA为一个类,a为该类公有的数据成员,x为该类的一个对象,则访问x对象中数据成员a的格式为( )。 A: x(a) B: x[a] C: x->a D: x.a 9、 假定AA为一个类,a为该类私有的数据成员,GetValue()为该类公有函数成员,它返回a的值,x为该类的一个对象,则访问x对象中数据成员a的格式为( )。 A: x.a B: x.a() C: x->GetValue() D: x.GetValue() 10、 假定AB为一个类,则( )为该类的拷贝构造函数的原型说明。 A: AB(AB x); B: AB(AB& x); C: void AB(AB& x); D: AB(int x); 11、 关于运算符重载,下列说法正确的是( )。 A: 重载时,运算符的优先级可以改变。 B: 重载时,运算符的结合性可以改变。 C: 重载时,运算符的功能可以改变。 D: 重载时,运算符的操作数个数可以改变。 12、 下列对派生类的描述中错误的说法是:( )。 A: 派生类至少有一个基类 B: 派生类可作为另一个派生类的基类 C: 派生类除了包含它直接定义的成员外,还包含其基类的成员 D: 派生类所继承的基类成员的访问权限保持不变 13、 C++程序的基本模块为( )。 A: 语句 B: 函数 C: 表达式 D: 标识符 14、 以下叙述中不正确的是( )。 A: 在一个函数中,可以有多条return语句 B: 函数的定义不能嵌套,但函数的调用可以嵌套 C: 函数必须有返回值 D: 不同的函数中可以使用相同名字的变量 15、 文件包含命令中被包含的文件的扩展名( )。 A: 必须是.h B: 不能是.h C: 可以是.h或.cpp D: 必须是.cpp 16、 软件产品可被全部或部分地再用于新的应用的能力叫做软件的( )。 A: 可维护性 B: 可复用性 C: 兼容性 D: 正确性 17、 假定一个类的构造函数为 “A(int aa, int bb) {a=aa; b=aa*bb;}”,则执行 “A x(4,5);”语句后,x.a和x.b的值分别为( )。 A: 4和5 B: 5和4 C: 4和20 D: 20和5 18、 派生类的对象对其基类中( )可直接访问。 A: 公有继承的公有成员 B: 公有继承的私有成员 C: 公有继承的保护成员 D: 私有继承的公有成员 19、 程序运行中需要从键盘上输入多于一个数据时,各数据之间应使用( )符号作为分隔符。 A: 空格或逗号 B: 逗号或回车 C: 逗号或分号 D: 空格或回车 20、 假定有定义“int b[10]; int *pb;”,则不正确的赋值语句为( )。 A: pb=b[5]; B: *pb=new int; C: pb=&b[0]; D: pb=b; 21、 假定变量a和pa定义为“double a[10], *pa=a;”,要将12.35赋值给a中的下标为5的元素,不正确的语句是( )。 A: pa[5]=12.35; B: a[5]=12.35; C: *(pa+5)=12.35; D: *(a[0]+5)=12.35; 22、 关于面向对象系统分析,下列说法中不正确的是( )。 A: 术语“面向对象分析”可以用缩写OOA表示 B: 面向对象分析阶段对问题域的描述比实现阶段更详细 C: 面向对象分析包括问题域分析和应用分析两个步骤 D: 面向对象分析需要识别对象的内部和外部特征 23、 在C++程序中使用的cin标识符是系统类库中定义的( )类中的一个对象。 A: istream B: ostream C: iostream D: fstream 24、 假定AB为一个类,则执行 “AB *px=new AB[n];”语句时将( )。 A: 动态分配一个数组 B: 动态分配一个对象 C: 静态分配一个数组 D: 静态分配一个对象 25、在下面循环语句中循环体执行的次数为( )。 int i=0,s=0; while(s<20) {i++; s+=i;} A: 4 B: 5 C: 6 D: 7 26、 以下叙述不正确的是( )。 A: 宏替换不占用运行时间 B: 宏名无类型 C: 宏替换只是字符替换 D: 宏名必须用大写字母表示 27、 假定一条定义语句为“int a[10], x, *pa=a;”,若要把数组a中下标为3的元素值赋给x,则不正确的语句为( )。 A: x=a[3]; B: x=*(a+3); C: x=pa[3]; D: x=*pa+3; 28、 如果表达式++a中的“++”是作为成员函数重载的运算符,若采用运算符函数调用格式,则可表示为( )。 A: a.operator++(1) B: operator++(a) C: operator++(a,1) D: a.operator++() 29、 设”int a=12;”,则执行完语句”a+=a*a;”后,a的值是( )。 A: 144 B: 156 C: 288 D: 12 30、 在一个用链表实现的队列类中,假定每个结点包含的值域用elem表示,包含的指针域用next表示,链队的队首指针用elemHead表示,队尾指针用elemTail表示,若链队为空,则进行插入时必须把新结点的地址赋给( )。 A: elemHead B: elemTail C: elemHead和elemTail D: elemHead或elemTail 31、 类的析构函数可以带有( )个参数。 A: 0 B: 1 C: 2 D: 任意 32、 下面是重载双目运算符-的成员函数原形,其中最符合-原来含义的是( )。 A: Value Value::operator-(Value); B: Value Value::operator-(int); C: Value& Value::operator-(Value); D: Value& Value::operator-(Value&); 33、 在重载一运算符时,若运算符函数的形参表中没有参数,则不可能的情况是( )。 A: 该运算符是一个单目运算符。 B: 该运算符函数有一个隐含的参数this。 C: 该运算符函数是类的成员函数。 D: 该运算符函数是类的友元函数。 二、编程综合题 1. #include<iostream.h> void f4(int a[], int n, int& s) { s=0; for(int i=0; i<n; i++) s+=a[i]; } void main() { int b[8]={4,8,6,9,2,10,7,12}; int x; f4(b,5,x); cout<<x<<' '; int y; f4(b+3,4,y); cout<<y<<' '; cout<<x+y<<endl; } 2. #include<iostream.h> void main() { int a[10]={76,83,54,62,40,75,90,92,77,84}; int b[4]={60,70,90,101}; int c[4]={0}; for(int i=0;i<10;i++) { int j=0; while(a[i]>=b[j]) j++; c[j]++; } for(i=0;i<4;i++) cout<<c[i]<<’ ’; cout<<endl; } 3. #include<iostream.h> class A { int *a; public: A(int x=0):a(new int(x)){} ~A() {delete a;} int getA() {return *a;} void setA(int x) {*a=x;} }; void main() { A x1,x2(3); A *p=&x2; p->setA(x2.getA()+5); x1.setA(15+x1.getA()); cout<<x1.getA()<<' '<<x2.getA()<<endl; } 4. #include <iostream.h> #include <string.h> class Point { int x,y; public: Point(int x1=0, int y1=0) :x(x1), y(y1) { cout<<"Point:"<<x<<' '<<y<<' '; } ~Point() { cout<<"Point des! "; } }; class Text { char text[100]; //文字内容 public: Text(char * str) { strcpy(text,str); cout<<"Text con! "; } ~Text() {cout<<"Text des! ";} }; class CircleWithText : public Point,public Text { public: CircleWithText(int cx,int cy, char *msg): Point(cx,cy),Text(msg) { cout<<"Point with Text con! "; } ~CircleWithText() {cout<<"Point with Text des ";} };   void main() { CircleWithText cm(3,4,"hello"); } 5、 #include<iostream.h>         int a=5;         void main() {          int b=a+20;       int a=10;          cout<<a<<' '<<b<<endl;          {   int a=0,b=0;              for(int i=1; i<6; i++) {                   a+=i; b+=a;              }              cout<<a<<' '<<b<<endl;          }          cout<<a<<' '<<b<<endl;         } 6、 #include<iostream.h> int f1(int x, int y) { x=x+y; y=x+y; cout<<"x="<<x<<", y="<<y<<endl; return x+y; } void main() { int x=5,y=8; int z=f1(x,y); cout<<"x="<<x<<", y="<<y; cout<<", z="<<z<<endl; } 7、 #include<iostream.h>         class Franction {  //定义分数类          int nume;  //定义分子          int deno;  //定义分母           public:           //把*this化简为最简分数,具体定义在另外文件中实现    void FranSimp();            //返回两个分数*this和x之和,具体定义在另外文件中实现    Franction FranAdd(const Franction& x);           //置分数的分子和分母分别0和1          void InitFranction() {nume=0; deno=1;}           //置分数的分子和分母分别n和d          void InitFranction(int n, int d) {nume=n; deno=d;}        //输出一个分数    void FranOutput() {cout<<nume<<'/'<<deno<<endl;}         };   void main()         {          Franction a,b,c,d;           a.InitFranction(6,15);          b.InitFranction(3,10);          c.InitFranction();          c=a.FranAdd(b);          d=c.FranAdd(a);    cout<<"a: "; a.FranOutput();          cout<<"b: "; b.FranOutput();          cout<<"c: "; c.FranOutput();          cout<<"d: "; d.FranOutput();         } 8、 #include <iostream.h> class Point { int x,y; public: Point(int x1=0, int y1=0) :x(x1), y(y1) { cout<<"Point:"<<x<<' '<<y<<' '; } ~Point() { cout<<"Point destructor! "; } }; class Circle { Point center; //圆心位置 int radius; //半径 public: Circle(int cx,int cy, int r):center(cx,cy),radius(r) { cout<<"Circle radius:"<<radius<<' '; } ~Circle() {cout<<"Circle destructor! ";} };   void main() { Circle c(3,4,5); } 9、 #include<iomanip.h> const int M=20; void main() { int c2,c3,c5; c2=c3=c5=0; for(int i=1; i<=M; i++) { if(i%2==0) c2++; if(i%3==0) c3++; if(i%5==0) c5++; } cout<<c2<<' '<<c3<<' '<<c5<<endl; } 10、 #include<iostream.h> const int B=2; void main() { int p=1,s=1; while(s<50) { p*=B; s+=p; } cout<<"s="<<s<<endl; } 11、 #include<iostream.h> void main() { char s[3][5]={"1234","abcd","+-*/"}; char *p[3]; for(int I=0;I<3;I++) p[I]=s[I]; for(I=2;I>=0;I--) cout<<p[I]<<' '; cout<<endl; } 12、 #include<iostream.h> #include<string.h> class CD { char* a; int b; public: void Init(char* aa, int bb) { a=new char[strlen(aa)+1]; strcpy(a,aa); b=bb; } char* Geta() {return a;} int Getb() {return b;} void Output() {cout<<a<<' '<<b<<endl;} } dx; void main() { CD dy; dx.Init("abcdef",30); dy.Init("shenyafen",3*dx.Getb()+5); dx.Output(); dy.Output();   } 13、 #include <iostream.h> class Date { public: Date(int y=2001,int m=1,int d=1){Year=y; Month=m; Day=d;} void PrintDate(){ cout<<Year<<"/"<<Month<<"/"<<Day<<endl;} protected: int Year,Month,Day; }; class Time { public: Time(int h=5,int m=30,int s=0){Houre=h; Minutes=m; Seconds=s;} void PrintTime(){ cout<<Houre<<":"<<Minutes<<":"<<Seconds<<endl;} protected: int Houre, Minutes, Seconds; }; class Date_Time: public Date, public Time { public: Date_Time( ){}; Date_Time(int y,int mo,int d,int h=0,int mi=0,int s=0): Date(y,mo,d), Time(h,mi,s){} void PrintDate_Time(){PrintDate();PrintTime();} };   void main( ) { Date_Time a, b(2002,10,1,6,20,0), c(2003,3,8,6,7); a.PrintDate_Time(); b.PrintDate_Time(); c.PrintDate_Time(); } 16、 //*********************test.h********************// #include <iostream.h> class Base { public: Base (int i,int j){ x0=i; y0=j;} void Move(int x,int y){ x0+=x; y0+=y;} void Show(){ cout<<"Base("<<x0<<","<<y0<<")"<<endl;} private: int x0,y0; }; class Derived: private Base { public: Derived(int i,int j,int m,int n):Base(i,j){ x=m; y=n;} void Show (){cout<<"Next("<<x<<","<<y<<")"<<endl;} void Move1(){Move(2,3);} void Show1(){Base::Show();} private: int x,y; }; //**************************test.cpp************************// #include "test.h" void main( ) { Base b(1,2); b.Show(); Derived d(3,4,10,15); d.Move1(); d.Show(); d.Show1(); } 14、 #include<iomanip.h> const int N=5; void main() { int i,p=1,s=0; for(i=1;i<N; i++) { p=p*i; s=s+p; cout<<setw(5)<<i<<setw(5)<<p; cout<<setw(5)<<s<<endl; } } 15、 #include<iostream.h> const int N=5; void fun(); void main() { for(int i=1; i<N; i++) fun(); } void fun() { static int a; int b=2; cout<<(a+=3,a+b)<<' '; } 16、 #include<iostream.h> class A { int a[10]; int n; public: A(int aa[], int nn): n(nn) { for(int i=0; i<n; i++) a[i]=aa[i]; } int Get(int i) {return a[i];} int SumA(int n) { int s=0; for(int j=0; j<n; j++) s+=a[j]; return s; } }; void main() { int a[]={2,5,8,10,15,20}; A x(a,4); A y(a,6); int d=1; for(int i=0; i<4; i++) d*=x.Get(i); int f=y.SumA(5); cout<<"d="<<d<<endl; cout<<"f="<<f<<endl; } 三、简答题 1.计算1+3+32+...+310的值并输出,假定分别用i,p,s作为循环变量、累乘变量和累加变量的标识符。 2.已知,求出并显示当x依次取从键盘输入的不同值时所对应的y值,要求把a定义为常量,其值设定为10.2,x的每个值由键盘输入,并假定用-100作为键盘输入数据的终止标志,求平方根函数为sqrt(x)。 3.根据下面类中CompareBig 函数成员的原型和注释写出它的类外定义。 class AA {  int* a;  int n;  int MS; public:  void InitAA(int aa[], int nn, int ms) {   if(nn>ms) {cout<<"Error!"<<endl; exit(1);}   MS=ms;   n=nn;   a=new int[MS];   for(int i=0; i<n; i++) a[i]=aa[i];  } int CompareBig(AA b);  //比较*this与b的大小,从前向后按两数组            //中的对应元素比较,若*this中元素值大则返回1,若b中            //元素值大则返回-1,若相等则继续比较下一个元素,直到            //一个数组中无元素比较,此时若两者的n值相同则返回0,            //否则若*this中的n值大则返回1,若b中的n值大则返回-1。 }; 4、 根据下面类中Give函数的原型和注释写出它的类外定义。 class Array {  int *a;  //指向动态分配的整型数组空间  int n;    //记录数组长度 public:  Array(int aa[], int nn);  //构造函数,利用aa数组长度nn初始化n,                      //利用aa数组初始化a所指向的数组空间  Array(Array& aa);  //拷贝构造函数  Array& Give(Array& aa);  //实现aa赋值给*this的功能并返回*this     Array Uion(Array& aa);  //实现*this和aa中的数组合并的                       //功能,把合并结果存入临时对象并返回  int Lenth() {return n;}  //返回数组长度  void Print() {       //输出数组   for(int i=0; i<n; i++)    cout<<a[i]<<' ';      cout<<endl;  } }; 5、 指出程序或函数的功能 double f1(double a, double b, char op) { switch(op) { case ’+’: return a+b; case ’-’: return a-b; case ’*’: return a*b; case ’/’: if(b==0) { cout<<"divided by 0!"<<endl; exit(1); } else return a/b; default: cout<<"operator error!"<<endl; exit(1); } } 6、 根据下面类中Compare 函数成员的原型和注释写出它的类外定义。 class AA {  int* a;  int n;  int MS; public:  void InitAA(int aa[], int nn, int ms) {   if(nn>ms) {cout<<"Error!"<<endl; exit(1);}   MS=ms;   n=nn;   a=new int[MS];   for(int i=0; i<n; i++) a[i]=aa[i];  } int Compare(AA b); //比较*this与b的大小,若两者中         //的n值相同,并且数组中前n个元素值对应         //相同,则认为两者相等返回1,否则返回0。 }; 7、 下面给出了矩阵类Matrix定义。为了求两个矩阵对象的乘积,需要定义一个Matrix的友元函数Multiply()。请按照友元函数Multiply()的声明编写出该函数的定义。 class Matrix {   public:  Matrix(int row,int col);   //构造一个具有row行col列的矩阵     ~Matrix() {delete []mem;}    //析构函数 friend bool Multiply(Matrix &m1, Matrix &m2, Matrix &m3);            //定义Multiply()为友元函数,该函数把m1×m2的值赋给m3 //其他成员函数从略   private:  int *mem;                  //动态申请矩阵空间  const int rows,cols;          //矩阵的行数和列数 }; Matrix::Matrix(int row,int col):rows(row),cols(col) {     mem = new int[row*col]; } bool Multiply(Matrix &m1, Matrix &m2, Matrix &m3) { //确定矩阵是否能够进行相乘   if(m1.rows != m3.rows ||m2.cols != m3.cols || m1.cols != m2.rows)   return false; //定义sum变量,用于计算乘积矩阵m3中每个元素的值   int sum; //请在下面编写剩余部分 } 8、 指出程序或函数的功能 int fun6(int m, int n, int b=2) { if(m<b && n<b) return m*n; else if(m%b==0 && n%b==0) return b*fun6(m/b,n/b,b); else return fun6(m,n,++b); } 9、 下面程序段第4-9行中存在着三条语句错误,请指出错误语句的行号并说明原因。 class A { //1行 int a,b; //2行 const int c; //3行 public: //4行 A() {a=b=c=0;} //5行 A(int aa, int bb):c(aa+bb) {a=aa; b=bb;} //6行 }; //7行 A a,b(1,2,3);
展开阅读全文

开通  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 

客服