C++面向对象复习题.doc_第1页
C++面向对象复习题.doc_第2页
C++面向对象复习题.doc_第3页
C++面向对象复习题.doc_第4页
C++面向对象复习题.doc_第5页
免费预览已结束,剩余8页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1. 阅读下面的程序,写出main的输出。int h(int iCount, const int* ipIndex, int& irObj)iCount %= 2;if (*ipIndex = 5)irObj += iCount;return *ipIndex + irObj;void main()int i, j = 1, k = 2, m;for (i = 0; i 10; i+, j+) m = h(i, &j, k);printf(i = %2d, j = %2d, k = %2d, m = %2d.n, i, j, k, m);答案:i = 0, j = 1, k = 2, m = 3. i = 1, j = 2, k = 2, m = 4. i = 2, j = 3, k = 2, m = 5. i = 3, j = 4, k = 2, m = 6. i = 4, j = 5, k = 2, m = 7. i = 5, j = 6, k = 3, m = 9. i = 6, j = 7, k = 3, m = 10. i = 7, j = 8, k = 4, m = 12. i = 8, j = 9, k = 4, m = 13.i = 9, j = 10, k = 5, m = 15.2. 用C+语言定义MyString(包括成员函数的实现代码),使之能符合下面程序及在注释中描述的运行结果的要求:void main()MyString s1 = 0123456789, s2(5), s3;s1.display(); / 此时显示出: s2.display(); / 此时显示出(之间是五个空格): s3.display(); / 此时显示出: s3 = s1;s3.display(); / 此时显示出: s2 = s12;s2.display(); / 此时显示出: s1.display(); / 此时显示出: s3 = s2+;s2.display(); / 此时显示出: s3.display(); / 此时显示出: 答案:#include using namespace std; class MyStringchar cpBody81;public:MyString(const char* p = NULL);MyString(int i);MyString& operator=(const MyString& s) strncpy(cpBody, s.cpBody, 80); return *this; MyString& operator(int i);MyString& operator+(int i) static MyString s; s = *this; *this = (cpBody0 = 0) ? *this : (*this)1; return s; void display() printf(n, cpBody); ;MyString:MyString(const char* p)if (p = NULL)cpBody0 = 0;elsestrncpy(cpBody, p, 80);MyString:MyString(int i) int j;for (j = 0; j i & j 80; j+)cpBodyj = ;cpBodyj = 0;MyString& MyString:operator(int i) static MyString s;int j;s = *this;for (j = i; cpBodyj != 0; j+)s.cpBodyj-i = s.cpBodyj;s.cpBodyj-i = 0;return s;3. 某公司有两类职员Employee和Manager,Manager是一种特殊的Employee。每个Employee对象所具有的基本信息为:姓名、年龄、工作年限、部门号,Manager对象除具有上述基本信息外,还有级别(level)信息。公司中的两类职员都具有以下两种基本操作:1). printOn() / 输出Employee/Manager对象的个人信息2). retire() /* 判断是否到了退休年龄,是,则从公司中除名。公司规定:Employee类对象的退休年令为55岁,Manager类对象的退休年龄为60岁。*/ 要求: 1). 定义并实现类Employee和Manager; 2). 分别输出公司中两类职员的人数(注意:Manager亦属于Employee)。答案:#include #include using namespace std;class Employeechar name21;int workYear;int departmentNum;protected:int age;static int ECount;public:Employee(char* s, int age1, int workYear1, int depN) if(strlen(s)=18 & workYear1=0 & depN0) age=age1;workYear=workYear1;departmentNum=depN;Employee:ECount+;virtual void printOn()cout name= name age= age workYear= ;coutworkYear departmentNum= departmentNum =55)delete this; Employee:ECount-;else return;static void countE()coutEmployee:ECountendl;int Employee:ECount=0;class Manager :public Employeeint level;static int MCount;public:Manager(char* s, int age1, int workYear1, int depN, int lev):Employee(s,age1,workYear1, depN), level(lev) Manager:MCount+;void printOn()Employee:printOn();coutLevel= level=60) delete this; MCount-; ECount-;else return;static void countM()coutManager:MCountendl;int Manager:MCount=0;/测试函数void main()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();Employee:countE();Manager:countM();return;4. 定义类模板SortedSet,即元素有序的集合,集合元素的类型和集合元素的最大个数可由使用者确定。要求该类模板对外提供两种操作:(1) insert:加入一个新的元素到合适的位置上,并保证集合元素的值不重复;(2) get: 返回比给定值大的最小元素的地址。若不存在,返回0;(3) del: 删除与给定值相等的那个元素,并保持剩余元素的有序性。(假定集合元素类型上已经定义了赋值操作符和所有的比较操作符。)答案template class SortedSet T tBodyiSz; / 集合元素数组int iCurrentElmts;/ 当前集合的有效元素个数public:bool insert(T t);T* get(T t);void del(T t);SortedSet() : iCurrentElmts(0);template bool SortedSet:insert(T t)int i;if (iCurrentElmts = iSz)return false;/ 满额,无法插入for (i = 0; i iCurrentElmts; i+) if (tBodyi = t)return true;/ 元素重复if (tBodyi i; j-)tBodyj = tBodyj-1;/ 元素后移break;tBodyi = t;iCurrentElmts+;return true; template T* SortedSet:get(T t)for (int i = 0; i iCurrentElmts; i+)if (tBodyi = t)continue;return &(tBodyi);return 0; template void SortedSet:del(T t)for (int i = 0; i iCurrentElmts; i+) if (tBodyi != t)continue;for (int j = i; j iCurrentElmts - 1; j+)tBodyj = tBodyj+1;/ 元素前移iCurrentElmts-;break; 5. 定义类模板SortedSet,即元素有序的集合,集合元素的类型和集合元素的最大个数可由使用者确定。要求该类模板对外提供三种操作:(1)insert:加入一个新的元素到合适的位置上,并保证集合元素的值不重复;(2)get:返回比给定值大的最小元素的地址。若不存在,返回0。(3)del: 删除与给定值相等的那个元素,并保持剩余元素的有序性。答案:#include #include using namespace std;/*类模板中定义了两个函数指针: fp1指向对任意两个类型量比较的函数指针, 返回值的含义为 =0 表示二者=1 表示二者=-1 以上情况都不是fp2为指向任意二个类型量赋值的函数指针fp1和fp2中的第一个形参为左操作数,第二个形参为右操作数*/template class SortedSet T tBodyiSz; / 集合元素数组int iCurrentElmts;/ 当前集合的有效元素个数public:bool insert(T t);T* get(T t);void del(T t);SortedSet():iCurrentElmts(0)void print();template bool SortedSet:insert(T t) int i;if (iCurrentElmts = iSz)coutIts full.endl; return 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+;return true; template T* SortedSet:get(T t)for (int i = 0; i iCurrentElmts; i+) if (*fp1)(&tBodyi,&t)=3)continue;return &(tBodyi);return 0; template void SortedSet:del(T t)int i;for (i = 0; i iCurrentElmts; i+) if (*fp1)(&tBodyi,&t)=1) continue; /查找比t大的最小的元素if(i=iCurrentElmts) coutIts not found.endl; return;for (int j = i; j iCurrentElmts - 1; j+)(*fp2)(&tBodyj,&tBodyj+1);/ 元素前移iCurrentElmts-;return; template void SortedSet:print()for(int i=0; iiCurrentElmts; i+) couti ; coutendl;/以下写了mycomplex类和二个函数compare1及assign1,主要用于对类模板的测试class mycomplexdouble real;double image;friend int compare1(void*, void*);friend void assign1(void*, void*);public:mycomplex(float r=0, float i=0):real(r),image(i);/ .;/*compare1函数功能如下:若*p1=*p2 函数返回值为0; 若*p1*p2 函数返回值为2;以上情况都不是者,函数返回值为-1.*/int compare1(void* p1, void* p2)mycomplex *pt1,*pt2;pt1=(mycomplex*)p1;pt2=(mycomplex*)p2;double t1,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论