c++期末复习资料.docx

上传人:b****6 文档编号:15802811 上传时间:2023-07-08 格式:DOCX 页数:19 大小:17.89KB
下载 相关 举报
c++期末复习资料.docx_第1页
第1页 / 共19页
c++期末复习资料.docx_第2页
第2页 / 共19页
c++期末复习资料.docx_第3页
第3页 / 共19页
c++期末复习资料.docx_第4页
第4页 / 共19页
c++期末复习资料.docx_第5页
第5页 / 共19页
c++期末复习资料.docx_第6页
第6页 / 共19页
c++期末复习资料.docx_第7页
第7页 / 共19页
c++期末复习资料.docx_第8页
第8页 / 共19页
c++期末复习资料.docx_第9页
第9页 / 共19页
c++期末复习资料.docx_第10页
第10页 / 共19页
c++期末复习资料.docx_第11页
第11页 / 共19页
c++期末复习资料.docx_第12页
第12页 / 共19页
c++期末复习资料.docx_第13页
第13页 / 共19页
c++期末复习资料.docx_第14页
第14页 / 共19页
c++期末复习资料.docx_第15页
第15页 / 共19页
c++期末复习资料.docx_第16页
第16页 / 共19页
c++期末复习资料.docx_第17页
第17页 / 共19页
c++期末复习资料.docx_第18页
第18页 / 共19页
c++期末复习资料.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

c++期末复习资料.docx

《c++期末复习资料.docx》由会员分享,可在线阅读,更多相关《c++期末复习资料.docx(19页珍藏版)》请在冰点文库上搜索。

c++期末复习资料.docx

c++期末复习资料

一、选择题:

30分(考15个,有13在习题集中,考原题)

二、概念题10分(习题集加课本)

三、程序阅读32分

四、填空题(三个选一个,会有所改动)

1.在下面程序中定义了类A、A1和A2,其中A1类由A类公有派生,A2类由A1类公有派生。

上述三个类的定义并不完整,请按照要求完成下列操作,将程序补充完整。

定义类A的构造函数,该构造函数有一个整型的参数x,在构造函数中将x赋值给数据成员a。

请在注释“//***1***”之后添加适当的语句。

定义类A1的构造函数,该构造函数有两个整型的参数x和y,在构造函数中将x赋值给数据成员b,将y作为基类A的构造函数的参数传入。

请在注释“//***2***”之后添加适当的语句。

定义类A2的构造函数,该构造函数有三个整型的参数x、y和z,在构造函数中将x赋值给数据成员c,将y和z作为基类A1的构造函数的参数传入。

请在注释“//***3***”之后添加适当的语句。

完成类A2的成员函数show的定义,该函数调用基类成员函数,输出基类数据成员a和b及类A2自身数据成员c的值,上述3个值在输出时以空格隔开。

请在注释“//***4***”之后添加适当的语句。

注意:

除在指定位置添加语句之外,不要改动程序中德其他内容。

程序输出结果如下:

9

6

962

源程序文件清单如下:

#include

classA

{

inta;

public:

______________________________//***1***

intgeta(){returna;}

};

classA1:

publicA

{

intb;

public:

______________________________//***2***

intgetb(){returnb;}

};

classA2:

publicA1

{

intc;

public:

______________________________//***3***

voidshow()

{

______________________________//***4***

}

};

voidmain()

{

A2a(2,6,9);

cout<

cout<

a.show();

}

2..根据给定的程序执行结果,将下列程序补充完整(每空2分,10分)

#include

#include

usingnamespacestd;

classStudent//声明基类Student

{public:

Student(intn,stringnam,chars)//基类构造函数

{num=n;

name=nam;

sex=s;}

protected:

intnum;

stringname;

charsex;

};

classStudent1:

publicStudent//声明派生类Student1

{public:

Student1(intn,stringnam,chars,inta):

  ① //调用基类构造函数

{————②———//在函数体中只对派生类新增的数据成员初始化

}

voidshow()

{—————③——————//根据输出结果填写输出语句

cout<<″name:

″<

cout<<″sex:

″<

cout<<″age:

″<

}

private:

intage;

};

intmain()

{Student1stud1(———④—————);//根据输出结果填写实际参数

   ⑤//根据程序的运行结果调用相应的成员函数

return0;

}

