Dotcpp  >  试卷列表  >  C++面向对象程序设计试卷

C++面向对象程序设计试卷


第1题

若定义语句为const char *ptr; , ptr应该是______。()

共 3 分 

第2题

对定义重载函数的下列要求中,______是错误的。()

共 3 分 

第3题

有以下类的说明,请指出A、B、C、D四个语句错误的是_____。()

1
2
3
4
5
6
7
class CSample
{int a=2.5;
CSample( );
public:
CSample(int val);
~CSample( );
};


共 3 分 

第4题

一个类的友元函数能够访问该类的_____。()

共 3 分 

第5题

下列运算符中,_____运算符在C++中不能重载。()

共 3 分 

第6题

以下对模板的说明,正确的是_____。()

共 3 分 

第7题

关于多继承二义性的描述中,_____是错误的。()

共 3 分 

第8题

以下基类中的成员函数,哪个表示纯虚函数_____。()

共 3 分 

第9题

在C++中,打开一个文件就是将这个文件与一个_____建立关联;关闭一个文件就取消这种关联。()

共 3 分 

第10题

现有语句

1
2
3
int iarray[] = { 0, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8 };
vector<int
ivector(iarray, iarray + sizeof(iarray) / sizeof(int));

请问要找出ivector之中小于7的元素个数,采用下面哪个算法_____。()

共 3 分 

第11题

请在下面程序的横线处填上适当内容,以使程序完整,并使运行结果为:2/10/2004

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
#include <iostream.h>
static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31};
class date
{int mo,da,yr;
public:
date(int m,int d,int y){mo=m;da=d;yr=y;}
date(){}
void disp(){cout<<mo<<"/"<<da<<"/"<<yr<<endl;}
void leap(int a){
if(a%4==0&&a%100!=0||a%400==0)
dys[1]=29;
else dys[1]=28;}
friend date operator+(_______(1)_______)
{day+=d.da;
d.leap(d.yr);
while(_______(2)_______)
{d.leap(d.yr);
day-=dys[d.mo-1];
if(++d.mo==13)
{d.mo=1;
d.yr++;}
}
d.da=day;
return d;
}
};
int main()
{date d1(2,10,2003),d2;
int n;
cin>>n; //从键盘输入365
while(n<0){cin>>n;}
d2=d1+n;
d2.disp();
}
共 8 分 

第12题

请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:n=30

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream.h>
template <class T>
class Sample
{T n;
public:
Sample(){}
Sample(T i){n=i;}
Sample<T> operator+(const Sample<T> &);
void disp(){cout<<"n="<<n<<endl;}
};
template <class T>
Sample<T> Sample<T>::operator+(const Sample<T> &s){Sample<T> temp;
_______(1)_______;
return temp;
}
int main()
{Sample<int> s1(10),s2(20),s3;
s3=s1+s2;
_______(2)_______;
}
共 8 分 

第13题

请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为

A Constructor2

B Constructor2

x1=12

x2=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
#include <iostream.h>
class A
{public:
A(){cout<<"A Constructor1"<<endl;}
A(int i){x1=i;cout<<"A Constructor2"<<endl;};
void dispa(){cout<<"x1="<<x1<<endl;}
private:
int x1;
};
class B:public A
{
public:
B(){cout<<"B Constructor1"<<endl;}
B(int i): _____(1)_____{x2=i;cout<<"B Constructor2"<<endl;}
void dispb()
{_____(2)_____;
cout<<"x2="<<x2<<endl;
}
private:
int x2;
};
int main()
{B b(2);
b.dispb();
}
共 8 分 

第14题

请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为

构造 base 对象

调用 base::f()

构造 derive 对象

调用 derive::f()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream.h>
class base
{
public:
base()
{cout<<"构造base对象"<<endl;
f();
}
_________(1)_________{cout<<"调用base::f()"<<endl;}
};
class derive:public base
{
public:
derive()
{cout<<"构造derive对象"<<endl;
_________(2)_________;
}
void f(){cout<<"调用derive::f()"<<endl;}
};
int main()
{
derive d;
}
共 8 分 

第15题

