C++题目x答案Word格式.docx
- 文档编号:14967917
- 上传时间:2022-10-26
- 格式:DOCX
- 页数:14
- 大小:20.65KB
C++题目x答案Word格式.docx
《C++题目x答案Word格式.docx》由会员分享,可在线阅读,更多相关《C++题目x答案Word格式.docx(14页珍藏版)》请在冰豆网上搜索。
2编写一个程序。
用名为max的函数模板计算三个参数中的最大值,用整数、字符和浮点数测试所编写的程序。
20、#include<
21、usingnamespacestd;
22、
23、template<
typenameO>
24、OMax(Oa,Ob,Oc){returna>
ba>
ca:
c:
b>
cb:
c;
25、
26、voidmain()
27、{
28、cout<
Max(9,1,8)<
endl;
29、cout<
Max,,<
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表面积:
\n体积:
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*;
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面积:
endl
34.<
\n三角:
高:
10,底:
5\n面积:
35.<
\n正方形:
边长:
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<
年"
月"
日\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.(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.("
小时工A"
777);
41.();
42.Mangerm(999);
43.("
经理A"
888);
44.();
45.}
1.
8、回答下面问题。
下面列出了由三个文件、和组成的一个程序。
文件中实现了主函数;
文件中定义了类MyClass;
文件中实现了类MyClass的成员函数。
题中没有给出三个文件的完整实现。
仔细阅读所给的程序,根据题意补充文件和文件的内容。
a.将成员变量定义为私有的;
b.不要增加题中没有用到的类成员。
1./********************************************************/
2.//文件
3.#include<
>
4.#include"
5.voidmain()
6.{
7. MyClassobj;
8. cout<
()<
endl;
9. ;
10. cout<
11.}
12./***********************************************************************/
13./文件
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.//文件
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类的属性包括姓名、职工号、工资级别,月薪(实发基本工资加业绩工
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 题目 答案