c面向对象复习题资料.docx
- 文档编号:9484868
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:15
- 大小:18.42KB
c面向对象复习题资料.docx
《c面向对象复习题资料.docx》由会员分享,可在线阅读,更多相关《c面向对象复习题资料.docx(15页珍藏版)》请在冰豆网上搜索。
c面向对象复习题资料
1.阅读下面的程序,写出main的输出
inth(intiCount,constint*ipIndex,int&irObj)
{
iCount%=2;
if(*ipIndex>=5)
irObj+=iCount;
return*ipIndex+irObj;
}
voidmain()
{
inti,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=
0,k=
5,
m=
15.
,使之能符合下面
2•用C++语言定义MyString(包括成员函数的实现代码)
程序及在注释中描述的运行结果的要求:
voidmain()
{
MyStrings1="0123456789",s2(5),s3;
s1.display();〃此时显示出:
<0123456789〉
>s3.display();〃此时
s2.display();〃此时显示出(<>之间是五个空格):
<显示出:
<>
s3=s1;
s3.display();〃此时显示出:
<0123456789〉
s2=s1[2];
s2.display();〃此时显示出:
<23456789>
s1.display();〃此时显示出:
<0123456789〉
s3=s2++;
s2.display();〃此时显示出:
<3456789>
s3.display();〃此时显示出:
<23456789>
}
答案:
#include
{charcpBody[81];
public:
MyString(constchar*p=NULL);
MyString(inti);
MyString&operator=(constMyString&s)
{
strncpy(cpBody,s.cpBody,80);return*this;}
MyString&operator[](inti);
MyString&operator++(inti)
{
staticMyStrings;s=*this;
*this=(cpBody[0]=='\0')?
*this:
(*this)[1];}
voiddisplay()
printf("<%s>\n",cpBody);
}
};
MyString:
:
MyString(constchar*p)
{
if(p==NULL)
cpBody[0]='\0';
else
strncpy(cpBody,p,80);
}
MyString:
:
MyString(inti)
{
intj;
for(j=0;j
cpBody[j]='';
cpBody[j]='\0';
}
MyString&MyString:
:
operator[](inti)
{
staticMyStrings;
intj;
s=*this;
for(j=i;cpBody[j]!
='\0';j++)
s.cpBody[j-i]=s.cpBody[j];
s.cpBody[j-i]='\0';
returns;
}returns;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
usingnamespacestd;
classEmployeecharname[21];intworkYear;intdepartmentNum;
protected:
intage;
staticintECount;
public:
Employee(char*s,intage1,intworkYear1,intdepN){
if(strlen(s)<=20&&*s!
='\0')strcpy(name,s);if(age1>=18&&workYear1>=0&&depN>0){
age=age1;
workYear=workYear1;
departmentNum=depN;
Employee:
:
ECount++;
}
}
virtualvoidprintOn()
{
cout<<"name="< virtualvoidretire(Employee&e) { if(e.age>=55) { deletethis; Employee: : ECount--; } elsereturn; } staticvoidcountE() { cout< : ECount< } }; intEmployee: : ECount=0; classManager: publicEmployee { intlevel; staticintMCount; public: Manager(char*s,intage1,intworkYear1,intdepN,intlev) : Employee(s,age1,workYear1,depN),level(lev) { Manager: : MCount++; } voidprintOn() { Employee: : printOn();cout<<"Level="< } voidretire(Manager&m) { if(m.age>=60) {deletethis;MCount--;ECount--;} elsereturn; } staticvoidcountM() { cout< : MCount< } }; intManager: : MCount=0; //测试函数 voidmain() { Employeee1("LiYanni",40,21,1),e2("LiQingsan",30,6,1);Managerm1("ChenPing",50,30,2,1),m2("XiuXuezhou",57,35,2,2);Managerm3("GongJieming",64,35,2,1),m4("WangLi",61,34,2,2);e2.printOn(); m1.printOn(); Employee: : countE(); Manager: : countM(); return; } 4•定义类模板SortedSe,即元素有序的集合,集合元素的类型和集合元素的最大个数可由使用者确定。 要求该类模板对外提供两种操作: (1)insert: 加入一个新的元素到合适的位置上,并保证集合元素的值不 重复; (2)get: 返回比给定值大的最小元素的地址。 若不存在,返回0; (3)del: 删除与给定值相等的那个元素,并保持剩余元素的有序性。 (假定集合元素类型上已经定义了赋值操作符和所有的比较操作符。 ) 答案 template classSortedSet { TtBody[iSz];//集合元素数组 intiCurrentElmts;//当前集合的有效元素个数public: boolinsert(Tt); T*get(Tt); voiddel(Tt); SortedSet(): iCurrentElmts(0) {} }; template : insert(Tt) { inti; if(iCurrentElmts==iSz)returnfalse;//满额,无法插入for(i=0;i { if(tBody[i]==t) returntrue;//元素重复 if(tBody[i] continue; for(intj=iCurrentElmts;j>i;j--) tBody[j]=tBody[j-1];//元素后移break; } tBody[i]=t; iCurrentElmts++; returntrue; }template T*SortedSet : get(Tt) { for(inti=0;i { if(tBody[i]<=t) continue; return&(tBody[i]); } return0; } template : del(Tt){ for(inti=0;i { if(tBody[i]! =t) continue; for(intj=i;j tBody[j]=tBody[j+1];//元素前移 iCurrentElmts--; break; } } 5•定义类模板SortedSe,即元素有序的集合,集合元素的类型和集合元素 的最大个数可由使用者确定。 要求该类模板对外提供三种操作: (1)insert: 加入一个新的元素到合适的位置上,并保证集合元素的值不重复; (2)get: 返回比给定值大的最小元素的地址。 若不存在,返回0。 (3)del: 删除与给定值相等的那个元素,并保持剩余元素的有序性。 答案: #include #include usingnamespacestd; /*类模板中定义了两个函数指针: fp1指向对任意两个类型量比较的函数指针,返回值的含义为=0表示二者"=="=1表示二者"<" =2表示二者">" =-1以上情况都不是 fp2为指向任意二个类型量赋值的函数指针 fp1和fp2中的第一个形参为左操作数,第二个形参为右操作数 */ template { TtBody[iSz];//集合元素数组 intiCurrentElmts;//当前集合的有效元素个数 public: boolinsert(Tt); T*get(Tt); voiddel(Tt); SortedSet(): iCurrentElmts(0) {} voidprint(); }; template : insert(Tt) { inti; if(iCurrentElmts==iSz) { cout<<"It'sfull."< }//满额,无法插入 for(i=0;i { if((*fp1)(&tBody[i],&t)==0) { cout<<"It'srepeated."< }//元素重复 if((*fp1)(&tBody[i],&t)==2) continue; for(intj=iCurrentElmts;j>i;j--) (*fp2)(&tBody[j],&tBody[j-1]);//元素后移 break; } (*fp2)(&tBody[i],&t); iCurrentElmts++; returntrue; }template SortedSet : get(Tt) { for(inti=0;i { if((*fp1)(&tBody[i],&t)==3) continue; return&(tBody[i]); } return0; } template : del(Tt) { inti; for(i=0;i if((*fp1)(&tBody[i],&t)==1)continue;//查找比t大的最小的元素if(i==iCurrentElmts) { cout<<"It'snotfound."< } for(intj=i;j (*fp2)(&tBody[j],&tBody[j+1]);//元素前移 iCurrentElmts--; return; } template : print() { for(inti=0;i cout< } //以下写了mycomplex类和二个函数comparel及assign1主要用于对类模板的测试classmycomplex { doublereal; doubleimage; friendintcompare1(void*,void*); friendvoidassign1(void*,void*); public: mycomplex(floatr=0,floati=0): real(r),image(i){}; //... }; /*compare1函数功能如下: 若*p仁二*p2函数返回值为0; 若*p1<*p2函数返回值为1; 若*p1>*p2函数返回值为2; 以上情况都不是者,函数返回值为-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(t1 if(t1>t2)return2; return-1; } //assign1函数功能如下: *p1=*p2 voidassign1(void*p1,void*p2) { mycomplex*pt1,*pt2; pt1=(mycomplex*)p1; pt2=(mycomplex*)p2; (*pt1).real=(*pt2).real; (*pt1).image=(*pt2).image; } //测试函数 intmain() { mycomplex*p1; mycomplexx1(1.0,2.0),x2(3.0,4.0),x3(5.0,6.0),x4(7.0,8.0),x5;SortedSet 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; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 复习题 资料