收藏 分销(赏)

C++试题(含答案).doc

上传人:a199****6536 文档编号:10508507 上传时间:2025-05-31 格式:DOC 页数:9 大小:20.50KB
下载 相关 举报
C++试题(含答案).doc_第1页
第1页 / 共9页
C++试题(含答案).doc_第2页
第2页 / 共9页
点击查看更多>>
资源描述
一、选择题 1、下列的各类函数中,不是类的成员函数( )。 A) 构造函数 B) 析构函数 C) 友元函数 D) 拷贝初始化构造函数 2、已知: int n=10;下列表示引用的方法中,正确的是( )。 A) int &r; B) int &r=10; C) int &r=n; D) int *r=&n; 3、下列关于析构函数的说法,正确的是( ) A) 其名与类名完全相同 B) 返回类型是void类型 C) 函数体中必须有delete语句 D) 无形参,不可重载 4、已知函数原型:void fun(int a,int b=7,char z='*'); 则下面函数调用中不合法的为( )。 A) fun(5); B) fun(5,8); C) fun(5,'#'); D) fun(0,0,'*');; 5、类A是类B的友元,类B是类C的友元,则以下说法正确的是( )。 A) 类B是类A的友元 B) 类C是类A的友元 C) 类A是类C的友元 D) 以上都不对 6、关于对象成员的构造函数的调用顺序,说法正确的是( )。 A)与它们在类中说明顺序相同 B)与析构函数的调用顺序相同 C)与它们在成员初始化列表中给出的顺序相同 D)以上说法都不对 7、以下关于内联函数的说法正确的是( ) A)类的内联函数必须在类体外用关键字inline定义 B)类的内联函数必须在类体内定义 C)编译时将内联函数的目标代码插入每个调用该函数的地方 D)运行时将内联函数的目标代码插入每个调用该函数的地方 8、在有同名全局变量和局部变量时,可以用( )提供对全局变量的访问。 A)类运算符 B)域运算符 C) 重载 D)引用 9、假定x为一个类,执行X a[3],*p[2];语句时会自动调用该类的构造函数( )次。 A) 2 B) 3 C) 4 D)5 10、下列关于new运算符的描述中,错误的是( )。 A) 可以new运算符来动态创建对象和对象数组 B) 使用new运算符创建的对象或对象数组,可以使用运算符delete删除 C) 使用new运算符创建对象时要调用构造函数 D) 使用new运算符调用对象数组时不允许指定初始值 11、下面对静态数据成员的描述中,正确的是( ) A)类的每个对象都有自己的静态数据成员 B)静态数据成员是类的所有对象共享的数据 C)类的不同对象有不同的静态数据成员值 D)静态数据成员不能通过类的对象调用 12、如果一个类至少有一个纯虚函数,那么就称该类为( )。 A)抽象类 B)纯基类 C)派生类 D)虚类 13、若myclass类定义了拷贝构造函数和带一个整型参数的构造函数,还重载了赋值运算符,则语句 myclass obj = 100;将会( ) A) 调用赋值运算符重载函数 B) 调用带一个整型参数的构造函数 C) 调用拷贝构造函数 D) 引起编译错误 14、下列哪一种情况不会调用拷贝构造函数 ( ) A)用派生类的对象去初始化基类对象时 B)将类的一个对象赋值给该类的另一个对象时 C)函数的形参是类的对象,调用该函数将实参传给形参时 D)函数的返回值是类的对象,函数执行返回语句时 15、下列关于this指针的说法正确的是( ) A)在类的非静态函数中this指针指向调用该函数的对象 B)this指针是指向类的函数成员的指针 C)this指针是指向虚函数表的指针 D)this指针存在于每个函数之中 16、运算符的重载是对已有的运算符赋予多重含义,以下说法正确的是( )。 A) 可以对基本类型(如int 类型)的数据,重新定义“+”运算符的含义 B) 可以改变一个已有运算符的优先级和操作数个数 C) 只能重载C++中已经有的运算符,不能定义新运算符 D) C++中已经有的所有运算符都可以重载 17、若一个类的构造函数为 A(int aa, int bb){a=aa++;b=a*bb++;} ,则执行 Ax(4,5); 语句后, x.a 和 x.b 的值分别为 ( ) 。 A) 4 和 5 B) 5 和 4 C) 4 和 20 D) 20 和 5 18、在c++中下列声明之间有( )不同。 const int * ptr1=&num; int * const ptr2=&num; A) 不能用ptr1改变常整数的内容,然而可以用ptr2改变它指向的整数的内容 B) 不能用ptr2改变常整数的内容,然而可以用ptr1改变它指向的整数的内容 B) 不能用ptr2改变常整数的内容,也不能用ptr1改变它指向的整数的内容 C) 没有不同 19、建立派生类对象时,3种构造函数分别是C(成员对象的构造函数)、J(基类的构造函数)、D(派生类的构造函数),这3种构造函数的调用顺序为( )。 A) CJD B) JCD C) DJC D) DCJ 20、在公有派生情况下,有关派生类对象和基类对象关系的叙述不正确的是( )。 A) 派生类的对象可以赋给基类的对象 B) 派生类的对象可以初始化基类的引用 C) 派生类的对象可以直接访问基类中的成员 D) 派生类的对象的地址可以赋给指向基类的指针 二、写程序运行结果 1、请写出以下程序的运行结果。 #include <iostream> using namespace std; class A { public: A (int i) { x = i; } void dispa () { cout << x << “,”; } private : int x; }; class B: public A { public: B(int i) : A(i+10) { x = i; } void dispb() { dispa(); cout << x << endl; } private : int x; }; void main( ) { B b(2); b.dispb( ); } 2、请写出以下程序的运行结果。 #include<iostream> using namespace std; class MyClass { public: MyClass(int i=0) { n=i; cout<<n;} MyClass(const MyClass &x) { n=x.n; cout<<n;} MyClass& operator=(const MyClass &x) { n=x.n; cout<<n; return *this;} ~MyClass(){cout<<'#';} private: int n; }; int main( ) { MyClass obj1, obj2(2), obj3(obj1); obj1=obj2; return 0; } 3、请写出以下程序的运行结果。 #include <iostream.h> class B; class A { public: A(int i) { a=i; } friend int Fun(A &f1, B &f2); private: int a; }; class B { public: B(int i) { b=i; } friend int Fun(A &f1, B &f2); private: int b; }; int Fun(A &f1,B &f2) { return (f1.a+f2.b)* (f1.a-f2.b); } void main( ) { A n1(10); B n2(8); cout<<Fun(n1,n2)<<endl; } 4、请写出以下程序的运行结果。 #include<iostream> using namespace std; class Complex { private: double real, imag; static int total; public: Complex( double r=0.0, double i=0.0 ) { real=r; imag=i; total++; } int getTotal( ) { return total; } ~Complex() { total--; } }; int Complex::total=0; int main( ) { Complex a(1,2),b,*p; p=new Complex[10]; cout<<a.getTotal()<<endl; delete [ ]p; cout<<a.getTotal()<<endl; return 0; } 三、程序填空题 1、以下程序的功能是从键盘上输入若干学生的学习成绩,统计并输出最高成绩和最低成绩,当输入为负数时结束输入。 #include<iostream> using namespace std; int main() { float x, max, min; cin>>x; max=min=x; while( ① ) { if(x>max) max=x; if( ② ) min=x; cin>>x; } cout<< "\nmax=">> max >> "\nmin="<< min << endl; } 2、以下程序计算两点之间的距离,请将之补充完整。 #include<iostream> #include<cmath> class Point { public: Point(int a=0, int b=0) {x=a; y=b;} int xcord( ) { return x; } int ycord( ) { return y; } private: int x, y; }; class Distance { public: Distance( ① &q1, Point &q2) double getdist( ) { return ② ; } private: Point p1, p2; double dist; }; Distance::Distance(Point &q1, Point &q2):p1(q1), p2(q2) { double x=p1.xcord( )-p2.xcord( ); double y=p1.ycord( )-p2.ycord(); dist=sqrt( ③ ); } void main( ) { Point p(0,0), q(1,1); Distance dist(p, q); cout << “两点之间的距离是:” << dist.getdist( ) << endl; } 3、以下程序实现复数类的“+”号运算符和“-”号运算符的重载,请将之补充完整。 #include <iostream> using namespace std; class Complex {public: Complex( ){real=0;imag=0;} Complex(double r,double i){real=r; imag=i;} friend Complex operator + (Complex &c1, Complex &c2); Complex operator - (Complex &c) void display( ); private: double real, imag; }; Complex operator +(Complex &c1, Complex &c2) { return Complex( ① ); } Complex ② operator – (Complex &c) { return Complex( ③ ); } void Complex::display( ) { cout<<" ("<<real<<", "<<imag<<"i) "<<endl; } int main( ) { Complex c1(3,4), c2(5,-10), c3; c3=c1+c2; c3.display( ); c3=c1-c2; c3.display( ); return 0; } 4、下列 shape 类是一个表示形状的抽象类, area() 为求图形面积的函数。从 shape 类派生三角形类 (triangle) 、矩形类 (rectangle)和圆类(circle) ,请将函数定义补充完整。 class shape { public: ① float area( ) = 0 ; }; class triangle: public shape { float a, b, c; public: triangle(float x, float y, float z): a(x), b(y), c(z) { } float area( ) { float t, s; t=(a+b+c)/2; s=sqrt(t*(t-a)*(t-b)*(t-c)); return s; } }; class rectangle: public shape { float a,b; public: rectangle(int x, int y) { a=x; b=y; } float area( ) { return a*b; } }; class circle: public shape { float r; public: circle(float x){ r=x; } float area( ) { return ② ; } }; 四、编程题 1、定义一个时间类,含有私有成员:时、分、秒,用构造函数给私有成员赋值,再定义一个日期类,含有私有成员:年、月、日,用构造函数给私有成员赋值,再定义一个子类,继承日期和时间类,最后输出日期和时间。 2、声明一个函数模版,将数组内容从小到大排列并打印出来,并写出调用此函数模版的完整程序,调用时,数组的类型可以是整型、字符型或双精度型。 参考答案 一、选择题 CCDCD ACBBD BABBA CCABC 二、写程序运行结果(本题共20分,每小题5分) 1、12,2 2、0202### 3、36 4、12 2 三、程序填空 1、① x>=0 ② x<min 2、① Point ② dist ③ y*y-x*x 3、① c1.real+c2.real, c1.imag+c2.imag ② Complex:: ③ real-c.real, imag-c.imag 4、① virtual ② 3.14*r*r 四、编程题 1、定义一个时间类,含有私有成员:时、分、秒,用构造函数给私有成员赋值,再定义一个日期类,含有私有成员:年、月、日,用构造函数给私有成员赋值,再定义一个子类,继承日期和时间类,最后输出日期和时间,初始值自拟。 #include<iostream> using namespace std; class time { private: int h,m,s; public: time(int x,int y,int z) {h=x;m=y;s=z;} void print1() { cout<<h<<":"<<m<<":"<<s<<endl;} }; class date { private: int y,m,d; public: date(int a,int b,int c) {y=a;m=b;d=c;} void print2() { cout<<y<<"-"<<m<<"-"<<d<<endl;} }; class datetime:public date, public time { public: datetime(int e,int f,int g,int i,int j,int k):date(e,f,g),time(i,j,k) { } }; void main() { datetime dt(2013,11,18,10,11,12); dt.print2(); dt.print1(); } 2、声明一个函数模版,将数组内容从小到大排列并打印出来,并写出调用此函数模版的完整程序,调用时,数组的类型可以是整型、字符型或双精度型。 #include <iostream> #include<cstring> using namespace std; template <class stype> void bubble(stype * item, int count) { int i,j; stype t; for(i=1; i<count; i++) for(j=count-1; j>=i; j--) { if(item[j-1]>item[j]) { t=item[j-1]; item[j-1]=item[j]; item[j]=t; } } } void main() { char str[]="gefadcb"; bubble(str, strlen(str)); cout<<"The sorted string is: "<<str<<endl; int nums[]={9,4,2,6,8,5,1}; bubble(nums,7); cout<<"The sorted number are:"; for(int i=0; i<7; i++) cout<<nums[i]<<' '; cout<<endl; }
展开阅读全文

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


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 通信科技 > 开发语言

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

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

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

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

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

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

客服