青岛理工大学C++考试真题_第1页
青岛理工大学C++考试真题_第2页
青岛理工大学C++考试真题_第3页
青岛理工大学C++考试真题_第4页
青岛理工大学C++考试真题_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

IJudgment. (The right answer is “T”, the wrong one is “F” .Each judgment is 2 points, total 20 points.)Number12345678910T/F1. Both constructor and destructor of base class can be inherited by derived class. 2. Static data member is shared by all objects of this class. 3. Constructors can be declared as virtual function. 4. When overloading an operator, at least one argument of the resulting overloaded operator must be of a class type. 5. Constructors have no return type. 6. A friend function is not a member of the class, but it can access the private members of that class. 7. We can only list types of formal parameters in function declarations.8. There are loop statements can be wrote in the inline function.9. The assignment operator can only be overloaded into a member function. 10.In C+, STL is a standard function template library.IIFill in the blank . (Each blank is 1 point, total:10 points)1. The default access label for class is _.2. The constructor and the destructor must have the same as the class,.3. The member functions of derived classes can get access to _ _ members and _ members of a base class, do not get access to private members of a base class. 4. In C+, a class with a pure virtual function is called an _.5. Suppose we have a class named as Dc, when such code like “Dc a5, b(2),*p;” is implemented, the system will auto-call the constructor of class Dc for _ times. 6. Statement _ can catch all kinds of exceptions. 7. In C+, in addition to “.”, “*”, “sizeof”, the operator _ and _ are also cant be overload.8. Class istream is used to support input operation, and class _ is used to support output operation. IIISingle Choice (Each choice is 2 point, total:10 points)1. ( )Usually copy constructors parameter isA) the name of some object B) the reference of some objectC) member name of some object D) the pointer of some object2. ( )Which is wrong about constructor? A) The compiler may supply default constructor.B) Constructors can have parameters, and returned value.C) Constructors can be overloaded.D) Constructors can have default parameter value.3. ( )Which is wrong about friend?A) The keyword friend is used to declare the friend.B) The member function of one class can be the friend of another class.C) The friend functions can access any specified access label member.D) The friend functions access the member of some object by this pointer. 4. ( )The purpose of virtual base class is. A) to eliminate(消除)ambiguity B) to make a program simpleC) to increase the run efficiency D) to reduce the object codes5. ( )Which is right about the static data member?A) The static data member can not be called by the object name.B) The static data member can be initialized in the constructors.C) The static data member belongs to a certain class.D) The static data member must not be initialized in the file scope.4. int x=9 , *x_ptr=&x; /L16 int y=78; /L17*x_ptr = &y ; /L18.Point out the error the following programming and explain the reason(write out error line number, total:10points)1. const size=10; /L1int asize; /L22. class Point public: Point() c=0; x=-1; /L3 Point(Point pobj); /L4 Point(Point& p); /L5private: int x; /L6 const int c; /L7;3. #include using namespace std;class Example public: Example (int i)m=i; /L8 void Example() m=0; /L9 void show()coutm; /L10 private: int m; /L11; int main( ) Example ex1; /L12Example ex2(5); /L13 ex2.m+=10; /L14 Example.show(); /L15 return 0; 4. int x=9 , *x_ptr=&x; /L16 int y=78; /L17*x_ptr = &y ; /L185. #includeusing namespace std;class BC public: void set_x(int a) x=a; /L19 protected: int get_x() const return x; /L20 private: int x;class DC : public BC public: void add2( ) /L21 int c=get_x( ); set_x(c+2); /L22 void add3( ) x+=3; /L23;int main( ) DC d; d.set_x(3); /L24 coutd.get_x( )endl; /L25 d.x=77; /L26 d.add2(); /L27 return 0; Number/ LineReasonNumber / LineReason1/6/2/7/3/8/4/9/5/10/Write out the results of the following programs. (Each subject is 5 points; total:30 points)1. #include using namespace std;char str20=Hello C+ world.;char &rep(int k)return strk; int main( )rep(15)=!;coutThe string is:strendl;return 0;2. #include #include using namespace std;int main() double values=1.23,35.36,657.6,778.2; char* names=Zoot,Jimmy,A1,Stan; for(int i=0;i4;i+) coutleftsetw(6)namesi rightsetw(6) fixedsetprecision(1) valuesiendl; return 0;3. #include const double PI=3.14;using namespace std;class Figure public: Figure(); virtual double area() const return 0.0;class Circle : public Figure public: Circle(double myr)R=myr; double area() const return PI*R*R;protected: double R;class Rectangle : public Figure public: Rectangle (double myl,double myw) L=myl;W=myw;double area() const return L*W;private: double L,W;void func(Figure &p) coutp.area()endl;int main() Figure fig; coutArea of Figure is ; func(fig);Circle c(3.0); cout“Area of circle is ”; func(c); Rectangle rec(4.0,5.0); coutArea of rectangle is ; func(rec); return 0;4. #include #include using namespace std;int main() cout“Opening data.txt for appending.”endl; ofstream fout; fout.open(“data.txt”,ios:app); if ( fout.fail( ) ) cout“Input file opening failed.”endl; exit(1); fout“Hello C+ world!”endl “This is an appending program.”endl; fout.close(); cout“End of appending to file.”endl; return 0;Suppose that there is the content: “This is a test program!” in the data.txt.when the program executes, what content will exist in the data.txt? 5. #include using namespace std;class Base1 public: Base1(int i) a=i; coutconstructing Base1 a= aendl; private:int a; ;class Base2 public: Base2(int i)b=i; coutconstructing Base2 b= bendl;private: int b;class Base3 public: Base3(int i)c=i;coutconstructing Base3 c= cendl; private: int c;class Derivedclass:public Base1public: Derivedclass(int i,int j,int k,int m); private: int d;Base2 f;Base3 g; ; Derivedclass:Derivedclass(int i,int j,int k,int m):Base1(i),g(j),f(k) d=m;coutconstructing Derivedclass d=dendl; int main() Derivedclass x(5,7,6,8);return 0;6. #includeusing namespace std;templateclass A T m; static T n; public: A(T a): m(a) n+=m; void disp( ) coutm=m,n=nendl; ;templateT A:n=0;int main() A a(2) , b(3); a.disp(); b.disp(); A c(1.6),d(5.4); c.disp(); d.disp(); return0;Programming Design. (total 20 points)1. (10 points) To define a Point class to satisfy the given test function main: int main()Point p1(1,2), p2;cout请输入点p2的坐标:endl; p2.input( );p1.show( ) ; /输出(1,2)p2.show( ) ; coutThe distance of point p1 and point p2 is p1.dist(p2)endl;return 0; 2. (10 points) To design a RMB class to satisfy the given test function main: int main() RMB m1(100,2,8), m2(200,9), m; if( m1=m2) cout”两者的钱数相等!”endl; else cout”两者的钱数不相等!”endl; m=m1+m2; m=m+50;m.show( ); /按元角分输出 return 0;2011 2012学年第 一 学期 C+面向对象程序设计 课程试卷标准答案及评分标准 A()/B( ) 卷 _专业 软件103-5 IJudgment. (The right answer is “T”, the wrong one is “F” .Each judgment is 2 points, total 20 points.)题号12345678910选项FTFTTTTFTFIIFill in the blank . (Each blank is 1 point, total:20 points)题 号1234填空privatenamepublic and protected abstract class题 号5678填空6catch ()?:, :ostreamIIISingle Choice (Each choice is 1 point, total:10 points)1B 2.B 3.D 4.A 5.CIV Point out the error the following programming and explain the reasonNumber/ LineReasonLine NumberReason1/L1没有数据类型int6/L15不能使用类名访问成员函数2/L3c是常数据成员,不能这样初始化7/L18左值不是一个指针变量3/L4pobj不能是Point类型8/L23不能直接使用x4/L9构造函数不能有返回类型9/L25不能访问protected成员5/L14不能直接用.来访问私有成员10/L26不能直接访问私有成员Write out the results of the following programs. (Each subject is 5 points; total:30 points)1. The string is Hello C+ world!3. Area of Figure is 0Area of circle is 28.26Area of rectangle is 202. Zoot 1.2 Jimmy 35.4 A1 657.6 Stan 778.26. m=2,n=5m=3,n=5m=1.6,n=7m=5.4,n=74. This is a test program!Hello C+ world!This is an appending program. 5.constructing Base1 a=5constructing Base2 b=6constructing Base3 c=7constructing Derivedclass d=8. Programming Design. (total 20 points)1.#include #include /1 pointusing namespace std;class Pointpublic:Point(int xx,int yy) x=xx; y=yy; / 2 pointsPoint()x=y=0; /2 pointsvoid input() /2 points cinxy; void show() cout(x,y)endl; / 1 pointdouble dist(Point p); /2 pointsprivate:int x;int y; ;double Point:dist(Point p) return sqrt(p.x-x)*(p.x-x)+(p.y-y)*(p.y-y); int main()Point p1(1,2), p2;cout请输入点p2的坐标:endl; p2.input( );p1.show( ) ; /输出(1,2)p2.show( ) ; coutThe distance of point p1 and point p2 is p1.dist(p2)endl;return 0; 2. #includeusing namespace std;class RMBpublic:RMB (int y,int j,int f)yuan=y;jiao=j;fen=f; /1 pointRMB (int y,int j) yuan=y; jiao=j; fen=0; /1 pointRMB (int y) yuan=y; jiao=0; fen=0; /2 pointsRMB ()yuan=0;jiao=0;fen=0; /1 pointvoid show()coutyuan元jiao角fen分=10) mon.fen -=10;mon.jiao +;if(mon.jiao =10) mon.jiao -=10;mon.yuan +;return mon;int main() RMB m1(100,2,8), m2(200,9), m; if( m1=m2) cout”两者的钱数相等!”endl; else cout”两者的钱数不相等!”endl; m=m+50; m.show( ); /按元角分输出 return 0;Judgment.(The right answer is “T”,the wrong one is “F”.)The keyword friend is used in a function definition,not in a function declaration.(F)Values stored in the elements of a vector may be accessed the same way as values stored in the elements array.()A class can only have one subclass.(F)It is not possible to access the attributes of a class with no member fuctions(methods).(F)The meaning of all the C+ operators(such as *,+etc.)is fixed and cannot be changed.(T)If you do not declare a constructor in a class, the compiler will furnish a default constructor for you automatically.(T)An object of a class A can be a private member of a class B.(T)Because we cannot instantiate objects of abstract base classes, we cannot declare pointers and references to abstract base classes.(F)In dynamic binding an objects type must be known at compile time for a virtual function call to be compiled.()The assignment operator can only be overloaed into a nonmember function.(F)Fill in the blank.There are four stream object defined in ,the object_cerr_is the standard error output stream.In C+,Encapsulation is achieved by making_class(类)_.In a class,maximum number of destructors are_1_.Assume that A is a class,the constructor of class A had be called_4_times when the statement “A a3,b(6),*p;”was executed.Assume that the name of type parameter is defined as T,when defining the function template,you should add prefix statement_template_.If the name of a is Myclass,then the name of its destructor is_it has _return values.If class B is derived from class A,class C is derived from class B,an object of class C is created,then the called order of constructor is_In the C+,in addition to “,”,” * ”,”sizeof ”,the operator_and_are also cant be overload. 选择1、( )Which are isnt the member funcition of a class?A)Constructor B)Destructor C)Friend Function D)Copy Constructor2、( )Opening the file”d:file.dat”and write data to it,which statement should we use?A)ifstream infile(“d:file.dat”,ios:in);B)ifstream infile(“d:file.dat”,ios:in);C)ofstream infile(“d:file.dat”,ios:out); D)fstream infile(“d:file.dat”,ios:in/ios:out);3、( )Which is wrong about class? A)Class is one kind of type, it encapsulates data and operates. B)Object is a instance of a class. C)One object can only belong to a class. D)There are only one object in a class.4、( )Usually the parameter of a copy constructor is_. A) a object B)the member of a object C)the reference of a object D)the pointer of a object5、( ) If there are a pure virtual function in a class,then we call this class as_ A)abstract class B)virtual basic class C)derived class D)none of abovePoint out the error the following programming and explain the reason(write out the error line member,total:15 points)#include using namespace std; class Cbook private: char *p_book; int num; public: void CBook(const char *p_val)p_book=new charstrlen(p_val);strcopy(p_book,p_val);num=num+1;void getNum()constreturn num;void setNum(int number)constnum=number;void print() const;CBook()deletep_book;Void print()constcoutp_bookendl;Void main()char book_title60;CBook *p_book_obj;coutbook_title;Cbook abook;coutabook.num;p_book_obj=abook;p_book_obj.print();错误行号 改错错误行号 改错1_1改成,#include1_23改成“void CBook:printf”1_29引号,应该是英文的1_3改成 CBook1_22之后加“;” 编程1、class Moveprivate:double x;double y;public:Move(double a = 0, double b=0); /set x,y to a,bshowmove()const; /shows current x,y values;Move add(const Move & m)const; /this function adds x of m to x of invoking object to get /new x, adds y of m to y of invoking /object to get new y,create a new /move object initialized to new x,y values, and returns itreset(double a=0,double b=0); /reset x,y to a,b;Int main() Move Point1(3,4),Point2,Point3;cout”x and y values of Point1 are:”;Point1.showmove();Point2.reset(5,6);Point3=Point1.add(Point2);cout”x and y values of Point3 are:”;Point3.showmove();return 0; Create member function definitions to satisfy the test function main.2、(9 points)Deine a Complex class,it has two private member real and image.1)Please write a constroctor which can accept 0-2 parameters,a copy constructor, a Print function which can display the value of data member;2) Write Set function:setReal and setImage,which can change the value of private data member;3)Overload operator “+”and”-”;which can finish addition and subtraction of two complex.The main() function listed as following:void main()Complex c1(1.5,7.9);Complex c2(c1);c2.setReal(2.7);c2.setImage(0);c1.Print();c2.Print();(c1+c2).Print();(c1-c2).Print();一、Judgement(Please judge the following 10 sentence, and full the blank of the list with your result. The right answer is “”, the wrong one is “” .20 point.) 1. We can assign a value to a constant when it is declared. 2. int sort (int arr ) and float sort (float arr ) are overloaded functions.3A function that the compiler generates using a function template is called an instance of the function template or a template function. 4 A constant pointer to a constant cannot be changed to point to another constant of the same type. 5Just like the structure members, the default access specifier for class members is private. ()6If we defined a co

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论