运行结果为

num:

10010

name:

Wang-li

sex:

f

age:

19

3.分析以下程序,根据输出结果完善程序。

要求:

(1)在主函数中不可以通过对象访问类中的所有数据成员。

(2)程序的运行结果为;3,6,9。

#include

classA

(1)______________________________//最合理的访问特征

inta;

public;

A(inti=0){a=i;}

};

classB

{

(2)_________________________________//最合理的访问特征

intb;

public:

B(inti=0){b=i;}

};

classC:

publicA

{

intc;

Bb1;

public:

(3)_______________________________//根据运行结果定义构造函数

{(4)___________________________________________________}

voidshow()

{

(5)____________________________//输出c的所有的数据成员

}

};

voidmain()

{

Cc1(3,6,9);

c1.show();

}

答案:

1.

(1)A(intx){a=x;}

(2)A1(intx,inty):

A(y){b=x;}

(3)A2(intx,inty,intz):

A1(y,z){c=x;}

(4)cout<

2.

(1)Student(n,nam,s)

(2)age=a;

(3)cout<<″num″<

(4)10010,”Wang-li”,’f’,19

(5)stud1.show();

3.

(1)protected:

(2)public:

(3)C(intn,intm,intw):

A(n),b1(m)

(4)c=w;

(5)cout<

五、编程题:

(答案是个人编程,用于参考)

题型1:

重载

1.课本149第四题1.cpp

矩阵之和:

1.#include

usingnamespacestd;

classM

{

public:

M(){a[0][0]=0;a[0][1]=0;a[0][2]=0;a[1][0]=0;a[1][1]=0;a[1][2]=0;}

Moperator+(Mb);

voidset();

voidshow();

private:

inta[2][3];

};

MM:

:

operator+(Mb)

{

Mc;

inti,j;

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

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

c.a[i][j]=a[i][j]+b.a[i][j];

returnc;

}

voidM:

:

set()

{inti,j;

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

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

cin>>a[i][j];

}

voidM:

:

show()

{

inti,j;

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

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

cout<

}

intmain()

{

Ma,b,c;

a.set();b.set();

c=a+b;

c.show();

return0;

}

 

2.#include

usingnamespacestd;

classM

{

public:

M(){a[0][0]=0;a[0][1]=0;a[0][2]=0;a[1][0]=0;a[1][1]=0;a[1][2]=0;}

M(inta1,inta2,inta3,inta4,inta5,inta6){a[0][0]=a1;a[0][1]=a2;a[0][2]=a3;a[1][0]=a4;a[1][1]=a5;a[1][2]=a6;}

Moperator+(Mb);

voidset();

voidshow();

private:

inta[2][3];

};

MM:

:

operator+(Mb)

{

Mc;

inti,j;

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

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

c.a[i][j]=a[i][j]+b.a[i][j];

returnc;

}

voidM:

:

set()

{inti,j;

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

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

cin>>a[i][j];

}

voidM:

:

show()

{

inti,j;

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

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

cout<

}

intmain()

{

Ma(1,2,3,4,5,6),b(1,1,1,1,1,1),c;

c=a+b;

c.show();

return0;

}

 

3.课本149第5题.cpp

#include

//usingnamespacestd;

classM

{

public:

M(){a[0][0]=0;a[0][1]=0;a[0][2]=0;a[1][0]=0;a[1][1]=0;a[1][2]=0;}

Moperator+(Mb);

friendostream&operator<<(ostream&,M&);

friendistream&operator>>(istream&,M&);

private:

inta[2][3];

};

MM:

:

operator+(Mb)

{

Mc;

inti,j;

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

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

c.a[i][j]=a[i][j]+b.a[i][j];

returnc;

}

istream&operator>>(istream&input,M&f)

{inti,j;

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

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

input>>f.a[i][j];

returninput;

}

ostream&operator<<(ostream&output,M&f)

{

inti,j;

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

{for(j=0;j<3;j++)

output<

output<

}

returnoutput;

}

intmain()

{

Ma,b,c;

cin>>a;

cin>>b;

c=a+b;

cout<

return0;

}

 

题型2:

继承

1.用单一继承方式定义平面坐标体系下的"点"类Point,"线"类Line和"三角形"类Triangle。

