C++面向对象程序设计模拟试题6.docx

上传人:b****9 文档编号:25822147 上传时间:2023-06-15 格式:DOCX 页数:17 大小:19.54KB
下载 相关 举报
C++面向对象程序设计模拟试题6.docx_第1页
第1页 / 共17页
C++面向对象程序设计模拟试题6.docx_第2页
第2页 / 共17页
C++面向对象程序设计模拟试题6.docx_第3页
第3页 / 共17页
C++面向对象程序设计模拟试题6.docx_第4页
第4页 / 共17页
C++面向对象程序设计模拟试题6.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

C++面向对象程序设计模拟试题6.docx

《C++面向对象程序设计模拟试题6.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计模拟试题6.docx(17页珍藏版)》请在冰豆网上搜索。

C++面向对象程序设计模拟试题6.docx

C++面向对象程序设计模拟试题6

C++面向对象程序设计模拟试题六

一、单项选择题(本大题共10小题,每小题2分,共20分)在每小题列出的四个备选项中,只有一个是苻合题目要求的。

请将其代码填写在题后的括号内。

错选,多选或未选均无分。

1.类的析构函数(d)。

A)作为类的一般成员函数B)类初始化时被调用

C)对象初始化时被调用D)对象被删除时被调用

*2.一个类的友元函数或友元类可以访问该类的(d)。

A)私有成员B)保护成员C)公有成员D)所有成员

*3.下列关于成员函数特征的描述中,(a)是错误的。

A)成员函数一定是内联函数B)成员函数可以重载

C)成员函数可以设置参数的默认值D)成员函数可以是静态的

*4.下列函数中,(c)不是类的成员函数。

A)构造函数B)析构函数C)友元函数D)拷贝构造函数

*5.下列对派生类的描述中,(b)是错误的。

A)一个派生类可以作为另一个派生类的基类

B)派生类至少有一个基类

C)派生类的成员除了它自己的成员外,还包含了它的基类的成员

D)派生类中继承的基类成员的访问权限到派生类中保持不变

6.下列的描述中,(d)是错误的。

A)公有继承时基类中的public成员在派生类中仍是public的

B)公有继承时基类中的private成员在派生类中仍是private的

C)公有继承时基类中的protected成员在派生类中仍是protected的

D)私有继承时基类中的public成员在派生类中仍是private的

7.下列虚基类的声明中正确的是(a)。

A)classvirtualB:

publicAB)virtualclassB:

publicA

C)classB:

publicAvirtualD)classB:

virtualpublicA

8.若类A和类B的定义如下:

classA

{

inti,j;

public:

A(intm,intn):

i(m),j(n){}

intGeti(){returni;}

};

classB:

publicA

{

intk;

public:

B(intm,intn,intu):

A(m,n),k(u){}

voidMake(){k=i*j;}

};

intmain()

{

Bb(1,2,3);

return0;

}

则上述定义中,(a)是非法的表达式.

A)k=i*j;B)intk;C)returni;D)voidMake()

9.若有以下程序:

#include

usingnamespacestd;

classA

{

public:

inta;

A(){a=10;}

};

classA1:

publicA

{

public:

A1(){a=a+1;}\

};

classA2:

publicA

{

public:

A2(){a=a+2;}

};

classB:

publicA1,publicA2

{

public:

B(){}

voidPrint(){cout<

};

intmain()

{

Bobj;

obj.Print();

return0;

}

则程序编译或运行后的输出结果为:

a

A)提示语法错误B)13C)12D)10

10.在下面的4个关键字中,(a )是用来说明虚函数的。

A)virtualB)publicC)protectedD)private

二、填空题(本大题共5小题,每小题2分,共10分)将正确的答案写在每小题的空格内,错填或不填均无分。

1.通过类创建(对象)来要调用构造函数。

2.假定AB为一个类,则执行“ABa[10];”语句时,系统自动调用该类的构造函数的次数为(10)。

3.使用函数模板的方法是先说明函数模板,然后实例化成相应的(模板函数)进行调用执行。

4.拷贝构造函数用它所在类的(引用)作为参数。

5.重载运算符“<<”的函数名为(operator<<)。

三、程序分析题(本大题共6小题,每小题5分,共30分)给出下面各程序的输出结果。

1.若有以下程序:

#include

usingnamespacestd;

classA

{

inta;

public:

A(intaa=0)

{

a=aa;

cout<<"A():

"<

}

};

classB:

publicA

{

intb;

public:

B(intaa=0,intbb=0):

A(aa)

{

b=bb;

cout<<"B():

"<

}

};

intmain()

{

Bx(5),y(6,7);

return0;

}

输出结果为:

2.若有以下程序:

#include

usingnamespacestd;

classPoint

