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

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


第1题

下面关于友元的描述中,错误的是( )。

共 3 分 

第2题

下列对重载函数的描述中,( )是错误的。

共 3 分 

第3题

有如下程序:

1
2
3
4
5
6
7
8
#include<iostream.h>
class Test
{
public:
Test (){}
~Test() { cout << ‘#’; }
};
void main(){Testtemp[2],*pTemp[2];}

执行这个程序输出“#”号的个数为( )。

共 3 分 

第4题

const int *p说明不能修改( )。

共 3 分 

第5题

友元运算符obj1>obj2被C++编译器解释为( )。

共 3 分 

第6题

现有语句

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

请问要找出ivector之中大于2的第一个元素所在位置的元素,采用下面哪个算法( )。

共 3 分 

第7题

下列关于虚基类的描述中,错误的是( )。

共 3 分 

第8题

在下面的程序中,A、B、C、D四个语句编译时出现错误的是( )。

1
2
3
4
5
6
class A                 //A 
{
public:                 //B
A() {func() ;}               //C
virtual void func()=0;               //D
};
共 3 分 

第9题

若定义cin>>str;,当输入Object Windows Programming!所得的结果是str=( )。

共 3 分 

第10题

类模板的模板参数( )。

共 3 分 

第11题

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

hELLO

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
#include <iostream.h>
#include <string.h>
class Words
{char *str;
public:
Words(char *s)
{str=new char[strlen(s)+1];
strcpy(str,s);
}
void disp(){cout<<str<<endl;}
char operator[](int i)
{if(str[i]>='A'&&str[i]<='Z')
return char(str[i]+32);
else if(str[i]>='a'&&str[i]<='z')
return char(str[i]-32);
else 
return str[i];
}
};
int main()
{int i;char *s="Hello";
Words word(s);
______________(1)____________;
for (i=0;i<strlen(s);i++)
______________(2)____________;
cout<<endl;
}
共 8 分 

第12题

利用函数模板,设计求一个数组元素之和的函数sum和两个数组元素之和的函数sum,请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

数组a之和:15

数组b之和:55

两数组之和:70

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream.h>
template <class T>
T sum (T a[],int n)
{int i;
T s=0;
for(i=0;i<n;i++)
s+=a[i];
return s;
}
template <class T>
T sum(______________(1)____________)
{
return ______________(2)____________;
}
int main()
{int a[5]={1,2,3,4,5};
int b[10]={1,2,3,4,5,6,7,8,9,10};
int s1=sum(a,5);
int s2=sum(b,10);
int s3=sum(a,5,b,10);
cout<<"数组a之和:"<<s1<<endl;
cout<<"数组b之和:"<<s2<<endl;
cout<<"两数组之和:"<<s3<<endl;
}
共 8 分 

第13题

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

(1,2)

5,6

(6,9)

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
#include <iostream.h>
class A
{
public:
A(int i,int j){a=i;b=j;}
void move(int x,int y){ ____(1)______;b+=y;}
void show(){cout<<"("<<a<<","<<b<<")"<<endl;}
private:
int a,b;
};
class B:private A
{
public:
B(int i,int j,int k,int l):A(i,j){x=k;y=l;}
void show(){cout<<x<<","<<y<<endl;}
void fun(){move(3,5);}
void fl(){____(2)______;}
private:int x,y;
};
int main()
{A e(1,2);
e.show();
B d(3,4,5,6);
d.fun();
d.show();
d.fl();
}
共 8 分 

第14题

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

10+10i

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream.h>
class complex
{
int x,y;
public:complex(){}
complex(int i,int j)
{x=i; y=j;}
complex operator+(complex &p)
{________(1)_______;}
void show()
{cout<<x<<"+"<<y<<"i";}
};
int main()
{complex p1(5,8),p2(5,2),p3;
________(2)_______;
p3.show();
共 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
20
21
22
23
#include <iostream.h>
class B
{
public:
B(){}
B(int i,int j){a=i;b=j;}
void printb()
{cout<<"a="<<a<<",b="<<b<<endl;}
private:
int a,b;
};
class A
{
B c;
public:
A(){}
A(int i,int j):c(i,j){}
void printa(){c.printb();}
};
int main()
{A a(7,8);
a.printa();
}

2、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream.h>
class CArray
{int *m_pArray;
int m_iSize;
public:
CArray(int iArray[],int iSize)
{m_pArray=iArray;
m_iSize=iSize;
}
int GetSize()
{return m_iSize;}
int &operator[](int i)
{return m_pArray[i];}
};
int main()
{int s[]={3,7,2,1,5};
CArray oArray(s,5);
oArray[0]=9;
for(int i=0;i<5;i++)
cout<<oArray[i]<<" ";
cout<<endl;
}

3、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream.h>
#include <math.h>
template <class T>
class TAdd
{T x,y;
public:
TAdd(T a,T b){x=a,y=b;}
T add(){return x+y;}
};
int main()
{TAdd<int> A(5,6);
TAdd<double> B(2.4,5.8);
cout<<"s1="<<A.add()<<endl;
cout<<"s2="<<B.add()<<endl;
}

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
#include <iostream.h>
class base
{
public:
void who(){cout<<"base class"<<endl;}
};
class derive1:public base
{
public:
void who(){cout<<"derive1 class"<<endl;}
};
class derive2:public base
{
public:
void who(){cout<<"derive2 class"<<endl;}
};
int main()
{base obj1,*p;
derive1 obj2;
derive2 obj3;
p=&obj1;
p->who();
p=&obj2;
p->who();
p=&obj3;
p->who();
obj2.who();
obj3.who();
}

5、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;
double area(double a, double b, double c) {
if (a <= 0 || b <= 0 || c <= 0)
throw invalid_argument("the side length should be positive");
if (a + b <= c || b + c <= a || c + a <= b)
throw invalid_argument("the side length should fit the triangle inequality");
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
int main() {
double a, b, c;cin >> a >> b >> c; //输入1 2 4
try {
double s = area(a, b, c);
cout << "Area: " << s << endl;
catch (exception &e) {
cout << "Error: " << e.what() << endl;
}
}
共 30 分 

试卷信息

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