下面程序用STL的条件计数算法和自定义的函数对象对一个存放在整数向量类对象中的学生成绩进行统计及格人数并显示结果。请在下面程序的横线处填上适当字句,以使程序完整。

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
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
class Pass
{
public:
____________________(1)___________________
{
return x>=60;
}
};
int main()
{
vector<int> a;
}
void disp(){cout<<"n="<<n<<endl;}
};
intmain()
{Sample s(10);
s--;
s.disp();
}
int count;
a.push_back(78);
a.push_back(92);
a.push_back(52);
count = ____________________(2)___________________;
cout<<"count="<<count<<endl;
}
共 8 分 

第16题

读程序写结果

1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream.h>
class CSample
{
private:
int n;
static int k;
public:
CSample(int i){n=i;k++;}
void disp();
};
void CSample::disp()
{cout<<"n="<<n<<",k="<<k<<endl;}
int CSample::k=0;
int main()
{CSample a(10),b(20),c(30);
a.disp();
b.disp();
c.disp();
}

2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream.h>
class Sample
{int n;
public:
Sample(){}
Sample(int m){n=m;
}
Sample operator--(int)
{Sample old=*this;
n--;
return old;
}
void disp(){cout<<"n="<<n<<endl;}
};
int main()
{Sample s(10);
s--;
s.disp();
}

3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream.h>
#include <string.h>
template <class T>
T max(T x, T y)
{
return (x > y ? x : y);
}
char *max(char *x, char *y)
{
if (strcmp(x, y) >= 0)
return x;
else
return y;
}
int main()
{
cout << max(2, 5) << endl;
cout << max(3.14,4.12) << endl;
cout << max("China""Beijing") << endl;
}

4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stack>
#include <iostream>
#include <string>
using namespace std;
int main() {
stack<char> s;
string str;
cin >> str;//从键盘输入字符串LoveChina
for(string::iterator iter = str.begin(); iter != str.end(); ++iter)
s.push(*iter);
while (!s.empty()) {
cout << s.top();
s.pop();
}
cout << endl;
}

