ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:18.42KB ,
资源ID:9484868      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9484868.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(c面向对象复习题资料.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

c面向对象复习题资料.docx

1、c面向对象复习题资料1.阅读下面的程序,写出 main 的输出inth(intiCount,constint* ipIndex,int& irObj)iCount %=2;if(*ipIndex =5)irObj += iCount;return*ipIndex + irObj;void main()inti, j =1, k =2, m;for(i =0; i s3.display(); 此时s2.display();此时显示出( 之间是五个空格): 显示出 : s3 = s1;s3.display();此时显示出:0123456789s2 = s12;s2.display();此时显示出:

2、23456789s1.display();此时显示出:0123456789s3 = s2+;s2.display();此时显示出:3456789s3.display();此时显示出:23456789答案:#include using namespace std; classMyString charcpBody81;public:MyString(constchar* p = NULL);MyString(inti);MyString&operator=(constMyString& s)strncpy(cpBody, s.cpBody,80);return*this; MyString&op

3、erator(inti);MyString&operator+(inti)staticMyString s; s = *this;*this= (cpBody0 =0) ? *this: (*this)1; voiddisplay()printf(n, cpBody);MyString:MyString(constchar* p)if(p = NULL)cpBody0 =0;elsestrncpy(cpBody, p,80);MyString:MyString(inti)intj;for(j =0; j i & j 80; j+)cpBodyj = ;cpBodyj =0;MyString&

4、MyString:operator(inti)staticMyString s;intj;s = *this;for(j = i; cpBodyj !=0; j+)s.cpBodyj-i = s.cpBodyj;s.cpBodyj-i =0;returns;returns;3.某公司有两类职员Employee和Manager, Manager是一种特殊的 Employee。每个Employee对象所具有的基本信息为:姓名、年龄、工作年限、部门 号,Manager对象除具有上述基本信息外,还有级别(level)信息。公司中的两类 职员都具有以下两种基本操作:1). printOn() / 输出

5、Employee/Manager对象的个人信息2). retire()/* 判断是否到了退休年龄,是,则从公司中除名。公司规定:Employee类对象的退休年令为 55岁,Manager类对象的退休年龄为 60 岁。 */ 要求:1).定义并实现类Employee和Manager;2).分别输出公司中两类职员的人数(注意: Ma nager亦属于Employee)。答案:#include#includeusingnamespacestd;classEmployee charname21; intworkYear; intdepartmentNum;protected:intage;static

6、intECount;public:Employee(char* s,intage1,intworkYear1,intdepN) if(strlen(s)=18& workYear1=0& depN0) age=age1;workYear=workYear1;departmentNum=depN;Employee:ECount+;virtualvoidprintOn()coutname=name age=age workYear=;coutworkYear departmentNum= departmentNum =55)deletethis;Employee:ECount-;elseretur

7、n;staticvoidcountE()coutEmployee:ECountendl;intEmployee:ECount=0;classManager :publicEmployeeintlevel;staticintMCount;public:Manager(char* s,intage1,intworkYear1,intdepN,intlev):Employee(s,age1,workYear1, depN), level(lev)Manager:MCount+;voidprintOn()Employee:printOn();coutLevel= level=60)deletethis

8、; MCount-; ECount-;elsereturn;staticvoidcountM()coutManager:MCountendl;intManager:MCount=0;/ 测试函数voidmain()Employee e1(Li Yanni,40,21,1), e2(Li Qingsan,30,6,1);Manager m1(Chen Ping,50,30,2,1), m2(Xiu Xuezhou,57,35,2,2);Manager m3(Gong Jieming,64,35,2,1), m4(Wang Li,61,34,2,2);e2.printOn();m1.printOn

9、();Employee:countE();Manager:countM();return;4定义类模板SortedSe,即元素有序的集合,集合元素的类型和集合元素 的最大个数可由使用者确定。要求该类模板对外提供两种操作:(1) insert:加入一个新的元素到合适的位置上,并保证集合元素的值不重复;(2)get:返回比给定值大的最小元素的地址。若不存在,返回 0;(3)del:删除与给定值相等的那个元素,并保持剩余元素的有序性。(假定集合元素类型上已经定义了赋值操作符和所有的比较操作符。)答案templateclassSortedSetT tBodyiSz;/ 集合元素数组intiCurren

10、tElmts;/ 当前集合的有效元素个数 public: boolinsert(T t);T* get(T t);voiddel(T t);SortedSet() : iCurrentElmts(0);template boolSortedSet:insert(T t)inti;if(iCurrentElmts = iSz) returnfalse;/ 满额,无法插入 for(i =0; i iCurrentElmts; i+)if(tBodyi = t)returntrue;/ 元素重复if(tBodyi i; j-)tBodyj = tBodyj-1;/ 元素后移 break;tBodyi