{

private:

intx,y;

public:

Point(intm=0,intn=0){x=m;y=n;}

voidShow()const{cout<<"x="<

};

intmain()

{

Point*p=newPoint(1,68);

p->Show();

deletep;

return0;

}

输出结果为:

3.若有以下程序:

#include

usingnamespacestd;

classSample

{

private:

intn;

public:

Sample(inti){n=i;s+=n;}

staticints;

voidShow()const{cout<

};

intSample:

:

s=0;

intmain()

{

Samplea

(2),b(5),c(8);

c.Show();

return0;

}

输出结果为:

4.若有以下程序:

#include

usingnamespacestd;

classSample

{

intn;

public:

Sample(inti){n=i;}

voidPrint(){cout<<"1:

n="<

voidPrint()const{cout<<"2:

n="<

};

intmain()

{

Samplea(10);

constSampleb(20);

a.Print();

b.Print();

return0;

}

输出结果为:

5.若有以下程序:

#include

usingnamespacestd;

classTest

{

intx;

public:

voidSetx(inti){x=i;}

intPutx(){returnx;}

};

intmain()

{

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];

cout<Putx()<<",";

}

cout<

return0;

}

输出结果为:

6.若有以下程序:

#include

usingnamespacestd;

classBase

{

public:

Base(intx){a=x;}

voidShow(){cout<

private:

inta;

};

classDerived:

publicBase

{

public:

Derived(inti):

Base(i+1),b(i){}

voidShow(){cout<

private:

intb;

};

intmain()

{

Baseb(5),*pb;

Derivedd

(1);

pb=&d;

pb->Show();

return0;

}

输出结果为:

四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。

1.将如下程序补充完整。

#include

usingnamespacestd;

classA

{

private:

staticintn;

public:

A(){n++;}

staticvoidShow(){cout<<"共有"<

"<

};

[1]n=0;//为静态数据成员赋初值

intmain()

{

Aobj1,obj2,obj3;

A:

:

Show();

return0;

}

2.下列程序的输出结果为“2”,试将程序补充完整。

#include

usingnamespacestd;

classA

{

public:

[2]Show()const{cout<<"1"<

};

classB:

publicA

{

public:

voidShow()const{cout<<"2"<

};

intmain()

{

A*p=newB;

p->Show();

deletep;

return0;

}

3.将如下程序补充完整。

#include

usingnamespacestd;

classBase

{

private:

intm;

public:

Base(inta):

m(a){}

virtualvoidShow()const{cout<

};

classDerived:

publicBase

{

private:

intn;

public:

Derived(inta,intb):

Base(a),n(a){}

voidShow()const

{

cout<

[3]Show();//调用基类的Show()

}

};

intmain()

{

Derivedobj(158,98);

Base*p=&obj;

p->Show();

return0;

}

4.将如下程序补充完整。

#include

usingnamespacestd;

classInt

{

private:

inti;

public:

Int(intx=0):

i(x){}

(){returni;}//类类型转换函数,将类Int转换为基本类型int

};

intmain()

{

Inta;

cout<

return0;

}

五、编程题(本大题共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++面向对象程序设计模拟试题六参考答案

一、单项选择题(本大题共10小题,每小题2分,共20分)在每小题列出的四个备选项中,只有一个是苻合题目要求的。

请将其代码填写在题后的括号内。

错选,多选或未选均无分。

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<<

三、程序分析题(本大题共6小题,每小题5分,共30分)给出下面各程序的输出结果。

1.参考答案:

A():

5

B():

0

A():

6

B():

7

2.参考答案:

x=1,y=68

3.参考答案:

15

4.参考答案:

1:

n=10,2:

n=20

5.参考答案:

5,6,7

6.参考答案:

2

四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。

1.参考答案:

[1]intA:

:

2.参考答案:

[2]virtualvoid

3.参考答案:

[3]Base:

:

4.参考答案:

[4]operatorint

五、编程题(本大题共2小题,第1小题12分,第2小题16分,共28分)

1.参考程序:

#include

usingnamespacestd;

classRect

{

private:

doublelength,width;

public:

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);}

};

intmain()

{

try

{

Rectobj(1,8);

cout<<"周长:

"<

}

catch(char*str)

{

cout<<"异常信息:

"<

}

return0;

}

2.参考程序:

#include

usingnamespacestd;

classVector2d

{

doublex,y;

public:

Vector2d(doublea,doubleb):

x(a),y(b){}

voidShow(){cout<<"("<

Vector2doperator+(Vector2d&obj);

};

Vector2dVector2d:

:

operator+(Vector2d&obj)

{returnVector2d(x+obj.x,y+obj.y);}

intmain()

{

Vector2dd1(3.5,4.5),d2(2.5,5.5),d3(0.0,0.0);

d3=d1+d2;

d3.Show();

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1