C++面向对象程序设计模拟试题3Word格式文档下载.docx
《C++面向对象程序设计模拟试题3Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计模拟试题3Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。
结束B)含有纯虚函数的类可以定义对象
C)含有纯虚函数的类是抽象类D)上面都是错误的
9.(C)解决二义性问题。
A)只能使用作用域分辨操作符B)只能使用虚基类
C)使用作用域分辨操作符或虚基类D)上面都不能
10.实现编译时的多态性要使用(A)。
A)重载函数B)析构函数C)构造函数D)虚函数
二、填空题(本大题共5小题,每小题2分,共10分)不写解答过程,将正确的答案写在每小题的空格内。
错填或不填均无分。
1.定义一个函数名为fun,返回值为int,没有参数的纯虚函数的声明是()。
2.定义静态成员函数应使用关键字()。
3.在C++中,如果类的名称是CTest,则此类的构造函数名称为()。
4.在类的对象被创建时,()函数会被自动调用。
5.用class声明的类中数据成员和成员函数的默认访问级别为(private)。
三、程序分析题(本大题共6小题,每小题5分,共30分)给出下面各程序的输出结果。
1.阅读下面程序,写出输出结果。
#include<
iostream>
usingnamespacestd;
classA
{
public:
A(){cout<
<
"
A()"
<
endl;
}
~A(){cout<
~A()"
}
};
intmain()
Aa,*p=newA;
deletep;
cout<
end"
return0;
}
上面程序的输出结果为:
//自己再做一次
2.阅读下面程序,写出输出结果。
classArray
private:
int*elem;
intsize;
Array(inta[],intsz):
elem(a),size(sz){}
intGetSize()const{returnsize;
}
int&
operator[](intpos){returnelem[pos-1];
inta[]={1,2,3,4,5};
Arrayar(a,5);
ar[1]=8;
for(inti=1;
i<
=ar.GetSize();
i++)
cout<
ar[i]<
;
82345
3.阅读下面程序,写出输出结果。
virtualvoidShow()const{cout<
A"
classB:
publicA
voidShow()const{cout<
B"
voidFun1(Aobj){obj.Show();
}
voidFun2(constA&
obj){obj.Show();
}
intmain()
Bb;
Fun1(b);
Fun2(b);
A*p=newB;
p->
A:
:
Show();
卧槽我已经乱了
A
B
B//尼玛为啥
4.阅读下面程序,写出输出结果。
template<
classElemType>
ElemTypef(ElemTypex)
{
returnx*(ElemType)5.1;
voidf(ElemTypex,ElemTypey)
x=x*1.1;
y=y*1.1;
x<
y<
f(3)<
f(1,2);
f(1.0,2.0);
5.阅读下面程序,写出输出结果。
inta;
A(intm=1):
a(m){}
voidShow(){cout<
a<
a+5<
intmain()
Aa,b(8);
a.Show();
b.Show();
constAc,d(8);
c.Show();
d.Show();
1
8
6
13//嗯(⊙_⊙)为毛
6.阅读下面程序,写出输出结果。
classTest
staticintn;
Test(){n+=6;
~Test(){n-=8;
staticintGetNum(){returnn;
intTest:
n=2;
intmain()
Test:
GetNum()<
Test*p=newTest;
四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。
1.请完成下面的程序
staticintcount;
//对象数//耶猜对了
Test(){count++;
staticvoidShow(){cout<
共有"
count<
个对象!
"
}数
count=0;
Testobj1,obj2,obj3;
2.请完成下面的程序
classInteger
Integer(intx=0){a=x;
operatorint(){returna;
Integeroperator+(constInteger&
i)const
{
returna+i.a;
//返回和
Integera
(2),b(3),c;
c=a+b;
c<
3.请完成下面的程序
classA
ElemTypea;
A(ElemTypex):
a(x){}
intmain()
A<
char*>
obj("
Hello,world!
);
obj.Show();
4.下列程序的输出结果为“B”,试将程序补充完整。
virtualvoidShow()const{cout<
intmain()
//回去试试
五、编程题(本大题共2小题,第1小题12分,第2小题16分,共28分)
1.试使用函数模板实现求一个数组各元素的平均值,要求编写测试程序。
函数模板声明如下:
classT>
TAverage(Ta[],intn)//求数组a各元素之平均值
2.定义Person(人)类,由Person分别派生出Teacher(教师)类和Cadre(干部)类,再由Teacher(教师)类和Cadre(干部)类采用多重继承方式派生出新类TeacherCadre(教师兼干部)类,各类之间的继承关系如下图所示。
要求:
(1)在Person类中包含的数据成员有姓名(name)、年龄(age)、性别(sex)。
在Teacher类还包含数据成员职称(title),在Cadre类中还包含数据成员职务(post),在TeacherCadre类中还包含数据成员工资(wages)。
(2)在类体中定义成员函数。
(3)每个类都有构造函数与显示信息函数(Show)。
C++面向对象程序设计模拟试题三参考答安全
1.C)2.D)3.A)4.B)5.D)
6.A)7.C)8.C)9.C)10.A)
1.参考答案:
intfun()=0;
2.参考答案:
static
3.参考答案:
CTest
4.参考答案:
构造函数
5.参考答案:
private或私有
A()
~A()
end
15
2
1.1
2.2
13
6.参考答案:
[1]static
[2]a+i.a或this->
a+i.a
[3]char*
[4]virtual
1.参考程序:
TAverage(Ta[],intn)
Ts=0;
for(inti=0;
n;
s=s+a[i];
returns/n;
doublea[]={11.8,2,3,4,5.5,6.8,7,8,9};
Average(a,9)<
2.参考程序:
classPerson
protected:
charname[18];
intage;
charsex[3];
Person(charnm[],intag,charsx[])
strcpy(name,nm);
age=ag;
strcpy(sex,sx);
}
voidShow()const
{
姓名:
name<
年龄:
age<
性别:
sex<
classTeacher:
virtualpublicPerson
chartitle[18];
Teacher(charnm[],intag,charsx[],chartl[]):
Person(nm,ag,sx)
{strcpy(title,tl);
voidShow()const
Person:
职称:
title<
classCadre:
charpost[18];
Cadre(charnm[],intag,charsx[],charpt[]):
{strcpy(post,pt);
职务:
post<
classTeacherCadre:
publicTeacher,publicCadre
doublewages;
TeacherCadre(charnm[],intag,charsx[],chartl[],charpt[],doublewg)
:
Person(nm,ag,sx),Teacher(nm,ag,sx,tl),Cadre(nm,ag,sx,pt)
{wages=wg;
}
工资:
wages<
元"
TeacherobjTeacher("
文冠杰"
48,"
男"
"
教授"
CadreobjCadre("
周杰"
56,"
院长"
TeacherCadreobjTeacherCadre("
李靖"
50,"
女"
6890);
objTeacher.Show();
objCadre.Show();
objTeacherCadre.Show();