南京信息工程大学滨江学院C面向对象程序设计期末复习资料docWord格式文档下载.docx
《南京信息工程大学滨江学院C面向对象程序设计期末复习资料docWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《南京信息工程大学滨江学院C面向对象程序设计期末复习资料docWord格式文档下载.docx(13页珍藏版)》请在冰豆网上搜索。
但由于保留共同基类的多份成员,不仅占用较多的存储空间,还增加了访问这些成天元时的困难,容易出错。
C++提供了虚基类的方法,使得在继承间接共同基类时只保留其一份成员。
18.多态及其分类P145
多态性是指用一个名字定义不同的函数,这些函数执行不同但又类似的操作,从而可以使用相同的调用方式来调用这些具有不同功能的同名函数。
参数多态,包含多态,重载多态,强制多态,前两种称为通用多态,后两种称为专用多态
19.虚函数与重载函数的比较....P153
(1)函数重载处理的是同•层次上的同名函数问题,而虚函数处理的是同-类族中不同派生层次上的同名函数问题,前者是横向重载,后者可以理解为纵向重载。
但与重载不同的是:
同一类族的虚函数的首部是相同的,而函数重载时函数的首部是不同的。
(2)重载函数可以是成员函数或普通函数,而虚两数只能是成员两数。
(3)重载函数调用是以所传递参数序列的差别作为调用不同函数的依据;
虚函数是根据对象的不同去调用不同类的虚函数。
(4)艰函数在运行吋表现出多态功能,这是C++精髄,而重载函数则在编译时表现出多态性。
20,一个类的函数是否是虚函数的条件是:
.....P151
判断该函数与基类的虚函数有相同的名称;
该函数与基类的虚断数有相同的参数个数及相同的对应参数类型;
该函数与基类的虚函数有相同的返冋类型或者满足赋值兼容规则的指针、引用型的返回函数。
21,cincout>
>
<
<
22,根据文件屮数据的组织,文件分为ascii文件和二进制文件
23,强制类型转换:
(int)3.5int(3.5)
24,内联函数inlineP62
该方法的思想是在编译吋将调用函数的代码直接嵌入到调用函数处.INLINE函数的机制兼顾了函数和效率两个方面的有点。
二、看程序结果
1、以下程序的结果是5,75,105,7(全局变量与局部变量问题)
viodmyfunction()
{inty=10;
cout«
x«
"
,”«
y«
endl;
}
intx=5,y=7;
intmain()
{cout«
myfunction();
cout«
«
2、以下程序的结果是—2,122,14(静态变量应用)
intret(inta)
{staticintc=10;
a=a+c;
c=a;
returnc;
{inta=2;
for(inti=0;
i<
2;
i++)cout«
a«
/,ret(a))«
3、以下程序的结果是
3
(指针)
Int
*b=&
a;
a=3;
*b;
}
4、以下程序的结果是
(引用)
{inta=2;
int
&
b=a;
a=3;
b;
5、以下程序的结果是
(参数传递)
{intx⑶,y⑻;
Swap(x,y);
“X二"
«
y«
voidSwap(int&
a」nt&
b)
{intt;
t=a;
a=b;
b=t;
6、以下程序的结果是(参数传递)
Swap(&
x,&
y);
“X二u«
“
y=n«
voidSwap(int
*a,int
*b)
t=*a;
*a=*b;
*b=t;
7、以下程序的结果是
4
int
b=a;
a=a+b;
8、以下程序的结果是
5
(数组)
{inta[5]={2,3,4,5,6};
Int*b=a;
*(b+3);
三、程序填空
1、输入一个整数,将各位数字反转后输出(利用运算符/和%)
intmain(){
intnzrdznewnum;
cin»
n;
do{rd=n%10;
rd;
n=_n/10_;
}while(n!
=0_);
2、输入一系列整数,统计出正整数个数和负整数个数,读入0结束
inti=0,j=0,n;
while(n!
=O){if(n>
0)i++;
if(nvO)_++cin»
n;
coutvv"
正整数个数:
“《ivv〃负整数个数:
〃vvjvvendl;
3、用户输入考试分数(0~100),输出考试等级。
intm;
cin>
m»
endl
switch(
m/10){
case10:
case
9:
/优秀〃;
break;
8:
coutvv"
良好"
;
7:
中等"
6:
及格〃;
default:
/不及格"
■
/
4、交换两个整数
{intx⑸,y(10);
“x=u«
“y二”«
x二u«
“y二"
voidSwap(int_&
a&
b)
{in11;
5、查找数组中元素值为5的元素,返回其序号
intn,a[10]zi=0;
for(i=0;
10;
i++)cin»
a[i];
i++)if(a[i]==5)_countvvi
if(i==10)cout<
v〃nofind"
;
elsecout«
i;
6、查找数组中元素值为'
b,的元素,将其替换为'
B,
chara[10];
inti=0,j=0,n;
i++)
if(a[i]=='
b'
){a[i]='
B'
coutvv〃ok〃;
7、交换两个整数
{intx⑸,y(10);
“x二u«
“y=n«
endI;
Swap(_int&
x,_int&
y);
voidSwap(int*a,int*b)
{jntt;
t=*a;
*a=*b;
*b=t}
2、定义一个复数类,实现两个复数类相加和相减。
P206
要求:
(1)将运算符函数重载为成员函数;
(2)将运算符函数重载为友元函数
#include<
iostream>
usingnamespacestd;
classComplex
{public:
Complex(){real=0;
imag=0;
Complex(doubler,doublei){real=r;
imag=i;
Complexoperator+(Complex&
c2);
Complexoperator-(Complex&
voiddisplay();
private:
doublereal;
doubleimag;
};
voidComplex:
:
display()
{cout<
z/(/z«
real«
z/,,z«
imag«
,zi)z/«
ComplexComplex:
operator+(Complex&
c2){
Complexc;
c・real=real+c2.real;
c.imag二imag+c2・imag;
returnc;
operator-(Complex&
c.real二:
real-c2.real;
c.imag二imag-c2.imag;
intmain()
{
Complexcl(3,4),c2(5,TO),c3,c4;
cl・display();
c2.display();
c3=cl+c2;
"
c3二cl+c2二"
;
c3・display();
c4二cl-c2;
cout<
,,c4=cl-c2=zz;
cd.display();
return0;
}#inelude<
iostream.h>
Complex(doubler,doublei){real=r;
friendComplexoperator+(Complex&
cl,Complex&
friendComplexoperator-(Complex&
,z(z,«
,/,,,«
zri)z,«
c2)
c.real二cl.real+c2・real;
c.imag=cl.imag+c2・imag;
Complexoperator一(Complex&
cl,Comp1ex&
c.real二cl.real-c2.real;
c.imag=c1.imag-c2・imag;
Complexcl(3,4),c2(5,TO),c3,c4;
cl.display();
c2.display();
〃c3二cl+c2二"
c3.display();
z,c4=cl-c2=,/;
c4.display();
return0;
5-7
usingnamespacestd;
classCat{public:
Cat(intage){numOfCats++;
virtual~Cat(){numOfCats-;
staticintgetnumOfCatsj){returnnumOfCats;
}private:
staticintnumOfCats;
intCat:
numOfCats=0;
voidtelepathicFunct:
ion(){cout«
MThereareH«
Cat:
getnumOfCats()«
11catsalive!
\nH;
intmain(){
constintmaxCats=5;
Cat*catHouse[maxCats];
inti;
for(i=0;
i<
maxCats;
i++){catHouse[i]=newCat(i);
telepathicFunction();
i++){deletecatHouse[i];
telepathicFunction();
4-11
Sinclude<
classRectanglc
public:
Rectangle(floatlen,floatwidth)
Length=len;
Width=width;
^Rectangle(){};
floatGetArcaO{rctumLength*Width;
}floatGetLengthO{returnLength;
}floatGetWidthO{returnWidth;
floatLength;
floatWidth;
};
voidmain()
floatlength,width;
cout«
〃请输入矩形的长度:
〃;
cin»
length;
〃请输入矩形的宽度:
cin>
width;
Rectangler(length,width);
〃长为〃«
length«
〃宽为〃«
width«
〃的矩形的面积为:
〃
r.GetArea()<
endl;
3、编写一个类,其成员函数包括:
1)接受键盘输入的英文句子;
2)实现一条英文句子中字母的个数统计并输出;
3)输岀英文句子。
主函数里进行测试。
#include<
classChar
Char(){count二0;
voidInput();
voidshow();
voidGetCount()
z,字母个数为:
〃《countT;
chara[100];
intcount;
voidChar:
Input()
for(inti=0;
i<
100;
i++)
count++;
辻(a[i]==0)//当输入的符合为@是退岀循环
:
show()
count-l;
i++)cout〈〈a[i];
cout〈〈endl;
Chara;
a.Input();
a.show();
a>
GctCount();