大学C++面向对象试题及参考答案文档格式.docx

上传人:b****5 文档编号:20750665 上传时间:2023-01-25 格式:DOCX 页数:17 大小:20.27KB
下载 相关 举报
大学C++面向对象试题及参考答案文档格式.docx_第1页
第1页 / 共17页
大学C++面向对象试题及参考答案文档格式.docx_第2页
第2页 / 共17页
大学C++面向对象试题及参考答案文档格式.docx_第3页
第3页 / 共17页
大学C++面向对象试题及参考答案文档格式.docx_第4页
第4页 / 共17页
大学C++面向对象试题及参考答案文档格式.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

大学C++面向对象试题及参考答案文档格式.docx

《大学C++面向对象试题及参考答案文档格式.docx》由会员分享,可在线阅读,更多相关《大学C++面向对象试题及参考答案文档格式.docx(17页珍藏版)》请在冰豆网上搜索。

大学C++面向对象试题及参考答案文档格式.docx

私有成员

保护成员

3.在C++中,关于类的析构函数,正确的说法是(A.能带形式参数B

C.可以被重载D

4.

一个类拥有多个构造函数,

A.重复关系

.函数体中必须有delete

.无形参,也不可重载

则这些构造函数之间为(

.拷贝关系

°

°

语句

5.

C.重载关系

C+冲声明常量的关键字是

A.externB.const

试卷装订线

6.

C.publicD

一个函数功能不太复杂,但要求被频繁调用,该函数应该设计成

A.内联函数B.重载函数

C.递归函数

嵌套函数

.volatile

7.

以下基类中的成员函数表示纯虚函数的是(

A.virtualvoidtt()=O

;

B.voidtt(int)=0

C.virtualvoidtt(int)

D.virtualvoidtt(int){}

 

8.

p的基类成员a,其中a是()。

•公有继承的私有成员;

•私有继承的公有成员。

F面正确的说法是()

可以在类外用p.a的形式访问派生类对象

A.公有继承的公有成员;

B

C.公有继承的保护成员;

D

9.对于任意一个类,析构函数的个数最多为(

A.0B.1C.2D.3

10.对于在类中定义的静态数据成员count:

A.count在类外进行初始化

B.该类的每个对象中都有一个独立的静态数据成员count

C.对象创建时产生count

B)10

、改错题(共3小题,每小题

count既可以在类外初始化,也可以在类内初始化

6分,共18分)

1.下面的程序在VC6.0上编译提示errorC2440:

'

typecast'

:

cannotconvertfrom'

classComplex'

to'

float'

Nouser-defined-conversionoperatoravailablethatcanperformthisconversion,请指出

错误原因并改正。

#include<

iostream>

usingnamespacestd;

classComplex{

public:

Complex(floatr=0,floati=0)

{real=r;

imag=i;

}

voidprint()

{cout<

<

'

('

real<

'

imag<

)'

endl;

}private:

floatreal,imag;

};

intmain()

{Complexa(2.2f,4.4f);

a.print();

cout<

float(a)*0.5<

return0;

2.下面的程序在VC6.0上编译提示errorC2662:

getX'

cannotconvert'

this'

pointerfrom'

constclassCTest'

classCTest&

,请指出错误原因并改正。

#include<

classCTest

{private:

intx;

CTest(intx)

{this->

x=x;

intgetX()

{returnx;

{constCTestobj(5);

obj.getX()<

}

3.下面的程序在VC6.0上编译无错,运行结果出错:

“p1:

葺葺葺葺葺葺葺葺”,请指出错误原

因并改正。

cstring>

classSTRING{

STRING(char*s)

{

ptr=newchar[strlen(s)+1];

strcpy(ptr,s);

~STRING()

{deleteptr;

{cout<

ptr<

private:

char*ptr;

{STRINGp1("

book"

);

{STRINGp2("

pen"

p仁p2;

"

p1:

pl.print();

读程序,给出程序的输出结果

(每小题6分,共24分)

【1】#include<

classPerson{public:

Person(){cout<

ConstructorofPerson"

endl;

}~Person(){cout<

DestructorofPerson"

}};

classStudent:

publicPerson{

Student(){cout<

ConstructorofStudent"

endl;

}~Student(){cout<

DestructorofStudent"

classTeacher:

Teacher(){cout<

ConstructorofTeacher"

}~Teacher(){cout<

DestructorofTeacher"

intmain(){

Students;

Teachert;

【2】#include<

classA

{public:

A(inti){x=i;

voiddispa(){cout<

x<

"

"

private:

intx;

classB:

publicA

B(inti):

A(i+10){x=i;

voiddispb(){dispa();

cout<

endl;

}private:

Bb

(2);

b.dispb();

【3】#include<

iostream.h>

classCount{staticintcount;

Count(){cout<

count++;

staticintGetc(){returncount;

}~Count(){count--;

intCount:

:

count=5;

intmain(){Countc1,c2,c3,c4;

Count:

Getc()<

return0;

【4】#include<

classMyClass{

MyClass(inti=0){cout<

1;

MyClass(constMyClass&

x){cout<

2;

MyClass&

operator=(constMyClass&

3;

return*this;

}~MyClass(){cout<

4;

MyClassobj1

(1),obj2

(2),obj3(obj1);

obj1=obj2;

四、编程题(共38分)

1•下面是一个类的测试程序,请设计出能使用如下测试程序的类。

(12分)

{Testa;

a.init(68,55);

其执行结果为:

测试结果:

68-55=13

(12分)

2•请为fraction类(分数类)定义下列重载运算符函数(注意函数原型)⑴加法运算+。

⑵赋值运算=

⑶提取运算>>。

classfraction

{private:

试卷装订线

intfz;

//分子

intfm;

//分母

3•创建一个表示雇员信息的employee类,其中包含数据成员nameempNo和salary,分

别表示雇员的姓名、编号和月薪。

再从employee类派生出3个类worker、technician

和salesman,分别代表普通工人、科研人员、销售人员。

三个类中分别包含数据成员productNum、workHours和monthlysales,分别代表工人每月生产产品的数量、科研人员每月工作的时数和销售人员每月的销售额。

要求各类中都包含成员函数pay,用来计

算雇员的月薪,并假定:

普通工人的月薪=每月生产的产品数x每件产品的赢利x20%

科研人员的月薪=每月的工作时数x每小时工作的酬金

销售人员的月薪=月销售额X销售额提成。

(14分)

重点大学考试试题答案(A卷)

2014~2015学年2学期面向对象程序设计课程

一、单选题(每小题2分,共20分)

CCDCBAAABA

二、改错题洪3小题,每小题6分,共18分)

1.main()函数第3句出错,因为类Complex无类类型转换函数,将Complex对象转换成float。

改正:

在Complex中类增加:

operatorfloat(){returnreal;

2.main()函数第2句出错,因为常对象obj不能调用非常成员函数,

在CTest类中:

将CTest(intx)换成fCTest(intx)const。

或者将main()中constCTestobj(5);

换成CTestobj(5);

3.当程序执行对象p2的析构函数时,对象p1的数据成员ptr出现了所谓的“指针悬挂问题”,这说明

C++中提供给对象的默认的赋值运算符并不是万能的,解决的办法就是重载赋值运算符“=”,使对象不但

能浅拷贝,还能实现深层拷贝。

STRING&

STRING:

operator=(constSTRING&

s)

{if(this==&

s)return*this;

deleteptr;

ptr=newchar[strlen(s.ptr)+1];

strcpy(ptr,s.ptr);

return*this;

三、读程序,给出程序的输出结果。

(每小题6分,共24分)

【1】ConstructorofPerson

ConstructorofStudent

ConstructorofPerson

ConstructorofTeacher

DestructorofTeacher

DestructorofPerson

DestructorofStudent

【2】12,2

【3】56789

【4】1123444

四、编程题(共38分=12分+12分+14分)

1.解答:

#include<

classTest4分

{intx,y;

voidinit(int,int);

voidprint();

voidTest:

init(inti,intj)4分

{x=i;

y=j;

print()4分

x<

-"

y<

="

x-y<

2.解答:

①fractionoperator+(fraction&

f1,fraction&

f2)

{4分

intnfz=f1.fz*f2.fm+f1.fm*f2.fz;

intnfm=f1.fm*f2.fm;

returnfraction(nfz,nfm);

2fraction&

fraction:

operator=(fraction&

f)

fz=f.fz;

fm=f.fm;

3istream&

operator>

>

(istream&

is,fraction&

is>

f.fz>

f.fm;

returnis;

3.解答:

classemployee

protected:

charname[20];

intempNo;

floatsalary;

employee(char*cp="

李强"

intno=1001);

employee(employee&

voidsetname(char*cp);

voidsetempNo(intno);

voidsetsalary(floatsa);

char*getname();

intgetempNo();

floatgetsalary();

voiddisplay();

employeecompsalary(employee*emp);

employee:

employee(char*cp,intno)

{inti=0;

while(*cp)

{name[i]=*cp;

i++;

cp++;

name[i]='

\0'

empNo=no;

employee(employee&

em)

while(em.name[i])

{name[i]=em.name[i];

\O:

empNo=em.empNo;

salary=em.salary;

voidemployee:

setname(char*cp)

setempNo(intno)

{empNo=no;

setsalary(floatsa)

{salary=sa;

char*employee:

getname()

{returnname;

intemployee:

getempNo()

{returnempNo;

floatemployee:

getsalary()

{returnsalary;

display()

工号为"

empNo<

的雇员"

name<

的月薪为"

salary<

employeeemployee:

compsalary(employee*emp)

if(this->

salary>

=emp->

salary)

else

return*emp;

classworker:

publicemployee

worker(char*,int,int);

voidsetproductNum(intn)

{productNum=n;

intgetproductNum()

{returnproductNum;

voidpay();

intproductNum;

staticfloatproper;

〃每件产品的利润

floatworker:

proper=20;

//假设每件产品的利润为20元

worker:

worker(char*name,intno,intpronum)

employee(name,no)

productNum=pronum;

voidworker:

pay()

salary=productNum*proper*0.2;

classtechnician:

technician(char*,int,float);

voidsethours(floath)

{workHours=h;

floatgethours()

{returnworkHours;

floatworkHours;

staticfloatpayperhour;

〃科研人员每小时工作的酬金

40元

floattechnician:

payperhour=40;

//假设科研人员每小时工作的酬金为

technician:

technician(char*name,intnofloathours)

{workHours=hours;

voidtechnician:

{salary=workHours*payperhour;

classsalesman:

publicemployee

salesman(char*,int,float);

voidsetmonthlysales(intnum)

{monthlysales=num;

intgetmonthlysales()

{returnmonthlysales;

floatmonthlysales;

staticfloatpercent;

//销售人员的销售提成

floatsalesman:

percent=0.5;

II假设销售人员的销售提成为50%

salesman:

salesman(char*name,intn,floatsn)

employee(name,n)

monthlysales=sn;

voidsalesman:

salary=monthlysales*percent;

voidmain()

workerw1("

张三"

1001,1000);

techniciant1("

李四"

1002,200);

salesmans1("

王五"

1003,10000);

w1.pay();

t1.pay();

s1.pay();

工人"

w1.getname()<

w1.getsalary()<

元\n"

科研人员"

t1.getiname()<

t1.getsalary()<

销售人员"

s1.getname()<

s1.getsalary()<

元\n"

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

当前位置:首页 > 农林牧渔 > 林学

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

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