Line类单一

继承Point类,另外增加第2个点坐标。

Triangle类单一继承Line类,另外增加第3个点坐标。

在主函数中定义一个三角形对象,调用成员函数求出并输出三角形的3个顶点坐标,三角形的周长和面积。

请设计这3个类:

classPoint{};

classLine{};

classTriangle{};

main()

{

Triangles1(0,0,1,1,1,0);

s1.show1();//输出s1这个三角形的3个顶点。

s1.show2();//计算并输出s1这个三角形的周长。

s1.show3();//计算并输出s1这个三角形的面积。

}

 

#include

#include

usingnamespacestd;

classPoint

{public:

Point(inta,intb){a1=a;a2=b;}

voiddisplay()

{cout<

protected:

inta1;

inta2;

};

classLine:

publicPoint

{

public:

Line(inta,intb,intc,inte):

Point(a,b)

{

b1=c;

b2=e;

}

voidplay()

{

display();

cout<

}

protected:

intb1;

intb2;

};

classTriangle:

publicLine

{

public:

intq;

Triangle(inta,intb,intc,inte,intf,intg):

Line(a,b,c,e)

{

c1=f;

c2=g;

}

voidshow1();

voidshow2();

voidshow3();

protected:

intc1;

intc2;

};

voidTriangle:

:

show1()

{cout<<"输出s1这个三角形的3个顶点"<

play();

cout<

}

 

voidTriangle:

:

show2()

{

doublel1,l2,l3;

doubleq;

cout<<"计算并输出s1这个三角形的周长"<

l1=sqrt(pow((a1-b1),2)+pow((a2-b2),2));

l2=sqrt(pow((a1-c1),2)+pow((a2-c2),2));

l3=sqrt(pow((b1-c1),2)+pow((b2-c2),2));

q=l1+l2+l3;

cout<

}

voidTriangle:

:

show3()

{

doubles;

doublel1,l2,l3;

doubleq;

cout<<"计算并输出s1这个三角形的面积"<

l1=sqrt(pow((a1-b1),2)+pow((a2-b2),2));

l2=sqrt(pow((a1-c1),2)+pow((a2-c2),2));

l3=sqrt(pow((b1-c1),2)+pow((b2-c2),2));

q=l1+l2+l3;

q=q/2;

s=sqrt(q*(q-l1)*(q-l2)*(q-l3));

cout<

}

 

voidmain()

{

Triangles1(0,0,1,1,1,0);

s1.show1();//输出s1这个三角形的3个顶点。

s1.show2();//计算并输出s1这个三角形的周长。

s1.show3();//计算并输出s1这个三角形的面积。

}

2.先定义“高度类”Hight和“圆”类Circle,再由Hight和Circle多重派生出“圆柱体”类Cylinder。

在主函数中定义一个圆柱体对象,调用成员函数求出圆柱体的体积和表面积。

intmain()

{

Cylindert;

cin>>t;

t.show1();//输出体积

t.show2();//输出表面积

return0;

}

答案:

#include

#definePI3.14

classHight//定义"高度类"Hight

{

public:

Hight(doublei=0)

{

h=i;

}

protected:

doubleh;

};

classCircle//定义"圆"类Circle

{

public:

Circle(doublej=0)

{

r=j;

}

protected:

doubler;

};

 

classCylinder:

publicHight,publicCircle//类Cylinder

{

public:

friendistream&operator>>(istream&input,Cylinder&c);

Cylinder(doublei=0,doublej=0):

Hight(i),Circle(j){}

voidshow1();

voidshow2();

};

 

voidCylinder:

:

show1()

{

doublev;

v=PI*r*r*h;

//v=3.14*r*r*h;

cout<<"输出体积"<

cout<

}

 

voidCylinder:

:

show2()

{

doubles;

s=PI*r*r*2+PI*r*2*h;

//s=3.14*r*r*2+3.14*r*2*h;

cout<<"输出表面积"<

cout<

}

 

istream&operator>>(istream&input,Cylinder&c)

{

input>>c.h>>c.r;

returninput;

}

intmain()

{

Cylindert;

cin>>t;

t.show1();//输出体积

t.show2();//输出表面积

return0;

}

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

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2