C++题目x答案文档格式.docx

上传人:b****4 文档编号:16948231 上传时间:2022-11-27 格式:DOCX 页数:14 大小:21.04KB
下载 相关 举报
C++题目x答案文档格式.docx_第1页
第1页 / 共14页
C++题目x答案文档格式.docx_第2页
第2页 / 共14页
C++题目x答案文档格式.docx_第3页
第3页 / 共14页
C++题目x答案文档格式.docx_第4页
第4页 / 共14页
C++题目x答案文档格式.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

C++题目x答案文档格式.docx

《C++题目x答案文档格式.docx》由会员分享,可在线阅读,更多相关《C++题目x答案文档格式.docx(14页珍藏版)》请在冰豆网上搜索。

C++题目x答案文档格式.docx

Max(7.0,3.2,9.9)<

30、cout<

Max('

a'

'

z'

b'

);

31、}

 

32、设计一个立方体类Box,它能计算并输出立方体的体积和表面积。

 

33、#include<

34、usingnamespacestd;

35、

36、classBox

37、{public:

38、floatL;

39、floatgetBMJ(){returnL*L*6;

40、floatgetTJ(){returnL*L;

41、Box(floatin){L=in;

42、};

43、

44、voidmain()

45、{

46、Boxr(10);

47、cout<

边长:

10\n表面积:

r.getBMJ()<

\n体积:

r.getTJ();

48、}

4、编写一个程序计算“三角形、正方形、圆形”三种图形的面积。

要求:

a)抽象出一个基类base;

b)在其中说明一个虚函数用来求面积;

c)利用派生类定义“三角形、正方形、圆形”;

d)编写主函数并测试。

1.#include<

2.usingnamespacestd;

3.

4.classbase

