中大实践考核面向对象C程序设计试题和答案上机考试.pdf

上传人:s****u 文档编号:12792460 上传时间:2020-05-24 格式:PDF 页数:8 大小:174.82KB
返回 下载 相关 举报
中大实践考核面向对象C程序设计试题和答案上机考试.pdf_第1页
第1页 / 共8页
中大实践考核面向对象C程序设计试题和答案上机考试.pdf_第2页
第2页 / 共8页
中大实践考核面向对象C程序设计试题和答案上机考试.pdf_第3页
第3页 / 共8页
点击查看更多>>
资源描述
1 面 向 对 象 (C+)程 序 设 计 (上 机 考 试 )样 题 . 下 列 Shape类 是 一 个 表 示 形 状 的 抽 象 类 ,Area()为 求 图 形 面 积 的 函 数 , Total()则 是 一 个 通 用的 用 以 求 不 同 形 状 的 图 形 面 积 总 和 函 数 。 请 从Shape 类 派 生 三 角 形 类 (triangle) 、 矩 形 类(rectangle),并 给 出 具 体 的 求 面 积 函 数 。 编 写 程 序 验证 求 面 积 函 数 的 正 确 性 。 Shape、 total 的 定 义 如下 所 示 。ClassshapePubilc:Virtualfloatarea()=0; floattotal(shape*s,intn)floatsum=0.0;for(inti=0;iarea();returnsum;解 答 : #includeclassshapepublic:virtualfloatarea()=0;floattotal(shape*s,intn) floatsum=0;for(inti=0;iarea();returnsum;classtriangle:publicshapeprotected:floatH,W;public:triangle(float h, float w) H=h;W=w; floatarea()returnH*W*0.5; ;classrectangle:publictrianglepublic:rectangle(float h, float w) :triangle(h,w)floatarea()returnH*W; voidmain() shape*s4;s0=newtriangle(3.0,4.0);s1=newrectangle(2.0,4.0);s2=newtriangle(5.0,8.0);s3=newrectangle(6.0,8.0);floatsum=total(s,4);cout The total area is: sum endl; 样 题 . 以 面 向 对 象 的 概 念 设 计 一 个 类 , 此 类 包 括 个 私 有 数 据 , unlead(无 铅 汽 油 ), lead 有 铅 汽 油 ,total(当 天 总 收 入 )。 其 中 , 无铅 汽 油 价 格 是 17/升 , 有 铅 汽 油 价 格 是16/升 , 请 以 构 造 函 数 的 方 式 建 立 此 值 ,并 编 写 程 序 , 该 程 序 能 够 根 据 加 油 量 ,自 动 计 算 出 油 站 当 天 的 总 收 入 。解 答 : #includeclassincomeprivate:floatunlead,lead,total;public:income(float ul, float l)unlead=ul;lead=l;total=0.0;floatcalculate(float,float); ;float income : calculate(floatunleadcontent,floatleadcontent) total = unlead*unleadcontent +lead*leadcontent;returntotal;voidmain() floatunleadcontent,leadcontent,total;incomeaccount(17,16);cout Please input unlead content: unleadcontent;cout Please input lead content: leadcontent;total = account.calculate(unleadcontent,leadcontent); 2 cout The total income is: totalendl;样 题 . 编 写 一 个 计 算 两 个 给 定 长 方 形 的 面 积 的程 序 , 要 求 长 方 形 用 一 个 类 (Rectangle)来 表 示 , 在 该 类 中 增 加 定 义 一 个 成 员 函数 add_area(), 该 成 员 函 数 使 用 对 象 作为 参 数 , 用 来 计 算 两 个 给 定 长 方 形 的 面积 。解 答 : #includeclassRectangleprivate:floatH,W; public:Rectangle(float h, float w)H=h;W=w;floatarea()returnH*W;floatadd_area(Rectangle;float Rectangle : add_area(Rectanglevoidmain() floath1,w1,h2,w2,totalarea;cout Please input the 1st rectangleHRectanglerec1(h1,w1);cout Please input the 2st rectangleHRectanglerec2(h2,w2);totalarea=rec1.add_area(rec2);cout The total area of the tworectangleis:totalarea0) if(imag=1)coutreal+i;else cout real + imagi;elseif(imag0) if(imag=-1)coutreal-i;else cout real imag i; else coutreal; 3 voidmain() complexa(4,5),b(2,3),x,y;x=a+b;y=a-b;a.show();cout+;b.show();cout=;x.show();coutendl;a.show();cout-; b.show();cout=;y.show();coutendl;样 题 5. 定 义 一 个 平 面 几 何 中 点 的 位 置 类POSITION, 它 应 该 包 含 有 移 动 、 计 算 两点 间 的 距 离 (包 括 到 原 点 的 距 离 ), 求 X坐 标 、 Y 坐 标 等 操 作 , 其 中 计 算 两 点 间的 距 离 以 友 元 函 数 形 式 实 现 。 编 写 程 序验 证 其 功 能 。解 答 : #include#include classPositionprivate:floatX,Y;public:Position(float xi, float yi) X=xi,Y=yi;void Move(float xo, floatyo)X+=xo,Y+=yo;floatGetX()returnX;floatGetY()returnY;floatdistanceToOrigin();friend float distance(Position ;floatPosition:distanceToOrigin() returnsqrt(X*X+Y*Y);floatdistance(Positionfloatdy=a.Y-b.Y;returnsqrt(dx*dx+dy*dy);voidmain() Positionp1(1.5,3.5),p2(4.5,6.5);p1.Move(3.5,5.5);floatdis0=p1.distanceToOrigin();floatdis=distance(p1,p2);cout The distance p1( p1.GetX(),p1.GetY() ) to origin is: dis0 endl;cout The distance between p1( p1.GetX(),p1.GetY() ) and p2( p2.GetX() ,p2.GetY()isdisendl;样 题 6. 利 用 类 和 对 象 , 编 制 出 一 个 卖 瓜 的 程 序 。每 卖 一 个 瓜 要 计 出 该 瓜 的 重 量 , 还 要 计算 所 卖 出 瓜 的 总 重 量 及 总 个 数 , 同 时 卖瓜 时 还 允 许 退 瓜 。 (提 示 :将 每 个 瓜 设 为对 象 ;用 静 态 成 员 变 量 分 别 统 计 卖 出 瓜的 总 重 量 和 总 个 数 ; 卖 瓜 行 为 用 构 造 函 数 模 拟 , 退 瓜 行 为 用 析 构 函 数 模 拟 。 )解 答 : #includeclassWatermelonprivate:staticintn;staticfloattotalWeight;floatweight;public:Watermelon(floatw) n+;weight=w;totalWeight+=w; Watermelon(Watermelonweight=wa.weight;totalWeight+=weight; 4 Watermelon() n-;totalWeight-=weight;intgetWeight() returnweight;staticintgetNum() returnn; staticintgetTotal() returntotalWeight;intWatermelon:n=0;floatWatermelon:totalWeight=0;voidmain() floatw;cout The initial weight ofwatermelon: Watermelon : getTotal() endl;cout Please input weight ofwatermelon:w;Watermelonwa1(w);cout Please input weight ofwatermelon:w;Watermelonwa2(w);cout Please input weight ofwatermelon:w;Watermelonwa3(w); cout Watermelon : getNum() Nos.watermelonsweresold.endl;cout The total weight is: Watermelon:getTotal()endl;wa3.Watermelon:Watermelon();cout Now one watermelon waswithdrawed!endl; cout Watermelon : getNum() Nos.watermelonsweresold.endl;cout The total weight is: Watermelon:getTotal()endl;样 题 6. 编 写 一 个 程 序 , 用 于 计 算 三 角 形 、 矩 形和 圆 的 总 面 积 。 (提 示 : 由 于 尚 不 能 确 定该 程 序 计 算 的 具 体 形 状 , 可 以 先 定 义 一个 抽 象 的 类 shape, 对 于 具 体 种 类 的 形状 , 通 过 从 shape 派 生 一 个 类 来 对 其 进行 描 述 。 )解 答 : #includeclassshape public:virtualfloatarea()=0;floattotal(shape*s,intn) floatsum=0;for(inti=0;iarea();returnsum;classtriangle:publicshapeprotected:floatH,W; public:triangle(float h, float w) H=h;W=w;floatarea()returnH*W*0.5;classrectangle:publictrianglepublic:rectangle(floath,floatw):triangle(h,w)floatarea()returnH*W;classcircle:publicshapeprotected: floatradius;public:circle(floatr)radius=r;float area() returnradius*radius*3.14; 5 voidmain() shape*s4;s0=newtriangle(3.0,4.0);s1=newrectangle(2.0,4.0);s2=newcircle(5.0);s3=newcircle(8.0);floatsum=total(s,4);cout The total area is: sum endl;题 样 7. 编 写 一 个 程 序 , 可 以 输 入 个 学 生 的 总分 , 并 按 总 分 从 高 到 低 排 序 , 要 求 设 计 一 个 学 生 类 STUDENT, 并 编 写其 所 有 成 员 函 数 的 代 码 , 类 STUDENT的 定 义 如 下 :classSTUDENTint total;/ 总 分 成 绩 pubic:void get_score();/获 取 一 个 学 生 的 成 绩 void display();/显 示 一 个 学 生 的 成 绩 void sort (STUDENT *);/将 若 干 学 生 成 绩 按 总 分 从 高 到 低 排 序 解 答 : #include#includeclassStudentinttotal;public:Student(intm)total=m;intget_score();voiddisplay();voidsort(Student*s3);intStudent:get_score() returntotal;voidStudent:display() coutsetw(6)totalget_score() sj-1-get_score()t=sj;sj=sj-1;sj-1=t; coutThesortedscoreis:;for(intk=0;k3;k+)cout setw(6) get_score();coutendl;voidmain() ints0,s1,s2;cout Please input 3 score: s0s1s2;Student*s3;s0=newStudent(s0);s1=newStudent(s1);s2=newStudent(s2);s0-sort(s);样 题 8. 设 计 一 个 栈 操 作 类 , 该 类 包 含 入 和 出 栈成 员 函 数 , 编 写 程 序 , 入 栈 一 组 数 据 :(5,2,6,7,3), 然 后 屏 幕 显 示 出 栈 结 果 。解 答 : #include#includeconstintsize=20; classstackprivate:intdatasize;inttop;public:stack()top=-1;voidpush(int); 6 intpop();voidstack:push(intc) if(top=19)coutstackoverflow!endl;elsedata+top=c;intstack:pop() if(top=-1) coutstackunderflow!endl;returnNULL;elsereturndatatop-;voidmain() stacks;s.push(5);s.push(2);s.push(6); s.push(7);s.push(3);for(inti=0;i5;i+)coutsetw(6)s.pop();coutendl;样 题 9. 编 写 一 个 输 入 、 显 示 学 生 (用 类 student表 示 )和 教 师 (用 类 teacher表 示 )数 据 的 程序 , 学 生 的 数 据 包 括 : 编 号 (NO)、 姓 名(NAME)和 班 号 (CLASS), 教 师 的 数 据包 括 : 编 号 (NO)、 姓 名 (NAME)和 部 门(DEPT)。 要 求 将 编 号 、 姓 名 的 输 入 和 显示 两 项 操 作 设 计 成 一 个 类 person, 并 将 它 作 为 student类 和 teacher类 的 基 类 。解 答 : #include#includeclasspersonprotected:charNo5; charName10;public:virtualvoidinput();virtualvoidshow();classstudent:publicpersonprivate:charClass3;public:virtualvoidinput();virtualvoidshow();classteacher:publicperson private:charDept3;public:virtualvoidinput();virtualvoidshow();voidperson:input() coutPleaseinputNO.:No;cout Please input NAME: Name;voidperson:show() cout.setf(ios:left);cout.width(8);coutNO.:Noendl;cout.width(8);coutNAME:Nameendl; voidstudent:input() person:input();cout Please input CLASS: Class; 7 voidstudent:show() person:show();cout.setf(ios:left);cout.width(8);coutCLASS:Classendl;voidteacher:input() person:input();coutPleaseinputDEPT:Dept; voidteacher:show() person:show();cout.setf(ios:left);cout.width(8);coutDEPT:Deptendl;voidmain() studentstu1,stu2; teachertea1;cout Please input 1st studentsinformation:endl;stu1.input();cout Please input 2st studentsinformation:endl;stu2.input();cout Please input teachersinformation:endl;tea1.input();coutendlSTUDENT:endl;stu1.show();stu2.show(); cout endl TEACHER: endl;tea1.show();样 题 10. 编 写 一 个 可 以 删 除 文 本 文 件 中 所 有 以“ /” 开 头 字 符 串 的 C+程 序 。 要 求 必 须 使 用 C+的 I/O流 成 员 函 数 来 完 成 。解 答 : #include#includevoidmain(intargc,char*argv) charch;if(argc!=3)cout Error, You shall use thisprogramsas:nt purgefile filename1 filename2endl; return; ifstreammyin(argv1);if(!myin)cout Cant openfile argv1endl; return;ofstreammyout(argv2);if(!myout)coutCant createfileargv2endl; return; while(myoutif(ch=/)do while (myin.get(ch) elsemyout.put(/);myout.put(ch);myin.get(ch); domyout.put(ch); while (myin.get(ch) if(ch=)|(ch=n)myout.put(ch); 8 elseif(ch=)|(ch=n)myout.put(ch);elsemyout.put(ch);myin.get(ch);domyout.put(ch); while (myin.get(ch) if(ch=)|(ch=n)myout.put(ch);myin.close();myout.close();coutpurgecompleted.endl;样 题 11. 己 知 类 test 含 有 整 型 私 有 数 据 成 员num, 编 写 主 程 序 , 要 求 :1) 写 出 test的 完 整 定 义 , 并 含 有 必 要的 函 数 成 员 ; 2) 建 立 长 度 为 9、 元 素 类 型 为 Test的动 态 数 组 且 初 值 为 0;3) 将 各 元 素 的 值 均 设 置 为 0 x7FF;4) 显 示 各 元 素 的 值 ;5) 删 除 动 态 数 组 ;解 答 : #include#includeclasstestprivate:intnum;public:test()num=0; test(intn)num=n;voidSetNum(intn)num=n;intGetNum()returnnum;voidmain() test*ptr=newtest9;inti;for(i=0;i9;i+)ptri.SetNum(0 x7FF);for(i=0;i9;i+)cout setw(8) hex ptri.GetNum();coutendl;deleteptr;
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!