川大c++期末复习题Word文档格式.docx
《川大c++期末复习题Word文档格式.docx》由会员分享,可在线阅读,更多相关《川大c++期末复习题Word文档格式.docx(14页珍藏版)》请在冰豆网上搜索。
publicAvirtualD)classB:
virtualpublicA
8.若类A和类B的定义如下:
classA
{
inti,j;
public:
A(intm,intn):
i(m),j(n){}
intGeti(){returni;
}
};
classB:
{
intk;
B(intm,intn,intu):
A(m,n),k(u){}
voidMake(){k=i*j;
}
intmain()
Bb(1,2,3);
return0;
}
则上述定义中,()是非法的表达式.
A)k=i*j;
B)intk;
C)returni;
D)voidMake()
9.若有以下程序:
#include<
iostream>
usingnamespacestd;
inta;
A(){a=10;
classA1:
A1(){a=a+1;
}\
classA2:
publicA
A2(){a=a+2;
publicA1,publicA2
B(){}
voidPrint(){cout<
<
a<
endl;
Bobj;
obj.Print();
则程序编译或运行后的输出结果为:
A)提示语法错误B)13C)12D)10
10.在下面的4个关键字中,( )是用来说明虚函数的。
A)virtualB)publicC)protectedD)private
二、填空题(本大题共5小题,每小题2分,共10分)将正确的答案写在每小题的空格内,错填或不填均无分。
1.通过类创建()来要调用构造函数。
2.假定AB为一个类,则执行“ABa[10];
”语句时,系统自动调用该类的构造函数的次数为()。
3.使用函数模板的方法是先说明函数模板,然后实例化成相应的()进行调用执行。
4.拷贝构造函数用它所在类的()作为参数。
5.重载运算符“<
”的函数名为()。
三、程序分析题(本大题共6小题,每小题5分,共30分)给出下面各程序的输出结果。
1.若有以下程序:
classA
A(intaa=0)
{
a=aa;
cout<
"
A():
<
intb;
B(intaa=0,intbb=0):
A(aa)
b=bb;
B():
b<
Bx(5),y(6,7);
输出结果为:
2.若有以下程序:
classPoint
private:
intx,y;
Point(intm=0,intn=0){x=m;
y=n;
}
voidShow()const{cout<
x="
x<
"
y="
y<
Point*p=newPoint(1,68);
p->
Show();
deletep;
3.若有以下程序:
classSample
intn;
Sample(inti){n=i;
s+=n;
staticints;
s<
intSample:
:
s=0;
Samplea
(2),b(5),c(8);
c.Show();
4.若有以下程序:
1:
n="
n<
"
;
voidPrint()const{cout<
2:
Samplea(10);
constSampleb(20);
a.Print();
b.Print();
5.若有以下程序:
classTest
intx;
voidSetx(inti){x=i;
intPutx(){returnx;
Test*p;
Testa[3];
a[0].Setx(5);
a[1].Setx(6);
a[2].Setx(7);
for(intj=0;
j<
3;
j++)
p=&
a[j];
Putx()<
cout<
6.若有以下程序:
classBase
Base(intx){a=x;
voidShow(){cout<
classDerived:
publicBase
Derived(inti):
Base(i+1),b(i){}
Baseb(5),*pb;
Derivedd
(1);
pb=&
d;
pb->
四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。
1.将如下程序补充完整。
staticintn;
A(){n++;
}
staticvoidShow(){cout<
共有"
个对象!
"
[1]n=0;
//为静态数据成员赋初值
intmain()
Aobj1,obj2,obj3;
A:
2.下列程序的输出结果为“2”,试将程序补充完整。
[2]Show()const{cout<
1"
2"
A*p=newB;
3.将如下程序补充完整。
intm;
Base(inta):
m(a){}
virtualvoidShow()const{cout<
m<
Derived(inta,intb):
Base(a),n(a){}
voidShow()const
[3]Show();
//调用基类的Show()
Derivedobj(158,98);
Base*p=&
obj;
4.将如下程序补充完整。
classInt
inti;
Int(intx=0):
i(x){}
(){returni;
}//类类型转换函数,将类Int转换为基本类型int
Inta;
五、编程题(本大题共2小题,第1小题12分,第2小题16分,共28分)
1.设计一个类Rect,要求如下:
(1)该类中的私有数据成员length,width存放它的长和宽,并且设置它们的默认值是0。
(2)通过成员函数设置其长和宽,并确保长和宽都在(0,50)范围之内。
(3)实现求周长函数GetPerimeter()。
2.定义一个二维座标类Vector2d,二个数据成员为double型x,y为private属性。
定义代二个参数的构造函数和一个Show()函数用以输出x,y的值,另外作为成员函数重载的运算苻”+”的功能是将此类二个对象的数据成员x和y对应相加。
这些成员函数的属性均为public.请用C++编写此程序,并编写测试程序进行测试
C++面向对象程序设计模拟试题六参考答案
1.D)2.D)3.A)4.C)5.D)
6.B)7.D)8.A)9.A)10.A).
二、填空题(本大题共5小题,每小题2分,共10分)不写解题过程,将正确的答案写在每小题的空格内,错填或不填均无分
1.参考答案:
对象
2.参考答案:
10
3.参考答案:
模板函数
4.参考答案:
引用
5.参考答案:
operator<
5
0
6
7
x=1,y=68
15
n=10,2:
n=20
5,6,7
6.参考答案:
2
[1]intA:
[2]virtualvoid
[3]Base:
[4]operatorint
1.参考程序:
classRect
doublelength,width;
Rect(doublel=0,doublew=0):
length(l),width(w){}
voidSet(doublel,doublew)
if(length<
=0||length>
=50||width<
=0||width>
=50)
throw"
数据不在指定范围(0,50)!
length=l;
width=w;
doubleGetPerimeter(){return2*(length+width);
try
{
Rectobj(1,8);
周长:
obj.GetPerimeter()<
catch(char*str)
异常信息:
str<
2.参考程序:
iostream>
classVector2d
doublex,y;
Vector2d(doublea,doubleb):
x(a),y(b){}
("
)"
Vector2doperator+(Vector2d&
obj);
Vector2dVector2d:
operator+(Vector2d&
obj)
{returnVector2d(x+obj.x,y+obj.y);
Vector2dd1(3.5,4.5),d2(2.5,5.5),d3(0.0,0.0);
d3=d1+d2;
d3.Show();