C++面向对象程序设计复习资料文档格式.docx
《C++面向对象程序设计复习资料文档格式.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计复习资料文档格式.docx(16页珍藏版)》请在冰点文库上搜索。
![C++面向对象程序设计复习资料文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/8/98c685b8-34fe-4733-a77e-c324960bdd06/98c685b8-34fe-4733-a77e-c324960bdd061.gif)
C.template<
classT1,classT2>
D.template<
classT1;
classT2>
二、填空题(20分)
1、C源程序的扩展名为.C,C++源程序文件的扩展名为____.CPP______。
2、多行注释的开始标记符和结束标记符分别为____/*______和____*/______。
3、从一条函数原型语句“floatfun1(void);
”可知,该函数的返回类型为__float____,该函数带有___0___个参数。
4、在C++语言中输入、输出操作是通过 I/O流类库 来实现的。
5、当一个类对象撤销时将自动调用该类的_____析构函数_______。
6、对于类中定义的成员,其隐含的访问权限是_____私有(private)_______。
7、在一般情况下,用点运算符(.)来引用对象成员,当用指向对象的指针来引用对象成员时,要用____->
______操作符。
8、假定AB为一个类,若用户没有给其定义构造函数,则系统自动为其隐含定义的构造函数为______AB:
:
AB{}________。
9、若需要把intF(void)函数定义为类A的友元函数,则应在类A的定义中加入一条语句_____friendintF(void);
___________。
10、类模板的使用实际上是将类模板实例化成一个具体的类,它的格式为____类名<
实际类型>
对象名_____________。
三、程序分析题(20分)
1、下面是一个C程序,改写它,使它采用C++风格的I/O语句。
#include<
stdio.h>
main()
{
inta,b,d,min;
printf("
Entertwonumbers:
"
);
scanf("
%d%d"
&
a,&
b);
min=a>
b?
b:
a;
for(d=2;
d<
min;
d++)
if((a%d==0)&
&
(b%d==0))break;
if(d==min)
{printf("
Nocommondenominators\n"
return0;
}
Thelowestcommondenominatoris%d"
d);
}
iostream.h>
cout<
<
”Entertwonumbers:
;
cin>
>
a>
b;
{
cout<
Nocommondenominators<
endl"
return0;
Thelowestcommondenominatoris"
d;
2、阅读下列程序,写出程序的运行结果。
。
#include<
classA{
inti,j;
public:
A(inta,intb){i=a;
j=b;
voidadd(intx,inty)
{i+=x;
j+=y;
voidprint(){cout<
”i=”<
i<
’\t’<
”j=”<
j<
’\n’;
};
classB:
publicA{
intx,y;
public:
B(inta,intb,intc,intd):
A(a,b)
{x=c;
y=d;
voidad(inta,intb)
{x+=a;
y+=b;
add(-a,-b);
voidprint(){cout<
”x=”<
x<
”y=”<
y<
voidmain()
Aa(100,200);
a.print();
Bb(200,300,400,500);
b.ad(50,60);
b.A:
print();
b.print();
运行结果:
i=100j=200
i=150j=240
i=450j=560
3、下面是一个类的定义,存在着3处语法错误(有的为单个字符),请指出错误行的行号并改正。
classCE{//1行
private//2行
inta,b;
//3行
intgetmin(){return(a<
a:
}//4行
//5行
intc;
//6行
voidSetValue(intx1,intx2,intx3){//7行
a=x1;
b=x2;
c=x3;
//8行
}//9行
intGetMin();
//10行
}//11行
intGetMin(){//12行
intd=getmin();
//13行
return(d<
c?
d:
c);
//14行
}//16行
错误行的行号为、________和________。
分别改正为_________________、________________和___________________。
21112
private:
};
intCE:
GetMin(){
4.阅读程序,写出运行结果。
#include<
string.h>
classperson
{intage;
charname[10];
person:
person(char*str,inta=20)
{strcpy(name,str);
age=a;
voidinit(inti,char*str)
{age=i;
strcpy(name,str);
voiddisplay()
{cout<
name<
”is”<
age<
”yearsold.\n”;
{persondemo1;
demo1.init(30,”li-ming”);
demo1.display();
persondemo2(”wang-yan”);
demo2.display();
12/25/2004
10:
13:
56
5、写出下列程序的运行结果。
classDate
Date(inty=2001,intm=1,intd=1){Year=y;
Month=m;
Day=d;
voidPrintDate(){cout<
Year<
/"
Month<
Day<
endl;
protected:
intYear,Month,Day;
classTime
Time(inth=5,intm=30,ints=0){Houre=h;
Minutes=m;
Seconds=s;
voidPrintTime(){cout<
Houre<
Minutes<
Seconds<
intHoure,Minutes,Seconds;
classDate_Time:
publicDate,publicTime
Date_Time(){};
Date_Time(inty,intmo,intd,inth=0,intmi=0,ints=0):
Date(y,mo,d),Time(h,mi,s){}
voidPrintDate_Time(){PrintDate();
PrintTime();
voidmain()
{
Date_Timea,b(2002,10,1,16,20,0),c(2005,4,18,6,7);
a.PrintDate_Time();
b.PrintDate_Time();
c.PrintDate_Time();
2001/1/1
5:
30:
2002/1/1
16:
20:
2005/4/18
6:
7:
6.#include<
classcomplex
{public:
complex(doubler=0.0,doublei=0.0)
{real=r,imag=i;
}
complexoperator+(complexc2);
complexoperator-(complexc2);
complexoperator=(complexc2)
{real=c2.real;
imag=c2.imag;
returncomplex(real,imag);
voiddisplay();
doublereal;
doubleimag;
complexcomplex:
operator+(complexc2)
{returncomplex(real+c2.real,imag+c2.imag);
operator-(complexc2)
{returncomplex(real-c2.real,imag-c2.imag);
voidcomplex:
display()
("
real<
"
imag<
)"
{complexcomp1(5.0,8.0),comp2(2.0,3.0),comp3,comp4;
comp3=comp1+comp2;
comp4=comp1-comp2;
comp3.display();
comp3.display();
(7.0,11.0)
(3.0,5.0)
7、写出下列程序的运行结果。
inti=15;
inti;
i=100;
i=i+1;
emdl;
程序运行结果_____101____________
8.下面是一个类的定义,存在着3处语法错误(有的为单个字符),请指出错误行的行号并改正。
//2行
public//5行
//9行
//11行
5912
}intCE:
四、简答题(15分)
1、对象有那些特性?
对象的特性:
封装性、模块独立性、动态连接性、易维护性
2、分析使用友元的利弊。
友员机制是对类的封装机制的补充,方便了编程,提高了程序运行效率、实现了类之间的数据共享。
但引入友元破坏了数据的隐蔽性和类的封装性,降低程序的可维护性,这与面向对象程序设计思想是背道而驰的,因此要谨慎使用。
3、构造函数与析构函数的主要作用是什么?
为什么构造函数允许重载而析构函数不允许重载?
构造函数的主要作用:
当建立类对象时,为对象分配存储空间,进行必要的初始化。
析构函数的主要作用:
当撤消类对象时,收回存储空间,并做一些善后工作。
构造函数可以有任意类型的参数符合函数重载的条件,因而能够被重载;
析构函数没有参数也没有返回值,因而不能重载。
4、编译时的多态性与运行时的多态性有什么区别?
它们的实现方式有什么不同?
静态联编支持的多态性称为编译时的多态性,要求在程序编译时就知道调用函数的全部信息。
动态联编支持的多态性称为运行时的多态性,程序运行时才能确定调用哪个函数。
编译时的多态性是通过函数重载和运算符重载实现的,运行时的多态性是通过继承和虚函数来实现的。
五、编程题(30分)
1、输入10个学生的成绩,求平均成绩,并将低于平均成绩的分数打印出来。
floatave,score[10];
”请输入10个学生的成绩.”<
for(i=0;
10;
i++)
cin>
score[i];
floatsum=0;
sum+=score[i];
ave=sum/10;
”低于平均成绩的分数是:
”;
if(score[i]<
ave)cout<
2、定义一个类Score,它含有私有成员English_score(英语分数)、公有成员函数Setscore()和Printscore(),其中Setscore()用来设置English_score的值,Printscore()用来输出English_score的值。
在主程序中定义类Score的两个对象stu1和stu2,其英语成绩分别为88.5和94.5,输出这两个分数。
classScore{
floatEnglish_score;
voidSetscore(floata)
{English_score=a;
voidPrintscore()
English_score<
Scorestu1,stu2;
stu1.Setscore(88.5);
stu2.Setscore(94.5);
”stu1的English_scores是:
”<
stu1.Printscore();
”stu2的English_score是:
stu2.Printscore();
3、建立类Cylinder,Cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。
用类Cylinder计算圆柱体的体积,并存储在一个double变量中。
在类Cylinder中包含一个成员函数Vol(),用来显示每一个Cylinder对象的体积。
在主程序中定义一个类Cylinder的对象C1,从键盘输出其半径和高度,输出该圆柱体的体积。
classCylinder{
doublerdius,height;
Cylinder(doubler,doubleh)
{rdius=r;
height=h}
doubleVol()
{return(3.1415926*radius*radius*height);
doubleR,H;
”请输入圆柱体的半径:
R;
”请输入圆柱体的高度:
H;
CylinderC1(R,H);
”圆柱体C1的体积为:
C1.Vol();
4、基于对象编程。
创建长方柱类(Box),它包含length(长)、width(宽)、height(高)三个数据成员以及三个成员函数get_value()、volume()、display(),三个成员函数分别完成如下功能:
(1)由键盘分别输入3个长方柱的长、宽、高;
(2)计算长方柱的体积;
(3)输出3个长方柱的体积。
试编写有关程序。
iostream>
usingnamespacestd;
classBox
{public:
voidget_value();
floatvolume();
floatlen;
floatwid;
floathgh;
voidBox:
get_value()
{cout<
pleaseinputlengh,width,height:
len;
wid;
hgh;
floatBox:
volume()
{return(len*wid*hgh);
volume()<
intmain()
{Boxbox1,box2,box3;
box1.get_value();
volmueofbax1is"
box1.display();
box2.get_value();
volmueofbax2is"
box2.display();
box3.get_value();
volmueofbax3is"
box3.display();