面向对象程序设计复习题Word格式.docx
《面向对象程序设计复习题Word格式.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计复习题Word格式.docx(38页珍藏版)》请在冰点文库上搜索。
6,6,0
2.#include<
inti,j;
for(i=11;
i<
=20;
i+=2)
{
for(j=2;
j<
i;
j++)
if(i%j==0)
break;
if(j!
=i)
i<
"
"
;
}
1214151618
3.#include<
#include<
intf(intp);
inta[]={1,2,3,4,5};
for(inti=0;
i<
5;
i++)
f(a[i])<
intf(intp)
staticints=1;
s*=p;
returns;
12624120
4.#include<
intf(int*p);
inta[]={1,2,3,4,5}
4;
cout<
f(a+i)<
”“;
intf(int*p)
staticints=0;
s+=*p;
returns;
1361015
4.
inta[5],x,i=0;
do
cin>
>
x;
if(x%2==0)
{
a[i]=x;
i++;
}
}while(i<
5);
for(i=0;
cout<
a[i]<
输入:
165820710
682010
5.#include<
classmyclass
private:
inta,b;
staticints;
public:
myclass(inti,intj)
a=i;
b=j;
Constructor.\n"
}
~myclass()
{
Destructor.\n"
voidsum()
{s+=a*b;
voidprint()
"
”sum=”<
s<
};
intmyclass:
s=0;
{
myclass*a1,*a2;
a1=newmyclass(1,1);
a2=newmyclass(10,20);
(*a1).sum();
(*a2).sum();
a1->
print();
a2->
deletea1;
deletea2;
Constructor.
1,1sum=201
10,20sum=201
Destructor.
6.
classDate
intyear;
intmonth;
intday;
Date(inty,intm,intd)
year=y;
month=m>
0&
&
m<
13?
m:
0;
switch(month)
case0:
d=0;
break;
case1:
case3:
case5:
case7:
case8:
case10:
case12:
day=(d>
d<
32?
d:
0);
case4:
case6:
case9:
case11:
31?
case2:
day=(isleapyear()?
(d>
30?
0):
(d>
29?
0));
intisleapyear()
return!
(year%4!
=0||year%400==0&
year%100!
=0);
if(year<
0)
cout<
公元前"
-year<
年"
else
公元"
year<
if(month)
month<
月"
月份不详!
if(day)
day<
日"
日期不详!
Dated1(1949,10,1),d2(2004,2,29),d3(-996,4,31);
d1.print();
d2.print();
d3.print();
公元1949年10月1日
公元2004年2月29日
公元前996年4月日期不详!
7.#include<
classA
A(inti,intj)
{a=i;
voidmove(intm,intn)
a+=m;
b+=n;
voidshow()
("
)"
classB:
publicA
intx,y;
B(inti,intj,intk,intl):
A(i,j)
{x=k;
y=l;
{cout<
x<
y<
voidfun(){move(30,50);
voidf1(){A:
show();
Ae(10,20);
e.show();
Bb(30,40,50,60);
b.fun();
b.show();
b.f1();
(10,20)
50,60
(60,90)
8.#include<
classB{
private:
intY;
public:
B(inty=0){Y=y;
″B(″<
″)\n″;
~B(){cout<
″~B()\n″;
}
voidprint(){cout<
Y<
″″;
};
classD:
publicB{
intZ;
D(inty,intz):
B(y)
{Z=z;
″D(″<
″,″<
z<
~D(){cout<
″~D()\n″;
voidprint(){
B∶∶print();
cout<
Z<
{Dd(11,22);
d.print();
B(11)
D(11,22)
112
~D()
~B()
9.
classPoint
intxcord,ycord;
Point(intx,inty)
{xcord=x;
ycord=y;
voidmove(intx1,inty1)
xcord+=x1;
ycord+=y1;
xcord<
ycord<
classCircle:
publicPoint
intradius;
Circle(intx,inty,intr):
Point(x,y)
{radius=r;
圆心坐标:
Point:
半径:
radius<
Circlec(10,20,5);
c.show();
c.move(-5,5);
c.show();
(10,20)半径:
5
(5,25)半径:
10.#include<
classA
doubleX,Y;
A(doublexx=0,doubleyy=0)
{X=xx;
Y=yy;
″构造函数被调用(″<
X<
″)″<
A(A&
p){X=p.X;
Y=p.Y;
Af()
{Aa(1,2);
returna;
{Aa(4,5);
Ab(a);
b=f();
构造函数被调用(4,5)
构造函数被调用(1,2)
11.#include<
classA{
virtualvoidf(){cout<
″A:
f()\n″;
publicA
char*buf;
B(inti){
buf=newchar[i];
voidf(){
″B:
~B(){delete[]buf;
{A*a=newA;
a->
f();
deletea;
a=newB(15);
a->
A:
f()
B:
12.#include<
iomanip.h>
voidmain()
{inta[9]={1,2,3,4,5,6,7,8,9};
for(inti=0;
9;
i++){
setw(4)<
a[i];
if(i%3==2)
123
456
789
13.#include<
template<
typenameT>
voidprint(Ta[],intn)
{for(inti=0;
n;
i++)
{cout<
a[i]<
″″;
if(i%5==4)
inta[]={1,2,3,4,5,6,7};
doubleb[4]={8,9,10,11};
print(a,sizeof(a)/sizeof(int));
print(b,4);
12345
67
891011
14.#include<
staticintn;
intX;
A(intx=0){X=x;
n++;
~A(){n--;
staticintGetNum(){returnn;
voidprint();
voidA∶∶print(){cout<
″n=″<
n<
″,X=″<
X<
endl;
intA∶∶n=0;
{A*p=newA(12);
p->
Aa(34);
a.print();
deletep;
A:
GetNum()<
n=1,X=12
n=2,X=34
n=1
15.#include<
voidmain(void)
{intn=6,k;
Factors"
for(k=2;
k<
n;
k++)
if(n%k==0)
6Factors23
16.#include<
classB{
virtualintf(){return0;
classD:
publicB{
intf(){return100;
voidmain(){
Dd;
B&
b=d;
b.f()<
b.B:
f()<
100
三、程序填空
1.菲波纳齐数列为:
1,1,2,3,5,8,13,……;
下面是实现计算该数列前10项的程序,请在空白处填入合适的内容。
longfa(intm)//递归函数
longf;
if(m>
2)
____________
else
_____________
returnf;
for(inti=1;
11;
______<
“ ”;
f=fa(n-1)+fa(n-2);
f=1;
fa(i)
2.下面是实现字符在字符串中出现频率的程序,请在空白处填入合适的内容。
intnchar(char*s,charc)
intn(0);
____________;
if(s[i]==c)
n++;
_________________
charstr[80],ch;
输入字符串:
str;
输入一个字符:
;
ch;
ch<
在"
str<
中的出现频率是:
______________<
endl;
s[i]
returnn;
nchar(str,ch)
3.下面是将一个一维数组及各元素乘积写入文本文件data.txt的程序,请在空白处填入合适的内容。
_________________
stdlib.h>
voidmain(){
inta[10]={1,2,3,4,5,6,7,8,9,10},cum=0;
_____________________
if(!
fout){//打开文件失败
Cannotopenthefile!
exit
(1);
inti;
10;
i++){
cum*=a[i];
_________________//将数组元素写入文件
_________________//将元素的乘积写入文件
fout.close();
fstream.h>
ofstreamfout("
data.txt"
);
fout<
cum<
4.下面是一个判断一个整数是否素数的程序,请在空白处填入合适的内容。
classinteger{
intnumber;
integer(intn){________________}
intgetnum(){returnnumber;
boolisprime(){
for(inti=2;
number;
if(number%i==0)
break;
if(______________)
returntrue;
returnfalse;
integern1(19);
if(_______________)
n1.getnum()<
是素数!
else
不是素数!
number=n;
number==i;
n1.isprime()
5.下面是利用运算符重载实现字符串赋值(=)的程序,请在空白处填入合适的内容。
#include<
string.h>
classCstring
{char*s;
Cstring(char*s1=0){
s=newchar[strlen(s1)+1];
strcpy(s,s1);
__________________________
voidlist(){cout<
CstringCstring:
operator=(Cstring&
s1)
deletes;
s=newchar[strlen(s1.s)+1];
strcpy(s,s1.s);
Cstringcstr1("
abcd"
),cstr2("
1234"
__________________
cstr1.list();
Cstringoperator=(Cstring&
s1);
return*this;
cstr1=cstr2;
6.为使下面程序输出结果为:
1*2
3+4
请在横线处填上适当的字句,以使程序完整。
intR1,R2;
A(intr1,intr2){R1=r1;
R2=r2;
voidprint()const;
voidA:
print()
R1<
①__________<
R2<
print()const
②__________<
Aa(1,2);
constAb(3,4);
b.print();
“*”“+”
7下面是利用运算符重载实现两个一维等长数组相加(+)的程序,请在空白处填入合适的内容。
classArray
int*a;
intn;
Array(intn1){
__________________
n=n1;
voidinput(){
for(inti=0;
i++)
cin>
a[i];