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

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


第1题

面向对象程序设计中的数据隐藏指的是( )。

共 3 分 

第2题

一个内联函数Fun,使用int类型的参数,求其平方并返回,返回值也为int类型,下列定义正确的是( )。

共 3 分 

第3题

下面关于重载函数的叙述中正确的是( )。

共 3 分 

第4题

下列关于纯虚函数的描述中,错误的是( )。

共 3 分 

第5题

下列关于析构函数的描述中正确的是( )。

共 3 分 

第6题

下列关于纯虚函数的描述中,正确的是( )。

共 3 分 

第7题

复制初始化构造函数的作用是( )。

共 3 分 

第8题

要将类A说明是类B的虚基类,正确的描述是( )。

共 3 分 

第9题

所谓多态性是指( )。

共 3 分 

第10题

标准模板库(STL)所涉及的4个最主要的基本组件是( )。

共 3 分 

第11题

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

2,1

4,3

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 A
{
int a;
public:
A(int i=0){a=i;}
Int Geta(){return a;}
};
class B
{
A a;
int b;
public:
B(int i=0,int j=0):___________(1)____________{}
void display(){cout<<a.Geta()<<’,’<<b<<endl;}
};
void main()
{
B b[2]={B(1,2),B(3,4)};
for(int i=0;i<2;i++)
_____________(2)______________;
}
共 8 分 

第12题

下面程序中A是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

B1 called

B2 called

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
#include<iostream.h>
class A
{
public:
______________(1)____________;
};
class B1:public A
{
public:
void display(){cout<”B1 called”<<endl;
};
class B2:public A
{
public:
void display(){cout<<”B2 called”<<endl;
};
void show(_________(2)________)
{
p->display();
}
void main()
{
B1 b1;
B2 b2;
A* p[2]={&b1,&b2};
for(int i=0;i<2;i++)
show(p[i]);
}
共 8 分 

第13题

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

Name:王小明

Grade:90

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>
#include<string.h>
class Person
{
char name[20]
public:
Person(char* s){strcpy(name,s);}
void display(){cout<<”Name:”<<name<<endl;}
};
class Student:public Person
{
int grade;
public:
Student(char* s,int g):________(1)_____{grade=g;}
void display(){
___________(2)___________;
cout<<”Grade:”<<grade<<endl;
}
};
void main()
{
Student s(“王小明”,90);
s.display();
}
共 8 分 

第14题

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream.h>
class Integer
{
int x;
public:
Integer(int a=0){x=a;}
void display(){cout<<x<<endl;}
_____________(1)_____________;
};
Integer Max(Integer a,Integer b)
{
if(__________(2)_________)
return a;
return b;
}
void main()
{
Integer a(3),b(5),c;
c=Max(a,b);
c.display();
}
共 8 分 

第15题

请在下面的横线处填上适当内容,以使类的定义完整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Array
{
Int* ptr;
Int size;
public:
Array(){size=0; ptr=NULL;}
Array(int n){size=n;ptr=new int[size];}
Array(______(1)_______)//复制初始化构造函数
{
size=a.size;
ptr=new int[size];
for(int i=0;i<size;i++)
________(2)________;//将源对象的动态数组内容复制到目标对象
}
};
共 8 分 

第16题

程序阅读题

1、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream.h>
class Test
{
private:
int num;
public:
Test(int n=0){num=n;num++}
~Test(){cout<<″Destructor is active,number=″<<num<<endl;}
};
void main()
{
Test x[2];
cout<<″Exiting main″<<endl;
}

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 A
{
public:
virtual void fun (int data){cout<<”class A:”<<data<<endl;}
void fun(char *str){ cout<<”class A:”<<str<<endl; }
};
class B: public A
{
public:
void fun() {cout<<”class B”<<endl;}
void fun(int data) { cout<<”class B:”<<data<<endl;}
void fun(char *str){ cout<<”class B:”<<str<<endl;}
};
void main()
{
A *pA;
B b;
pA=&b;
pA->fun(1);
pA->fun(“Hello”);
}

3、

1
2
3
4
5
6
7
8
9
10
11
#include <iostream.h>
void main()
{
cout.fill('*');
cout.width(10);
cout<<"123.45"<<endl;
cout.width(8);
cout<<"123.45"<<endl;
cout.width(4);
cout<<"123.45"<<endl;
}

4、从键盘输入10,给出程序的输出结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
main()
{
vector<int> v;
int i,j,n;
cin>>n;
for(i=1;i<=n;i++)
{
if(i%2!=0)
v.push_back(x);
}
vector<int>::iterator p;
for(p=v.begin();p!=v.end();p++)
cout<<*p<<endl;
}

5、

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>
using namespace std;
class Sample
{
private:
int i;
static int count;
public:
Sample();
void display();
};
Sample::Sample()
{
i=0;
count++;
}
void Sample::display()
{
cout<<"i="<<i++<<",count="<<count<<endl;
}
int Sample::count=0;
void main()
{
Sample a,b;
a.display();
b.display();
}
共 30 分 

第17题

按下列要求编程,实现类的定义,并在主函数中测试这个类。

定义一个描述日期的类Date,包括的数据成员有年(year)、月(month)和日(day),并实现如下功能函数;

(1)日期对象初始化;

(2)设置日期;

(3)以year/month/day形式输出日期;

(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
#include<iostream.h>
class Date
{
public:
Date(int y=1900,int m=1,int d=1)
{
year=y;
month=m;
date=d;
}
void setDate(int y,int m,int d)
{
year=y;
month=m;
date=d;
}
void outputDate()
{
cout<<year<<"/"<<month<<"/"<<day<<endl;
}
bool isLeapYear()
{
return year%4==0&&year%100!=0||year%400==0;
}
private:
int year,month,day;
};
void main()
{
Date d;
d.outputDate();
d.setDate(2015,3,5);
d.outputDate();
if(d.isLeapYear())
cout<<"a leap year."<<endl;
else
cout<<"not a leap year."<<endl;
}
共 0 分 

第18题

根据下列Vector类定义,编程完成Vector类的具体实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Vector
{
friend ostream &operator<<(ostream &out, const Vector &v);
private:
int *data;
int size;
public:
Vector();
Vector(int a[],int n);
Vector(const Vector &s);
~Vector();
Vector &operator=(const Vector &v);
int &operator[](int index);
};

参考答案:

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
#include<iostream>
using namespace std;
class Vector
{
friend ostream &operator<<(ostream &out, const Vector &v);
private:
int *data;
int size;
public:
Vector();
Vector(int a[],int n);
Vector(const Vector &s);
~Vector();
Vector &operator=(const Vector &v);
int &operator[](int index);
};
Vector::Vector()
{
data=NULL;
size=0;
}
Vector::Vector(int a[],int n)
{
size = n;
data = new int[size];
for(int i=0;i<size;i++)
data[i]=a[i];
}
Vector::Vector(const Vector &s)
{
size = s.size;
data = new int[size];
for(int i=0;i<size;i++)
data[i]=s.data[i];
}
Vector::~Vector()
{
if(data!=NULL)
delete[]data;
}
Vector Vector::&operator=(const Vector &v)
{
if(this == &v)
return *this;
if(size != v.size)
{
delete[] data;
size = v.size;
data = new int[size];
}
for(int i=0;i<=size;i++)
data[i] = v.data[i];
return *this;
}
int Vector::&operator[](int index)
{
return data[index];
}
ostream &operator<<(ostream &out, const Vector &v)
{
for(int i=0;i<v.size;i++)
out<<v.data[i]<<" ";
return out;
}
共 0 分 

试卷信息

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