001017面向对象程序设计天津大学考试参考资料答案.docx

上传人:b****6 文档编号:5592401 上传时间:2022-12-28 格式:DOCX 页数:26 大小:23.53KB
下载 相关 举报
001017面向对象程序设计天津大学考试参考资料答案.docx_第1页
第1页 / 共26页
001017面向对象程序设计天津大学考试参考资料答案.docx_第2页
第2页 / 共26页
001017面向对象程序设计天津大学考试参考资料答案.docx_第3页
第3页 / 共26页
001017面向对象程序设计天津大学考试参考资料答案.docx_第4页
第4页 / 共26页
001017面向对象程序设计天津大学考试参考资料答案.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

001017面向对象程序设计天津大学考试参考资料答案.docx

《001017面向对象程序设计天津大学考试参考资料答案.docx》由会员分享,可在线阅读,更多相关《001017面向对象程序设计天津大学考试参考资料答案.docx(26页珍藏版)》请在冰豆网上搜索。

001017面向对象程序设计天津大学考试参考资料答案.docx

001017面向对象程序设计天津大学考试参考资料答案

面向对象程序设计复习题

一.概念填空题

1.运算符能够用来访问与局部变量同名的全局变量。

2.运算符动态分配一个对象。

3.类的成员只能被该类的成员函数或友元函数访问。

4.类成员的默认访问模式是的。

5.类的数据成员是该类的所有对象共享的信息。

6.关键字指定了不可修改的对象或变量。

7.要在类的对象上使用运算符,除了运算符和外,其它的必须都要被重载。

8.重载不能改变原运算符的、、和对内部类型对象的原有含义。

9.类的对象可作为类的对象处理。

10.友元函数中可以直接访问类的和成员。

1l.公有成员函数的集合常称为类的函数。

私有成员函数的集合常称为类的函数。

12.为了访问某个类的私有数据成员,必须在该类中声明该类的。

13.提供了一种描述通用类的方法。

14.运算new分配的内存要用运算符回收。

15.参数表示重载后缀++运算符函数。

16.当用受保护的继承从基类派生一个类时,基类的公有成员成为派生类的

的成员,基类的受保护成员成为派生类的成员。

17.在表示数组元中,关键字、和用来建立新的数据类型。

18.限定符用来声明只读变量。

19.函数能够定义一个在不同数据类型基础上完成同一任务的函数。

20.指向基类对象的指针可以指向其派生类的对象,但是不允许指向其派生类的对象。

参考的答案:

1.:

:

2.new3.私有和保护4.私有

5.静态6.const7=&8.优先级、结合性、操作数个数

9.派生类、基类10.私有、受保护11.接口工具

12.友元13.类模板14.delete15.int

16.受保护、受保护17.classstructunion18.const

19.模板20.公有、私有和保护

二.阅读程序写结果

1.

#include

voidmain()

{

inta,b;

charop;

cout<<"请任意一个表达式:

";

cin>>a>>op>>b;

switch(op)

{

case'+':

cout<

case'-':

cout<

case'*':

cout<

case'/':

cout<

cout<

default:

cout<<"运算符错误!

"<

}

}

若程序运行时输入:

5/8

则输出结果是:

参考的答案:

输出结果:

5/8=0

 

2.

#include

#include

voidmain()

{

inti,j,k;

for(i=1;i<=3;i++)//控制输出行

{

for(j=1;j<=6-i;j++)//每行前的空格

cout<<"";//输出四个空格

for(k=1;k<=i;k++)

cout<

for(k=i-1;k>0;k--)

cout<

cout<

}

}

输出结果:

参考的答案:

输出结果:

1

121

12321

 

3.

#include

voidmain()

{

intm,n;

cout<<"输入2个正整数:

";

cin>>m>>n;

while(m!

=n)

{

while(m>n)

m=m-n;

while(n>m)

n=n-m;

}

cout<<”m=”<

}

输入:

921

输出结果:

参考的答案:

输出结果:

m=3

 

4.

#include