11、 = t;iCurrentElmts+;returntrue; templateT* SortedSet:get(T t)for(inti =0; i iCurrentElmts; i+)if(tBodyi = t)continue;return&(tBodyi);return0;template voidSortedSet:del(T t) for(inti =0; i iCurrentElmts; i+)if(tBodyi != t)continue;for(intj = i; j iCurrentElmts -1; j+)tBodyj = tBodyj+1;/ 元素前移iCurrentE

12、lmts-;break;5定义类模板SortedSe,即元素有序的集合,集合元素的类型和集合元素的最大个数可由使用者确定。要求该类模板对外提供三种操作:(1) insert:加入一个新的元素到合适的位置上,并保证集合元素的值不 重复;(2)get:返回比给定值大的最小元素的地址。若不存在,返回 0。(3)del :删除与给定值相等的那个元素,并保持剩余元素的有序性。答 案:#include#includeusingnamespacestd;/* 类模板中定义了两个函数指针 :fp1 指向对任意两个类型量比较的函数指针 ,返回值的含义为 =0表示二者 =1 表示二者 =-1 以上情况都不是fp2

13、 为指向任意二个类型量赋值的函数指针fp1 和 fp2 中的第一个形参为左操作数 ,第二个形参为右操作数*/templateclassSorte dSetT tBodyiSz;/ 集合元素数组intiCurrentElmts;/ 当前集合的有效元素个数public:boolinsert(T t);T* get(T t);voiddel(T t);SortedSet():iCurrentElmts(0)voidprint();templateboolSorte dSet:insert(T t)inti;if(iCurrentElmts = iSz)coutIts full.endl;return

14、false;/ 满额,无法插入for(i =0; i iCurrentElmts; i+)if(*fp1)(&tBodyi,&t)=0)coutIts repeated. i; j-)(*fp2)(&tBodyj,&tBodyj-1);/ 元素后移break;(*fp2)(&tBodyi,&t);iCurrentElmts+;returntrue; templateT*SortedSet:get(T t)for(inti =0; i iCurrentElmts; i+)if(*fp1)(&tBodyi,&t)=3)continue;return&(tBodyi);return0;templat

15、evoidSorted Set:del(T t)inti;for(i =0; i iCurrentElmts; i+)if(*fp1)(&tBodyi,&t)=1)continue;/ 查找比 t 大的最小的元素 if(i=iCurrentElmts)coutIts not found.endl;return;for(intj = i; j iCurrentElmts -1; j+)(*fp2)(&tBodyj,&tBodyj+1);/ 元素前移iCurrentElmts-;return;templatevoidSorted Set:print()for(inti=0; iiCurrentEl

16、mts; i+) couti ;coutendl;/以下写了 mycomplex类和二个函数comparel及assign1主要用于对类模 板的测试 classmycomplexdoublereal;doubleimage;friendintcompare1(void*,void*);friendvoidassign1(void*,void*);public:mycomplex(floatr=0,floati=0):real(r),image(i);/ .;/*compare1 函数功能如下 :若*p仁二*p2函数返回值为0;若*p1*p2 函数返回值为 2;以上情况都不是者 ,函数返回值为

17、-1.*/intcompare1(void* p1,void* p2)mycomplex *pt1,*pt2;pt1=(mycomplex*)p1;pt2=(mycomplex*)p2;doublet1,t2;t1=sqrt(*pt1).real*(*pt1).real+(*pt1).image*(*pt1).image); t2=sqrt(*pt2).real*(*pt2).real+(*pt2).image*(*pt2).image); if(*pt1).real=(*pt2).real & (*pt1).image=(*pt2).image) return0;if(t1t2)return

18、2;return-1;/assign1 函数功能如下 : *p1=*p2voidassign1(void* p1,void* p2)mycomplex *pt1,*pt2;pt1=(mycomplex*)p1;pt2=(mycomplex*)p2;(*pt1).real=(*pt2).real;(*pt1).image=(*pt2).image;/ 测试函数intmain()mycomplex *p1;mycomplex x1(1.0,2.0), x2(3.0,4.0), x3(5.0,6.0), x4(7.0,8.0), x5;SortedSet ss;ss.insert(x1);ss.insert(x2);ss.insert(x2);ss.insert(x3);ss.del(x4);ss.insert(x4);p1=ss.get(x5);ss.del(x3);ss.print();return0;

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

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