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

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

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

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

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

大学C++面向对象试题及参考答案

…………试卷装订线………………装订线内不要答题,不要填写考生信息………………试卷装订线…………

姓名

学号

专业班级

学院

重点大学考试试卷(A卷)

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

时间120分钟,学时,学分,总分100分,占总评成绩100%年月日

题号

合计

满分

20

18

24

38

100

得分

得分

 

一、单选题(每小题2分,共20分,本题答案填入下表中)

小题号

1

2

3

4

5

6

7

8

9

10

答案

 

1.在C++中,下列语句,错误的是()。

A.inta(3);B.inta[3];

C.int&a;D.int*a;

2.派生类中的成员函数可以直接访问基类的()。

A.公有成员B.私有成员

C.公有和保护成员D.保护成员

3.在C++中,关于类的析构函数,正确的说法是()。

A.能带形式参数B.函数体中必须有delete语句

C.可以被重载D.无形参,也不可重载

4.一个类拥有多个构造函数,则这些构造函数之间为()。

A.重复关系B.拷贝关系

C.重载关系D.继承关系

5.C++中声明常量的关键字是()。

A.externB.constC.publicD.volatile

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

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

C.递归函数D.嵌套函数

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

A.virtualvoidtt()=0;B.voidtt(int)=0;

C.virtualvoidtt(int);D.virtualvoidtt(int){}

8.可以在类外用p.a的形式访问派生类对象p的基类成员a,其中a是()。

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

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

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

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

10.对于在类中定义的静态数据成员count,下面正确的说法是()  

A.count在类外进行初始化  

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

C.对象创建时产生count  

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

得分B)10,10C)20,10D)20,20得分

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

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

'typecast':

cannotconvertfrom'classComplex'to'float',Nouser-defined-conversionoperatoravailablethatcanperformthisconversion,请指出错误原因并改正。

#include

usingnamespacestd;