classfact{

private:

intn;

public:

voidsetn(inti){n=i;}

intgetn(){returnn;}

longgetfact();

voidprint(){cout<

="<

};

longfact:

:

getfact(){

inti;

longf=1;

for(i=n;i>1;i--)

f*=i;

returnf;

}

voidmain()

{

factfa;

for(inti=1;i<=6;i++)

{

fa.setn(i);

fa.print();

}

}

输出结果:

参考的答案:

输出结果:

1!

=1

2!

=2

3!

=6

4!

=24

5!

=120

6!

=720

 

5.

#include

voidweaver(char*str1,char*str2,char*str3)

{

while(*str1!

=NULL&&*str2!

=NULL)

{

*(str3++)=*(str1++);

*(str3++)=*(str2++);

}

*str3=0;

}

voidmain()

{

charstr1[]天津大学考试参考资料参考的答案="xyz";

char*str2="123";

charstr3[100]天津大学考试参考资料参考的答案;

weaver(str1,str2,str3);

cout<

}

输出结果:

参考的答案:

输出结果:

x1y2z3

 

6.#include

voidmain()

{

inta[4]天津大学考试参考资料参考的答案,i,j,k;

for(i=0;i<4;i++)

a[i]天津大学考试参考资料参考的答案=0;

k=3;

for(i=0;i

for(j=0;j<=k;j++)

a[j]天津大学考试参考资料参考的答案=a[i]天津大学考试参考资料参考的答案+3;

cout<

}

输出结果:

参考的答案:

输出结果:

1518

 

7.#include

#include

voidmain()

{

inti;

char*max,str[3]天津大学考试参考资料参考的答案[10]天津大学考试参考资料参考的答案={"Wang","Zhang","Li"};

max=str[0]天津大学考试参考资料参考的答案;

for(i=0;i<3;i++)

if(strcmp(max,str[i]天津大学考试参考资料参考的答案)<0)

max=str[i]天津大学考试参考资料参考的答案;

cout<<"Themaxstringis:

"<

}

输出结果:

参考的答案:

输出结果:

Themaxstringis:

Zhang

 

8.#include

#include

intf(intp);

voidmain()

{

inta[]天津大学考试参考资料参考的答案={1,2,3,4,5};

for(inti=0;i<5;i++)

cout<

cout<

}

intf(intp)

{

staticints=1;

s*=p;

returns;

}

输出结果:

参考的答案:

12624120

 

9.#include

classcube

{

public:

cube(intl=6,intw=4,inth=2)

{

le=l;

wd=w;

ht=h;

}

~cube()

{

cout<<"le="<

cout<<"volume="<

}

intvolume(){returnle*wd*ht;}

private:

intle,wd,ht;

};

voidmain()

{

cubecone(10,8,5),ctwo;

}

输出结果:

参考的答案:

输出结果:

le=6wd=4ht=2

volume=48

le=10wd=8ht=5

volume=400

 

10.#include

voidmain()

{

inta,b;

for(a=1,b=1;b<=10;b++)

{

if(a>=10)

break;

if(a==1)

{

a+=3;

continue;

}

a-=3;

}

cout<<"a="<

}

输出结果:

参考的答案:

a=1b=11

 

11.#include

classmyclass

{

private:

intx,y;

staticlongsum;

public:

myclass(inta,intb){x=a;y=b;}

voidgetxy()

{

sum*=x*y;

cout<<"sum="<

}

};

longmyclass:

:

sum=1;

voidmain()

{

myclassob1(1,3);

ob1.getxy();

myclassob2(2,4);

ob2.getxy();

myclassob3(5,6);

ob3.getxy();

}

输出结果:

参考的答案:

输出结果:

sum=3

sum=24

sum=720

 

12.#include

intdays[]天津大学考试参考资料参考的答案={31,28,31,30,31,30,31,31,30,31,30,31};

classdate{

private:

inty,m,d;

public:

date(inty1,intm1,intd1){y=y1;m=m1;d=d1;}

date(){y=0;m=0;d=0;}

voiddisp(){cout<

frienddateoperator+(date&d2,intday);

};

dateoperator+(date&d2,intday)

{

datedy;

dy.y=d2.y;

dy.m=d2.m;

day+=d2.d;

while(day>days[dy.m-1]天津大学考试参考资料参考的答案)

{

day-=days[dy.m-1]天津大学考试参考资料参考的答案;

if(++dy.m==13)

{

dy.m=1;

dy.y++;

}

}

dy.d=day;

returndy;

}

voidmain()

{

dateda(2014,9,1),db,dc;

db=da+100;

db.disp();

dc=da+395;

dc.disp();

}

输出结果:

参考的答案:

2014-12-10

2015-10-1

 

13.#include

classAreaclass

{

public:

Areaclass(doublex=0,doubley=0)

{

height=x;

width=y;

}

protected:

doubleheight;

doublewidth;

};

classBox:

publicAreaclass

{

public:

Box(doubleh,doublew):

Areaclass(h,w){}

doubleArea();

};

classTriangle:

publicAreaclass

{

public:

Triangle(doubleh,doublew):

Areaclass(h,w){}

doubleArea();

};

doubleBox:

:

Area(){returnheight*width;}

doubleTriangle:

:

Area(){returnwidth*height*0.5;}

voidmain()

{

Boxobj1(2.5,4.0);

Triangleobj2(4.0,3.5);

cout<<"Box="<

cout<<"Triangle="<

}

输出结果:

参考的答案:

Box=10

Triangle=7

 

三.程序填空

1.以下程序将从键盘输入的20个整数按升序进行排序,并在屏幕上输出排序结果。

请在空白处填入合适的内容。

#include

______________________;

voidmain()

{

inta[20]天津大学考试参考资料参考的答案,i;

for(i=0;i<20;i++)

cin>>a[i]天津大学考试参考资料参考的答案;

__________________;

for(i=0;i<20;i++)//输出排序后结果,每行输出5个数

{

cout<

if(______________)

cout<

}

}

voidsortdata(inta[]天津大学考试参考资料参考的答案)

{

intt;

for(inti=0;i<19;i++)

for(intj=0;j<20-i-1;j++)

if(_______________)

{

t=a[j]天津大学考试参考资料参考的答案;

a[j]天津大学考试参考资料参考的答案=a[j+1]天津大学考试参考资料参考的答案;

a[j+1]天津大学考试参考资料参考的答案=t;

}

}

参考的答案:

voidsortdata(inta[]天津大学考试参考资料参考的答案)

sortdata(a)

++i%5==0

a[j]天津大学考试参考资料参考的答案

 

2.下面是统计一个英文句子中单词个数、最长单词及其字符数的程序,请在空白处填入合适的内容。

#include

#include

voidmain()

{

constintSIZE=20;

charbuf[SIZE]天津大学考试参考资料参考的答案;

charlargest[SIZE]天津大学考试参考资料参考的答案;

intcurLen,maxLen=-1;

;

cout<<"Inputwords:

\n";

while()

{

curLen=strlen(buf);

cnt++;

if()

{

maxLen=curLen;

;

}

}

cout<

cout<

cout<

cout<

}

参考的答案:

intcnt=0

cin>>buf

curLen>maxLen

strcpy(largest,buf)

 

3.以下程序的功能是输出100以内被4整除且个位数为6的所有整数。

请在横线处填写适当内容。

#include

voidmain(){

inti,k;

for(i=0;i<________;i++){

k=+6;

if()continue;

cout<

}

}

参考的答案:

100

i*10

k%4

4.以下程序实现复数加和复数赋制运算,请在空白处填入合适的内容。

#include

classcomplex{

private:

doublereal,image;

public:

complex(){real=0;image=0;}

complex(doubler,doublei){real=r;image=i;}

complexoperator+(complex&a,constcomplex&b);

complex________________(complex&c);

voidprint();

};

complexoperator+(complex&a,constcomplex&b)

{

returncomplex(a.real+b.real,a.image+b.image);

}

complex&complex:

:

operator=(complex&c)

{

real=c.real;

imag=c.imag;

return;

}

voidcomplex:

:

print()

{

cout<

}

voidmain()

{complexc1,c2(1.5,4.0),c3(6.5,-2.8);

complexc4=c2+c3;

c1=c4;

c1.print();

c4.print();

}

参考的答案:

friend

&operator=

*this

 

5.以下的程序实现求分数序列2/1,3/2,5/3,8/5,13/8,21/13,……前10项的和。

在横线处填写适当内容。

#include

#defineN10

voidmain(){

inti,m,n,k;

;

m=2,n=1;

for(i=1;;i++){

s=s+1.0*m/n;

k=m;

m=;

n=;

}

cout<<”s=”<

}

参考的答案:

doubles=0

i<=N

m+n

k

 

6.以下程序将从data1.dat中读出的内容显示在屏幕上,并将其中的字母写到文件data2.dat中去。

请在空白处填入合适的内容。

#include

#include

intmain()

{charch;

fstreaminfile,outfile;

infile.open(,ios:

:

in);

if(!

infile)

{cout<<″文件1不能打开!

\n″;return0;}

outfile.open(,ios:

:

out);

if(!

outfile)

{cout<<″文件2不能打开!

\n″;return0;}

while(infile.get(ch))

{cout<

if(ch>=’a’&&ch<=’z’||ch>=’A’&&ch<=’Z’)

;

}

cout<

infile.close();

outfile.close();

return1;

}

参考的答案:

”data1.dat”

”data2.dat”

ouffile.put(ch)或output<

 

7.为使下面的程序执行后输出矩形的面积,请在空白处填入合适的内容。

#include

classPoint{//定义点类

public:

Point(doublei,doublej)

{x=i;y=j;}

doubleArea()const

{return0.0;}

private:

doublex,y;

};

classRectangle:

publicPoint{//定义矩形类(点类的公有派生类)

public:

Rectangle(doublei,doublej,doublek,doublel):

Point(i,j)

{w=k;h=l;}

{returnw*h;}

private:

doublew,h;

};

voidmain()

{

Point*p;

Rectanglerec(3.5,1.2,5.2,7.5);

;

cout<<"矩形的面积是:

"<Area()<

}

参考的答案:

virtual

virtualdoubleArea()const或doubleArea()const

p=&rec

 

四.编程题

1.输入10个考生的学号和某门课的考试分数(0-100),然后根据每个学生的分数判断并显示每个学生的学号和等级。

等级关系为:

90<=分数<=100,等级为A;80<=分数<90,等级为B;70<=分数<80,等级为C;60<=分数<70,等级为C;0<=分数<60,等级为F。

当分数<0或分数>100时,显示“学号为**的考生成绩输入错”,并重新输入该生的成绩,不再输入学号。

参考的答案:

#include

voidmain()

{

inti,num,score;

cout<<"请输入学生的学号和考试分数(0-100):

"<

for(i=1;i<=10;i

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

当前位置:首页 > 经管营销 > 经济市场

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

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