Dotcpp  >  试卷列表  >  杭州电子科技大学面向对象程序设计试卷

杭州电子科技大学面向对象程序设计试卷


第1题

友元函数用于允许一个函数访问不相关类的私有部分。

共 2 分 

第2题

构造函数可以被继承。

共 2 分 

第3题

动态绑定的多态性是通过虚函数实现的。

共 2 分 

第4题

在c++中,传引用调用等同于传地址调用。

共 2 分 

第5题

重载函数必须有不同的参数列表。

共 2 分 

第6题

可以用delete释放不是用new运算符分配的内存。

共 2 分 

第7题

类成员的默认访问模式是private。

共 2 分 

第8题

在类Time中的析构函数可以声明为:void ~Time(int);。

共 2 分 

第9题

const对象必须初始化。

共 2 分 

第10题

在c++中,只能重载已有的运算符。

共 2 分 

第11题

关键字____________说明对象或变量初始化后不会被修改。(    )

共 2 分 

第12题

如果调用带有默认参数的函数时缺少一个参数,则________参数就作为这一参数。(    )

共 2 分 

第13题

成员函数可声明为静态的,条件是它不访问__________类成员。(   )

共 2 分 

第14题

内联函数执行起来比标准函数_______________。(   )

共 2 分 

第15题

默认参数的值由________提供。(    )

共 2 分 

第16题

在C++中,混合类型表达式_________。(    )

共 2 分 

第17题

表达式long(intVar)也可表示为________。(     )

共 2 分 

第18题

静态数据成员的生存期_________________。(   )

共 2 分 

第19题

要让一个类中的所有对象具有共同的数据,请使用__________________。(     )

共 2 分 

第20题

设置虚基类的目的是:_________。(    )

共 2 分 

第21题

指出下列程序片段中的错误标号,写出正确语句或解释错在何处。

1)

1
2
3
4
5
6
7
8
int index=675;
const int *ptr=&index;
int *const ntptr=&index;
④*ptr=555;
⑤*ntptr=666;
int another=8;
⑦ptr=&another;
⑧ntptr=&another;

2)

1
2
3
int arrp;
②arrp=new int[15];
delete arrp;
共 20 分 

第22题

下面程序为什么会编译错误,并改正错误(提出解决办法)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class window
{
protected:
int basedata;
};
class border: public window
{ };
class menu: public window
{ };
class border_and_menu: public border,public menu
{
public:int show()
{
return basedata;
}
共 10 分 

第23题

写出下面程序的执行结果:

1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
class A
{
friend double count(A&);
public:
A(double t, double r):total(t),rate(r){}
private:
double total;
double rate;
};
double count(A& a)
{
a.      total+=a.rate*a.total;
return a.total;
}
int main(void)
{
A x(80,0.5),y(100,0.2);
cout<<count(x)<<','<<count(y)<<'\n';
cout<<count(x)<<'\n';return 0;
}

2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;
class Count
{
private:
static int counter;
int obj_id;
public:
Count(); //constructor
static void display_total(); //static function
void display();
~Count(); //destructor
};
int Count::counter; //definition of static data member
Count::Count() //constructor
{
counter++;
obj_id = counter;
}
Count::~Count() //destructor
{
counter--;
cout<<"Object number "<<obj_id<<" being destroyed\n";
}
void Count::display_total() //static function
{
cout <<"Number of objects created is = "<<counter<<endl;
}
void Count::display()
{
cout << "Object ID is "<<obj_id<<endl;
}
int main(void)
{
Count a1;
Count::display_total();
Count a2, a3,a4;
Count::display_total();
a2.display();
a4.display();
return 0;
}

3)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream >
using namespace std;
class BASE
{
char c;
public:
BASE(char n):c(n){}
virtual ~BASE(){cout<<c;}
};
class DERIVED:public BASE
{
char c;
public:
DERIVED(char n):BASE(n+1),c(n){}
~DERIVED(){cout<<c;}
};
int main(void)
{
DERIVED('X');
return 0;
}
共 15 分 

第24题

程序填空:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
class A
{
______(1)______
char name[80];
public:
A( ____(2)______ ) {____(3)______ }
};
class B_____(4)_______
{
public:
B(const char*n)_____(5)_______{}
void PrintName( ) {cout<<”name:”<<name<<endl;};
};
void main( )
{
B b1(“Ling Li”);
b1.PrintName( ) ;
}//执行结果:name: Ling Li
共 15 分 

第25题

改正下面程序段中的错误,写出整个正确的程序段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<T>
void print(T *a)
{ cout<<a<<’\n’;}
void main( )
{
const int x=0;
cout<<y<<’\n’;
int y;
x=5;
int* p
p=&y;
print(p);
return 0;
}

参考答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream.h>//加本句
template<typename T> //加typename
void print(T *a)
{  cout<<a<<'\n';}
void main( )
{
int y=10; //y应先声明后使用,并给初值
const int x=0;
cout<<y<<'\n';
//x=5;x为const,去掉该句
int* p;
p=&y;
print(p);//return 0;main返回为void,去掉该句
}
共 0 分 

第26题

编写程序:定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle(长方形)、Trapezoid(梯形)和Triangle(三角形),用虚函数分别计算各种图形的面积,并求出它们的和。要求用基类指针数组。使它的每一个元素指向一个派生类的对象。

注:主函数中定义如下对象

Circle circle(12.6);

Square square(3.5);

Rectangle rectangle(4.5,8.4);

Trapezoid trapezoid(2.0,4.5,3.2);

Triangle triangle(4.5,8.4);


 参考答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;
class Shape
{public:
virtual double area() const =0;
};
  
class Circle:public Shape
{public:
Circle(double r):radius(r){}
virtual double area() const {return 3.14159*radius*radius;};
protected:
double radius;
};
  
class Square:public Shape
{public:
Square(double s):side(s){}
virtual double area() const {return side*side;}
protected:
double side;
};
  
class Rectangle:public Shape
{public:
Rectangle(double w,double h):width(w),height(h){}
virtual double area() const {return width*height;}
protected:
double width,height;
};
  
class Trapezoid:public Shape
{public:
Trapezoid(double t,double b,double h):top(t),bottom(t),height(h){}
virtual double area() const {return 0.5*(top+bottom)*height;}
protected:
double top,bottom,height;
};
  
class Triangle:public Shape
{public:
Triangle(double w,double h):width(w),height(h){}
virtual double area() const {return 0.5*width*height;}
protected:
double width,height;
};
  
int main()
{
Circle circle(12.6);
Square square(3.5);
Rectangle rectangle(4.5,8.4);
Trapezoidtrapezoid(2.0,4.5,3.2);
Triangle triangle(4.5,8.4);
Shape *pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};
double areas=0.0;
for(int i=0;i<5;i++)
{
areas=areas+pt[i]->area();
}
cout<<"totol of all areas="<<areas<<endl;
return 0;
}
共 0 分 

试卷信息

题量: 26 道
总分: 100 分
一、判断题(1 - 10题,共计20分)
二、选择题(11 - 20题,共计20分)
三、修改程序题(21 - 22题,共计30分)
四、读程序写结果(23题,共计15分)
五、程序填空题(24题,共计15分)
六、编写程序题(25-26题)