classComplex{

public:

Complex(floatr=0,floati=0)

{real=r;imag=i;}

voidprint()

{cout<<'('<

private:

floatreal,imag;

};

intmain()

{Complexa(2.2f,4.4f);

a.print();

cout<

return0;

}

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

'getX':

cannotconvert'this'pointer

from'constclassCTest'to'classCTest&',请指出错误原因并改正。

#include

usingnamespacestd;

classCTest

{private:

intx;

public:

CTest(intx)

{this->x=x;}

intgetX()

{returnx;}

};

intmain()

{constCTestobj(5);

cout<

return0;

}

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

“p1:

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

#include

#include

usingnamespacestd;

classSTRING{

public:

STRING(char*s)

{

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

strcpy(ptr,s);

}

~STRING()

{deleteptr;}

voidprint()

{cout<

private:

char*ptr;

};

 

intmain()

{STRINGp1("book");

{STRINGp2("pen");

p1=p2;

}

cout<<"p1:

";

p1.print();

return0;

}

 

得分

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

(每小题6分,共24分)

【1】#include

usingnamespacestd;

classPerson{

public:

Person(){cout<<"ConstructorofPerson"<

~Person(){cout<<"DestructorofPerson"<

};

classStudent:

publicPerson{

public:

Student(){cout<<"ConstructorofStudent"<

~Student(){cout<<"DestructorofStudent"<

};

classTeacher:

publicPerson{

public:

Teacher(){cout<<"ConstructorofTeacher"<

~Teacher(){cout<<"DestructorofTeacher"<

};

intmain(){

Students;

Teachert;

return0;

}

…………试卷装订线………………装订线内不要答题,不要填写考生信息………………试卷装订线…………

…………试卷装订线………………装订线内不要答题,不要填写考生信息………………试卷装订线…………

【2】#include

usingnamespacestd;

classA

{public:

A(inti){x=i;}

voiddispa(){cout<

private:

intx;

};

classB:

publicA

{public:

B(inti):

A(i+10){x=i;}

voiddispb(){dispa();cout<

private:

intx;

};

intmain(){

Bb

(2);

b.dispb();

return0;

}

【3】#include

classCount{

staticintcount;

public:

Count(){cout<

staticintGetc(){returncount;}

~Count(){count--;}

};

intCount:

:

count=5;

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

cout<

:

Getc()<

return0;

}

【4】#include

usingnamespacestd;

classMyClass{

public:

MyClass(inti=0){cout<<1;}

MyClass(constMyClass&x){cout<<2;}

MyClass&operator=(constMyClass&x){cout<<3;return*this;}

~MyClass(){cout<<4;}

};

intmain(){

MyClassobj1

(1),obj2

(2),obj3(obj1);

obj1=obj2;

return0;

}

得分

四、编程题(共38分)

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

(12分)

intmain()

{Testa;

a.init(68,55);

a.print();

return0;

}

其执行结果为:

测试结果:

68-55=13

…………试卷装订线………………装订线内不要答题,不要填写考生信息………………试卷装订线…………

2.请为fraction类(分数类)定义下列重载运算符函数(注意函数原型)(12分)

⑴加法运算+。

⑵赋值运算=。

⑶提取运算>>。

classfraction

{private:

intfz;//分子

intfm;//分母

public:

};

 

3.创建一个表示雇员信息的employee类,其中包含数据成员name、empNo和salary,分别表示雇员的姓名、编号和月薪。

再从employee类派生出3个类worker、technician和salesman,分别代表普通工人、科研人员、销售人员。

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

要求各类中都包含成员函数pay,用来计算雇员的月薪,并假定:

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

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

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

(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

DestructorofPerson

【2】12,2

【3】56789

【4】1123444

 

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

1.解答:

#include

classTest……………………………4分

{intx,y;

public:

voidinit(int,int);

voidprint();

};

voidTest:

:

init(inti,intj)……………………………4分

{x=i;y=j;

}

voidTest:

:

print()……………………………4分

{cout<<"测试结果:

"<

}

2.解答:

①fractionoperator+(fraction&f1,fraction&f2)

{……………………………4分

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

intnfm=f1.fm*f2.fm;

returnfraction(nfz,nfm);

}

②fraction&fraction:

:

operator=(fraction&f)

{……………………………4分

fz=f.fz;

fm=f.fm;

return*this;

}

③istream&operator>>(istream&is,fraction&f)

{……………………………4分

is>>f.fz>>f.fm;

returnis;

}

3.解答:

#include

usingnamespacestd;

classemployee

{……………………………4分

protected:

charname[20];

intempNo;

floatsalary;

public:

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(employee&em)

{inti=0;

while(em.name[i])

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

i++;

}

name[i]='\0';

empNo=em.empNo;

salary=em.salary;

}

voidemployee:

:

setname(char*cp)

{inti=0;

while(*cp)

{name[i]=*cp;

i++;

cp++;

}

name[i]='\0';

}

voidemployee:

:

setempNo(intno)

{empNo=no;

}

voidemployee:

:

setsalary(floatsa)

{salary=sa;

}

char*employee:

:

getname()

{returnname;

}

intemployee:

:

getempNo()

{returnempNo;

}

floatemployee:

:

getsalary()

{returnsalary;

}

voidemployee:

:

display()

{cout<<"工号为"<

}

employeeemployee:

:

compsalary(employee*emp)

{

if(this->salary>=emp->salary)

return*this;

else

return*emp;

}

classworker:

publicemployee……………………………4分

{

public:

worker(char*,int,int);

voidsetproductNum(intn)

{productNum=n;}

intgetproductNum()

{returnproductNum;}

voidpay();

private:

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:

publicemployee……………………………4分

{

public:

technician(char*,int,float);

voidsethours(floath)

{workHours=h;}

floatgethours()

{returnworkHours;}

voidpay();

private:

floatworkHours;

staticfloatpayperhour;//科研人员每小时工作的酬金

};

floattechnician:

:

payperhour=40;//假设科研人员每小时工作的酬金为40元

technician:

:

technician(char*name,intno,floathours)

:

employee(name,no)

{workHours=hours;

}

voidtechnician:

:

pay()

{salary=workHours*payperhour;

}

classsalesman:

publicemployee

{

public:

salesman(char*,int,float);

voidsetmonthlysales(intnum)

{monthlysales=num;}

intgetmonthlysales()

{returnmonthlysales;}

voidpay();

private:

floatmonthlysales;

staticfloatpercent;//销售人员的销售提成

};

floatsalesman:

:

percent=0.5;//假设销售人员的销售提成为50%

salesman:

:

salesman(char*name,intn,floatsn)

:

employee(name,n)

{

monthlysales=sn;

}

voidsalesman:

:

pay()

{

salary=monthlysales*percent;

}

voidmain()……………………………2分

{

workerw1("张三",1001,1000);

techniciant1("李四",1002,200);

salesmans1("王五",1003,10000);

w1.pay();

t1.pay();

s1.pay();

cout<<"工人"<

cout<<"科研人员"<

cout<<"销售人员"<

}

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

当前位置:首页 > 高等教育 > 农学

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

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