您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页c++期末复习练习题2及答案

c++期末复习练习题2及答案

来源:华佗小知识
 练习题2

一、单项选择题(每小题2分,共计40分) 1、一个C++程序的执行总是从_ __。 A、本程序的第一个函数开始,到本程序文件的最后一个函数结束 B、本程序的第一个函数开始,到本程序的main( )函数结束 C、本程序的main函数开始,到main函数结束 D、本程序的main函数开始,到本程序的最后一个函数结束 2、C++源程序文件的扩展名是_ _。 A、.cpp B、.c C、.obj D、.exe 3、下列函数中_ __不属于类的成员函数。 A、友元函数 B、默认构造函数 C、析构函数 D、带参的构造函数 4、在A类内部定义其析构函数,格式正确的是_ __。 A、~A(){} B、int ~A(){return 0;} C、void ~A(){} D、int ~A(int a){return a;} 5、以下程序的输出结果是_ _。 class Father class Sun:public Father {public: {public: Father() Sun() {cout<<\"Father created!\";} {cout<<\"Sun created!\"<void set(int a) return 0; {a = aa;} } private: int aa; }; A、A objA; 不正确,没有匹配的构造函数 B、a = aa; 不正确,应为aa = a; C、objA.aa = 2; 不正确,aa是私有数据,不能直接在类外访问 D、void A(int a):aa(a){}构造函数不正确,应该是void A(int a) {a = aa; } 8、下列对变量的引用中,错误的是__。 A、int a; int &p = a; B、char a; char &p = a; C、int a; int &p; p = a; D、float a; float &p = a; 9、主函数中,哪个语句出错_ _。 class Complex {public: Complex(i = 1.0) {real = 2.5; imag = i; } private: float real; // 实部 float imag; // 虚部 }; int main( ) { Complex num1(2.5); Complex num2 = num1; Complex num3(2.5,3.0); num3 = num2; return 0; } A、Complex num1(2.5); B、Complex num2 = num1; C、Complex num3(2.5,3.0); D、num3 = num2; 10、已知类A的一个成员函数声明: void set(A & a); 则A & a的含义是_ 。 A、指向类A的指针为a B、将a的地址值赋给变量set C、a是类A的对象的引用 D、变量A与a按位相与 11、下列各函数的说明中_ __表示纯虚函数。 A、virtual int foo(int x); B、void foo() =0; C、virtual void foo() =0; D、virtual void foo( ) {} 12、假设A类已定义,在主函数中有A objA[3];下列说法中正确的是_ __。 A、objA是一个对象,调用一次构造函数,初始化值为3 B、objA是一个对象数组,调用一次构造函数,数组的三个元素均初始化为3 C、objA是一个对象数组,调用三次构造函数 D、objA是一个对象数组,不调用构造函数 13、关于B类对象对数据的访问权限,说法正确的是_ __。 class A class B:public A {public: {public: void fa(); void fb(); protected: private:

int aa; float bb; }; }; A、public: fa(), fb() protected: aa private: bb B、public: fb() protected: fa() , aa private: bb C、public: fb() private: cc D、public: fa(), fb(), aa private: bb 14、以下程序输出结果是_ __。 #include using namespace std; void change(int x, int y) { int t = x; x = y; y = t; } int main() { int a = 2, b = 6; change(a,b); cout< using namespace std; void change(int &x, int &y) { int t = x; x = y; y = t; } int main() { int a = 2, b = 6; change(a,b); cout<}; A、add()函数 B、get()函数 C、change()函数 D、display()函数 18、在Time内完成两个Time类对象相加功能的函数声明,正确的是__。 class Time {public: Time(int h,int m):hour(h),minute(m){} private: int hour; int minute; }; A、friend Time operator+(Time t1, Time t2); B、friend Time operator+(Time t); C、void operator+(Time t); D、void operator+(Time t1, Time t2); 19、实现运行时的多态性要使用_ __。 A、重载函数 B、构造函数 C、析构函数 D、虚函数 20、以下运算符中不能重载的是_ __。 A、!= B、?: C、/ D、+ 二、填空(每空1分,共计14分) 1. C++使用cin和cout对已定义的变量x进行输入输出操作的程序必须先写______和______,输入语句形式为______,输出语句形式为______。 2. C++支持面向对象程序设计的四个特性是:__ ___、_____、_ _____、___ ___。信息隐蔽是通过其中的__ ____特性来实现的。 3. 任何类中允许有____、_____、_____三种访问权限的成员(使用英文)。 4. string str1 = \"hello \"; string str2 = \"world!\"; string str3 = str1 + str2; 则str3为_____。 5. int fun(int a, float b); int fun(int a); 这两个函数属于__ ___。 三、程序填空(每小题4分,共计8分) 1.class Base {public: void funB() {cout<<\"Base called!\"<{cout<<\"Base called!\"< using namespace std; class sample {public: sample(int x, int y ) { a = x; b = y ;} friend int sum(sample model); private: int a,b; }; int sum(sample model) { return model.a+model.b; } int main( ) { sample sam(15,20); cout< #include class String {public: String(char *str); friend bool operator>(String &string1,String &string2); friend bool operator==(String &string1,String &string2); void display(); private: char *p; }; String::String(char *str) {p=str;} void String::display() {cout<(String &string1,String &string2) { if(strcmp(string1.p,string2.p)>0) return true; else return false;

} bool operator==(String &string1,String &string2) { if(strcmp(string1.p,string2.p)==0) return true; else return false; } int main() { String string1(\"book\"),string2(\"Book\"); cout<<(string1>string2)< using namespace std; class Circle {public: Circle(float a):r(a){} float area(){return 3.14159*r*r;} protected: float r; }; class Cylinder:public Circle {public: Cylinder(float a, float b):Circle(a),h(b){} float area(){return 2*3.14159*r*r+2*3.14159*r*h;} private: float h; }; int main() { Circle c(1.0); Cylinder cy(1.0,1.0); cout< using namespace std; class Circle {public: Circle(float a):r(a){} virtual float area(){return 3.14159*r*r;} protected: float r; };

class Cylinder:public Circle {public: Cylinder(float a, float b):Circle(a),h(b){} float area(){return 2*3.14159*r*r+2*3.14159*r*h;} private: float h; }; int main() { Circle c(1.0),*p; Cylinder cy(1.0,1.0); p = &c; cout<area()<area()<一、单项选择题(每小题2分,共计40分) 1、C 2.A 3、A 4、A 5、C 6、B 7、D 8、C 9、C 10、C 11、C 12.C 13、A 14、A 15、B 16、D 17、A 18、A 19、D 20、B 二、填空(每空1分,共计14分) 1. #include using namespace std; cin>>x; cout< using namespace std; class Clock {public: Clock() {hour = 0; minute = 0; sec = 0;} void setTime(int h, int m, int s) {hour = h;minute = m;sec = s;} void operator++() { sec++; if(sec >= 60) { sec = sec - 60; minute++; } if(minute >= 60) { minute = minute - 60; hour++;

} if(hour>=24) hour = hour - 24; } void ring() { } void displayTime() { cout<Clock myClock; myClock.setTime(8,20,0); while(1) { } myClock++; myClock.displayTime(); myClock.ring(); return 0;

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务