5.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int divide(int x, int y) 
{
if (y == 0)
throw x;
return x / y;
}
int main() 
{try {
cout << "8/3 = " << divide(8, 3) << endl;
cout << "6/0 = " << divide(6, 0) << endl;
cout << "9/2 = " << divide(9, 2) << endl;
catch (int e) {
cout << e << " is divided by zero!" << endl;
}
cout << "That is ok." << endl;
}
共 30 分 

第17题

编程实现小型公司的工资管理。该公司主要有4类人员:经理(manager)、兼职技术人员(technician)、销售员(salesman)和销售经理(salesmanager)。要求存储这些人员的编号、姓名和月工资,计算月工资并显示全部信息。月工资计算办法是:经理拿固定月薪8000元,兼职技术人员按每小时100元领取月薪,销售员按当月销售额的4%提成,销售经理既拿固定月工资也领取销售提成,固定月工资为5000元,销售提成为所管辖部门当月销售总额的千分之五。兼职技术人员一个月工作小时数、销售员一个月销售额、销售经理所管辖部门一个月销售总额由各个类的成员函数完成设置。(要求用抽象类和类继承)

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
using namespace std;
//雇员类
class Employee {
protected :
int _id;
double _salary;
string _name;
public:
Employee(int id,string name)
:_id(id),_name(name)
{}
virtual ~Employee(){}
virtual double CalcSalary()=0;
};
//经理类
class Manager:public Employee{
public:
Manager(int id,string name):Employee(id,name){}
virtual double CalcSalary(){
_salary=8000;
return _salary;
};
};
//兼职技术员类
class Technician:public Employee{
int _workHour;
public:
Technician(int id,string name):Employee(id,name){}
void SetWorkHour(int workHour){
_workHour = workHour;
}
virtual double CalcSalary(){
_salary=100*_workHour;
return _salary;
};
};
//销售经理类
class Salesman:public Employee{
double _salesMount;
public :
Salesman (int id,string name):Employee(id,name){}
void SetSalesMount(double salesMount){
_salesMount=salesMount;
}
virtual double CalcSalary(){
_salary=0.04*_salesMount;
return _salary;
};
};
class SalesManager:public Employee{
double _salesTotalMount;
public:
SalesManager (int id,string name):Employee(id,name){}
void
SetSalesTotalMount(double salesTotalMount){
_salesTotalMount=salesTotalMount;
}
virtual double CalcSalary(){
_salary =5000 +0.005 * _salesTotalMount;
return _salary;
};
};
int main()
{
Manager zhang(1000, "张三");
Technician li(1000, "李四");
Salesman wang(1000, "王五");
SalesManager zhao(1000, "赵六");
li.SetWorkHour(50);
wang.SetSalesMount(175000);
zhao.SetSalesTotalMount(1500000);
Employee *pEmployeeA[]={&zhang,&li,&wang,&zhao};
for (int i = 0; i <4; i++) {
cout << pEmployeeA [i] ->CalcSalary() << endl;
}
}
共 0 分 

第18题

定义一个矩阵类(Matrix),设计相关的构造函数、析构函数等,采用运算符重载方式实现矩阵的加、减运算。编写main函数,对以上所有功能进行测试。注意:按照实际情况大小使用内存空间,矩阵的加、减运算不要造成内存浪费。

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <vector>
#include <cstdlib>
using
namespace std;
class
Matrix {
public:
Matrix (int row, int col); //构造row x col大小矩阵
Matrix operator + (const Matrix &rhs) const;//相同大小矩阵相加
Matrix operator - (const Matrix &rhs) const;//相同大小矩阵相减
void Display () const;//显示矩阵
//矩阵元素,可作为右值
const int &at (int row, int col) const {
return _datas [row][col];
}
//矩阵元素,可作为左值
int& at (int row, int col) {
return _datas [row][col];
}
//矩阵行数
int GetRowCount () const {
return _datas.size ();
}
//矩阵列数
int GetColCount () const {
return _datas [0].size ();
}
private:
//存放矩阵元素,本类没有直接动态分配,无需重载拷贝构造和赋值
vector<vector<int> > _datas;
};
Matrix::Matrix (int row, int col)
{
for (int i = 0; i < row; i++) {
vector<int> dataRow (col);
_datas.push_back (dataRow);
}
}
Matrix Matrix::operator + (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) + rhs.at (i, j);
}
}
return result;
}
Matrix Matrix::operator - (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) - rhs.at (i, j);
}
}
return result;
}
void Matrix::Display () const
{
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
cout.width (5);
cout << at (i,j);
}
cout << endl;
}
cout << endl;
}
int main()
{
Matrix A (5, 6), B (5, 6);
int i, j;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
A.at(i, j) = rand () % 100 ;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
B.at(i, j ) = rand () % 100;
A.Display();
B.Display();
Matrix C = A + B;
C.Display();
Matrix D = A - B;
D.Display();
return 0;
}#include <iostream>
#include <vector>
#include <cstdlib>
using
namespace std;
class
Matrix {
public:
Matrix (int row, int col); //构造row x col大小矩阵
Matrix operator + (const Matrix &rhs) const;//相同大小矩阵相加
Matrix operator - (const Matrix &rhs) const;//相同大小矩阵相减
void Display () const;//显示矩阵
//矩阵元素,可作为右值
const int &at (int row, int col) const {
return _datas [row][col];
}
//矩阵元素,可作为左值
int& at (int row, int col) {
return _datas [row][col];
}
//矩阵行数
int GetRowCount () const {
return _datas.size ();
}
//矩阵列数
int GetColCount () const {
return _datas [0].size ();
}
private:
//存放矩阵元素,本类没有直接动态分配,无需重载拷贝构造和赋值
vector<vector<int> > _datas;
};
Matrix::Matrix (int row, int col)
{
for (int i = 0; i < row; i++) {
vector<int> dataRow (col);
_datas.push_back (dataRow);
}
}
Matrix Matrix::operator + (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) + rhs.at (i, j);
}
}
return result;
}
Matrix Matrix::operator - (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) - rhs.at (i, j);
}
}
return result;
}
void Matrix::Display () const
{
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
cout.width (5);
cout << at (i,j);
}
cout << endl;
}
cout << endl;
}
int main()
{
Matrix A (5, 6), B (5, 6);
int i, j;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
A.at(i, j) = rand () % 100 ;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
B.at(i, j ) = rand () % 100;
A.Display();
B.Display();
Matrix C = A + B;
C.Display();
Matrix D = A - B;
D.Display();
return 0;
}
共 0 分 

试卷信息

题量: 18 道
总分: 100 分
一、选择题(1 - 10题,共计30分)
二、程序填空题(11 - 15题,共计40分)
三、程序阅读题(16题,共计30分)
四、编程题(17-18题)