5.{public:

6.virtualfloatgetMJ(){returnH*W;

7.floatH,W;

8.};

9.

10.classR:

publicbase

11.{public:

12.floatgetMJ(){returnH*H*3.14;

13.R(floatin){H=in;

14.};

15.

16.classA:

17.{public:

18.floatgetMJ(){return(H*W)/2;

19.A(floatin_H,floatin_w){H=in_H;

W=in_w;

20.};

21.

22.classS:

23.{public:

24.floatgetMJ(){returnH*H;

25.S(floatin){H=in;

26.};

27.

28.voidmain()

29.{

30.Rr(10);

31.Aa(10,5);

32.Ss(10);

33.cout<

圆:

10\n面积:

r.getMJ()<

endl

34.<

\n三角:

高:

10,底:

5\n面积:

a.getMJ()<

35.<

\n正方形:

边长:

s.getMJ();

36.}

5、定义一个处理日期的类TDate,它有3个私有数据成员:

Month,Day,Year和若干个公有成员函数,并实现如下要求:

①构造函数重载;

②成员函数设置缺省参数;

③定义一个友元函数来打印日期;

④定义一个非静态成员函数设置日期;

⑤可使用不同的构造函数来创建不同的对象。

1.include<

4.classTDate

5.{

6.public:

7.TDate():

Year(1900),Month

(1),Day

(1){;

8.TDate(intY,intM=1,intD=1){Month=M;

Day=D;

Year=Y;

9.voidset(intY=1990,intM=1,intD=1){Month=M;

10.friendvoidshow(TDate&

in);

11.private:

12.intMonth,Day,Year;

13.};

14.

15.voidshow(TDate&

in){cout<

in.Year<

年"

in.Month<

月"

in.Day<

日\n"

;

16.

17.voidmain()

18.{

19.TDatet1;

20.TDatet2(2014);

21.TDatet3(2015,6,5);

22.show(t1);

23.show(t2);

24.show(t3);

25.

26.t3.set(1999);

27.show(t3);

28.}

6、编程实现抽象类Employee,派生类Manger和HourlyWorker,Employee有数据成员姓名name和工号ID,Manger有数据成员sal,代表经理的月工资,HourlyWorker有wage和hours,分别代表钟点工的每小时的工资数和月工作时数,定义的所有类中必须包含构造函数、析构函数、修改和获取所有数据成员的成员函数,以及虚函数来计算职员的工资、输出职员的姓名name和工号ID。

2.#include<

string>

3.usingnamespacestd;

4.

5.classEmployee

6.{public:

7.stringname;

8.intid;

9.virtualintgetSal(){return0;

10.Employee():

name("

未命名"

),id(0){};

11.~Employee(){cout<

析构\n"

12.voidset(stringN,intI){id=I;

name=N;

}

13.voidshowSal()

14.{

15.cout<

\n姓名:

name<

16.<

工号:

id<

17.<

工资:

getSal()<

18.}

19.};

20.

21.classManger:

publicEmployee

22.{

23.public:

24.Manger(intS){sal=S;

25.intgetSal(){returnsal;

26.intsal;

27.};

28.classHourlyWorker:

29.{

30.public:

31.HourlyWorker(intW,intH){wage=W;

hours=H;

32.intgetSal(){returnwage*hours;

33.intwage,hours;

34.};

35.

36.

37.voidmain()

38.{

39.HourlyWorkerh(10,20);

40.h.set("

小时工A"

777);

41.h.showSal();

42.Mangerm(999);

43.m.set("

经理A"

888);

44.m.showSal();

45.}

1.

8、回答下面问题。

下面列出了由三个文件main.cpp、MyClass.h和MyClass.cpp组成的一个程序。

文件main.cpp中实现了主函数;

文件MyClass.h中定义了类MyClass;

文件MyClass.cpp中实现了类MyClass的成员函数。

题中没有给出三个文件的完整实现。

仔细阅读所给的程序,根据题意补充文件MyClass.h和文件MyClass.cpp的内容。

a.将成员变量定义为私有的;

b.不要增加题中没有用到的类成员。

1./********************************************************/

2.//文件main.cpp

3.#include<

iostream.h>

4.#include"

MyClass.h"

5.voidmain()

6.{

7.  MyClassobj(3.3);

8.  cout<

obj.GetMember()<

endl;

9.  obj.SetMember(5.6);

10.  cout<

11.}

12./***********************************************************************/

13./文件MyClass.cpp

14.#include"

15.floatMyClass:

:

GetMember()

16.{

17.returnmember;

18.}

19.voidMyClass:

SetMember(floatmember)

20.{

21.MyClass:

member=member;

22.}

23.//在这里写出构造函数的实现函数

24.

25.MyClass:

MyClass(floatin)

26.{

27.Member=in;

28.}

29.

30./***********************************************************************/

31.//文件MyClass.h

32.//类MyClass的定义

33.classMyClass{

34.//在下面写出类的定义体

35.public:

36.MyClass(floatin)

37.floatGetMember();

38.voidSetMember(floatmember);

39.private:

40.floatmember;

41.};

10、某公司雇员(employee)包括经理(manager),技术人员(technician)和销售员(salesman)。

开发部经理(developermanger),既是经理也是技术人员。

销售部经理(salesmanager),既是经理也是销售员。

以employ类为虚基类派生出manager,technician和salesman类;

再进一步派生出developermanager和salesmanager类。

employee类的属性包括姓名、职工号、工资级别,月薪(实发基本工资加业绩工资)。

操作包括月薪计算函数(pay()),该函数要求输入请假天数,扣去应扣工资后,得出实发基本工资。

technician类派生的属性有每小时附加酬金和当月工作时数,及研究完成进度系数。

业绩工资为三者之积。

也包括同名的pay()函数,工资总额为基本工资加业绩工资。

salesman类派生的属性有当月销售额和酬金提取百分比,业绩工资为两者之积。

manager类派生属性有固定奖金额和业绩系数,业绩工资为两者之积。

工资总额也为基本工资加业绩工资。

而developermanager类,pay()函数是将作为经理和作为技术人员业绩工资之和的一半作为业绩工资。

salesamanager类,pay()函数则是经理的固定奖金额的一半,加上部门总销售额与提成比例之积,这是业绩工资。

编程实现工资管理。

特别注意pay()的定义和调用方法:

先用同名覆盖,再用运行时多态。

同第6题

9、以点(point)类为基类,重新定义矩形类和圆类。

点为直角坐标点,矩形水平放置,由左下方的顶点和长宽定义。

圆由圆心和半径定义。

派生类操作判断任一坐标点是在图形内,还是在图形的边缘上,还是在图形外。

缺省初始化图形退化为点。

要求包括拷贝构造函数。

编程测试类设计是否正确。

cmath>

4.constdoublePI=3.1415926535;

5.classPoint{

6.private:

7.doublex,y;

8.public:

9.Point(){x=0;

y=0;

10.Point(doublexv,doubleyv){x=xv;

y=yv;

11.Point(Point&

pt){x=pt.x;

y=pt.y;

12.doublegetx(){returnx;

13.doublegety(){returny;

14.doubleArea(){return0;

15.voidShow(){cout<

x="

x<

'

'

y="

y<

16.};

17.classCircle:

publicPoint{

18.doubleradius;

19.public:

20.Circle(){radius=0;

21.Circle(doublexv,doubleyv,doublevv):

Point(xv,yv){radius=vv;

22.Circle(Circle&

cc):

Point(cc){radius=cc.radius;

}//拷贝构造函数

23.doubleArea(){returnPI*radius*radius;

24.voidShow(){//注意怎样访问基类的数据成员

25.cout<

getx()<

\t'

gety()<

radius="

radius<

26.}

27.intposition(Point&

pt){

28.doubledistance=sqrt((getx()-pt.getx())*(getx()-pt.getx())

29.+(gety()-pt.gety())*(gety()-pt.gety()));

30.doubles=distance-radius;

31.if(s==0)return0;

//在圆上

32.elseif(s<

0)return-1;

//在圆内

33.elsereturn1;

//在圆外

34.}

35.};

36.classRectangle:

37.doublewidth,length;

38.public:

39.Rectangle(){width=0;

length=0;

40.Rectangle(doublexv,doubleyv,doublewv,doublelv):

Point(xv,xv){

41.width=wv;

42.length=lv;

43.}

44.Rectangle(Rectangle&

rr):

Point(rr){

45.width=rr.width;

46.length=rr.length;

47.}

48.doubleArea(){returnwidth*length;

49.voidShow(){

50.cout<

cout<

width="

width<

length="

length<

51.}

52.intposition(Point&

pt);

53.};

54.intRectangle:

position(Point&

55.doubles1,s2;

56.s1=(pt.getx()-getx());

s2=(pt.gety()-gety());

57.if(((s1==0||s1==width)&

&

s2<

=length)||((s2==0||s2==length)&

s1<

=width))return0;

58.elseif(s1<

width&

length)return-1;

//0在矩形上,-1在矩形内

59.elsereturn1;

//1在矩形外

60.}

61.intmain(){

62.Circlecc1(3,4,5),cc2,cc3(cc1);

63.Rectanglert1(0,0,6,8),rt2,rt3(rt1);

64.Pointp1(0,0),p2(6,8),p3(3,3),p4(8,4),p5(8,8);

65.cc1.Show();

66.cc2.Show();

67.rt1.Show();

68.rt2.Show();

69.cout<

点p1:

70.p1.Show();

71.cout<

矩形rt3:

72.rt3.Show();

73.switch(rt3.position(p1)){

74.case0:

cout<

在矩形上"

break;

75.case-1:

在矩形内"

76.case1:

在矩形外"

77.}

78.cout<

圆cc3:

79.cc3.Show();

80.switch(cc3.position(p1)){

81.case0:

在圆上"

82.case-1:

在圆内"

83.case1:

在圆外"

84.}

85.cout<

点p2:

86.p2.Show();

87.cout<

88.rt3.Show();

89.switch(rt3.position(p2)){

90.case0:

91.case-1:

92.case1:

93.}

94.cout<

95.cc3.Show();

96.switch(cc3.position(p2)){

97.case0:

98.case-1:

99.case1:

100.}

101.cout<

点p3:

102.p3.Show();

103.cout<

104.rt3.Show();

105.switch(rt3.position(p3)){

106.case0:

107.case-1:

108.case1:

109.}

110.cout<

111.cc3.Show();

112.switch(cc3.position(p3)){

113.case0:

114.case-1:

115.case1:

116.}

117.cout<

点p4:

118.p4.Show();

119.cout<

120.rt3.Show();

121.switch(rt3.position(p4)){

122.case0:

123.case-1:

124.case1:

125.}

126.cout<

127.cc3.Show();

128.switch(cc3.position(p4)){

129.case0:

130.case-1:

131.case1:

132.}

133.cout<

点p5:

134.p5.Show();

135.cout<

136.rt3.Show();

137.switch(rt3.positio

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

当前位置:首页 > 考试认证 > 财会金融考试

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

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