青岛理工大学C++考试真题

更新时间:2024-04-22 00:12:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

I.Judgment. (The right answer is “T”, the wrong one is “F” .Each judgment is 2 points, total 20 points.) Number T/F 1 2 3 4 5 6 7 8 9 10

1. 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.

II.Fill 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 a[5], 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 can’t be overload.

8. Class istream is used to support input operation, and class _______ is used to support output operation.

III.Single Choice (Each choice is 2 point, total:10 points) 1. ( )Usually copy constructor’s parameter is

A) the name of some object B) the reference of some object C) member name of some object D) the pointer of some object 2. ( )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 simple C) to increase the run efficiency D) to reduce the object codes 5. ( )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.

Ⅳ.Point out the error the following programming and explain the reason(write out error line number, total:10points)

4. int x=9 , *x_ptr=&x;

1. const size=10; //L1

int y=78;

int a[size]; //L2

*x_ptr = &y ;

2. class Point

{ public:

Point() { c=0; x=-1 ;} //L3 Point(Point pobj); //L4 Point(Point& p) ; //L5 private:

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(){cout<

int m; //L11 }; int main( )

{ Example ex1; //L12 Example 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 ; //L18

5. #include using 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 cout<

Number/ Line 1/ 2/ 3/ 4/ 5/ Reason Number / Line Reason 6/ 7/ 8/ 9/ 10/

Ⅴ.Write out the results of the following programs. (Each subject is 5 points; total:30 points)

1. #include using namespace std;

char str[20]=\char &rep(int k) {

return str[k]; }

int main( ) {

rep(15)='!';

cout<<\return 0; }

2. #include #include using namespace std; int main()

{ double values[]={1.23,35.36,657.6,778.2}; char* names[]={\ for(int i=0;i<4;i++)

cout<

<

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) { cout<

{ Figure fig;

cout<<\ Figure is \

Circle c(3.0);

cout<<“Area of circle is ”; func(c); Rectangle rec(4.0,5.0);

cout<<\ func(rec); return 0; }

4. #include #include using namespace std; int main() {

cout<<“Opening data.txt for appending.”<

fout.open(“data.txt”,ios::app); if ( fout.fail( ) ) {

cout<<“Input file opening failed.”<

fout<<“Hello C++ world!”<

<<“This is an appending program.”<

cout<<“End of appending to file.”<

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; cout<<\ } private:

int a; };

class Base2 { public:

Base2(int i)

{b=i; cout<<\private:

int b; };

class Base3 { public:

Base3(int i)

{c=i;cout<<\ private:

int c; };

class Derivedclass:public Base1 { public:

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;

cout<<\int main()

{ Derivedclass x(5,7,6,8); return 0;

}

6. #include

using namespace std; template class A {

T m; static T n; public :

A(T a) : m(a) { n+=m; } void disp( )

} { cout<<\};

template T 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的坐标:\ p2.input( ); p1.show( ) ; //输出(1,2) p2.show( ) ; cout<<\ 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<<”两者的钱数相等!”<

m.show( ); //按元角分输出 return 0; }

2011~ 2012学年第 一 学期 C++面向对象程序设计 课程试卷 标准答案及评分标准 A(√)/B( ) 卷 _专业 软件103-5

I.Judgment. (The right answer is “T”, the wrong one is “F” .Each judgment is 2 points, total 20 points.)

题号 选项 1 F 2 T 3 F 4 T 5 T 6 T 7 T 8 F 9 T 10 F II.Fill in the blank . (Each blank is 1 point, total:20 points) 题 1 号 填 private 空 题 5 号 填 6 空 2 name 6 catch (…) 3 public and protected 7 ?:, :: 4 abstract class 8 ostream III.Single Choice (Each choice is 1 point, total:10 points) 1.B 2.B 3.D 4.A 5.C

IV Point out the error the following programming and explain the reason Number/ Line Reason 1/L1 2/L3 3/L4 4/L9 5/L14 没有数据类型int c是常数据成员,不能这样初始化 pobj不能是Point类型 构造函数不能有返回类型 Line Number Reason 6/L15 7/L18 8/L23 9/L25 不能使用类名访问成员函数 左值不是一个指针变量 不能直接使用x 不能访问protected成员 不能直接访问私有成员 不能直接用.来访问私有成员 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! 2. Zoot 1.2 3. Area of Figure is 0 Jimmy 35.4 Area of circle is 28.26 A1 657.6 Area of rectangle is 20 Stan 778.2

4. This is a test program!Hello C++ world!

6. m=2,n=5

This is an appending program.

m=3,n=5

5.constructing Base1 a=5

m=1.6,n=7

constructing Base2 b=6 m=5.4,n=7 constructing Base3 c=7

constructing Derivedclass d=8

Ⅵ. Programming Design. (total 20 points) 1.

#include

#include //1 point using namespace std; class Point {public: Point(int xx,int yy) { x=xx; y=yy; } // 2 points Point(){x=y=0;} //2 points void input() //2 points { cin>>x>>y; } void show(){ cout<<'('<

double dist(Point p); //2 points private: 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的坐标:\ p2.input( ); p1.show( ) ; //输出(1,2) p2.show( ) ; cout<<\ return 0; }

2. #include using namespace std; class RMB {

public: RMB (int y,int j,int f){yuan=y;jiao=j;fen=f;} //1 point RMB (int y,int j) { yuan=y; jiao=j; fen=0; } //1 point RMB (int y) { yuan=y; jiao=0; fen=0; } //2 points RMB (){yuan=0;jiao=0;fen=0;}; //1 point void show(){cout<

RMB RMB::operator+(RMB m) { RMB mon; mon.yuan =yuan+m.yuan ; mon.jiao =jiao+m.jiao ; mon.fen =fen+m.fen ;

if (mon.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<<”两者的钱数相等!”<

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 object’s 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 a[3],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 can’t be overload. 选择 1、( )Which are isn’t the member funcition of a class? A)Constructor B)Destructor C)Friend Function D)Copy Constructor 2、( )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 object

5、( ) 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 above

Point 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 char[strlen(p_val)]; strcopy(p_book,p_val); num=num+1; } void getNum()const {return num;} void setNum(int number)const {num=number;} 错误行号 1_1 1_29 1_3 1_22 改错 改成,#include 引号,应该是英文的 改成 CBook }之后加“;” void print() const; ~CBook() {delete[]p_book;} } Void print()const {cout<>book_title; Cbook abook; cout<

private: double x; double y; public:

Move(double a = 0, double b=0); //set x,y to a,b

showmove()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 it reset(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.√

3.A 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. √

5.Just like the structure members, the default access specifier for class members is private. (×) 6.If we defined a constructor in a class, we must define a destructor to destroy it. (×) 7.When passing an object to a function, its copy is created in memory. But it does not, invoke (调用)a class constructor. (×)

8. When instantiating an object of a derived class, constructors of all of its parent class are executed prior to the derived-class constructors. Destructor functions are always executed in the same order. ×

9. Virtual functions are used with inheritance to resolve conflicts when both base and derived class declare member functions with the same signature. ×

10. The member defined in base-class as protected or private can only be accessed by the member functions in derived-class. ×

二、Fill in the blank (each blank has 1 point,total:24 score)

1.A pointer type specifies the type of __object_________ to which the pointer points

2.Passing a reference to a function is like passing the ___ the address of the variable_used as the argument in the function call. 3.A program that uses _______dynamically ___________allocates exactly the amount of memory needed during its execution. 4.The default access specifier(指示符,区分符,说明符) for data members and member function are _____private_______

5.If member function of class don’t proviet to initialize a class object ,___default constructor_____________ is automatically called.

6.When member function is called, _____________of the member function point function called.

7.While the friend function is not a member of class,it has access to the class ______all menber___________ .

8.When declared static data member outside the class, _______class name______________must precede the static data member name.

9.Constant member functions must be used when working with____constant data member ______________.

10. A binary operator can be overloaded by using a ________friend function_______________ rather than

a member function.

11. ______control_______________access specifier is used only when implementing inheritance. 12. To pass arguments from a derived-class to a base-class, a ________derived-class constructor ______________ must be created. NUM

1 2 3 4 5 6 7 8 9 10

RESULT

三、Write out result of the following programs(each subject is 6 point;total:36 point) 1.# include class A { int a; public:

A(int aa=0) { a=aa; }

~A() { cout <<\ } };

class B:public A { int b; public:

B(int aa=0,int bb=0):A(aa) { b=bb; }

~B() { cout <<\ };

void main() { B x(5),y(6,7); }

class Temperature {

private: double temp; static int count; static double totTemp; public: void setTemp(double t) { temp=t; totTemp+=temp; count++; } static double Everage() { return totTemp/count; } };

int Temperature::count=0;

double Temperature::totTemp=0; int main(void) { Temperature t[10]; double temp; int i; for(i=0;i<7;i++) { cout << \输入第\天的温度 :\ cin >> temp; t[i].setTemp(temp); } cout << \七天的平均温度为:\ return 0; }

本文来源:https://www.bwwdw.com/article/h5op.html

Top