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