C++程序设计习题精编版Word文档下载推荐.docx
《C++程序设计习题精编版Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C++程序设计习题精编版Word文档下载推荐.docx(36页珍藏版)》请在冰豆网上搜索。
}//类的结束
intmain()//测试函数
Testx;
x.initx(30,20);
x.printx();
return0;
得到的测试结果:
4.定义并实现一个矩形类Crectangle。
该类包含了下列成员函数。
Crectangle():
累的构造函数,根据需要可以定义多个构造函数
SetTop(),SetLeft():
设置矩形的左上角坐标
SetLength(),SetWidth():
设置矩形的长和宽
Perimeter():
求矩形的周长
Area():
求矩形的面积
GetWidth():
返回矩形的宽度
Getlength():
返回矩形的长度
IsSquare():
判断矩形是否为正方形
Move():
将矩形从一个位置移动到另一个位置
Size():
改变矩形的大小
Where():
返回矩形的左上角的坐标
PrintRectangle():
输出矩形的四个顶点的坐标
数据成员
inttop,left;
intlength,width;
classCrectangle//类的开始
inttop,left;
intlength,width;
Crectangle(intt=0,intl=0,intlen=1,intw=1)
{
top=t;
left=l;
if(len>
0)length=len;
elselength=0;
if(w>
0)width=w;
elsewidth=0;
}
voidSetTop(intt)
voidSetLeft(intl)
left=l;
voidSetLength(intlen)
length=len;
voidSetWidth(intw)
width=w;
intPerimeter()
return2*(width+length);
intArea()
returnwidth*length;
intGetWidth()
returnwidth;
intGetLength()
returnlength;
intIsSquare()
if(width==length)
{
cout<
该矩形是正方形"
return1;
}
else
cout<
该矩形不是正方形"
return0;
voidMove(intx,inty)
left+=x;
top+=y;
voidSize(intl,intw)
length=l;
width=w;
voidPrintRectangle()
左上方的点为:
("
left<
"
top<
)"
右上方的点为:
left+length<
左下方的点为:
top+width<
右下方的点为:
//类的结束
intmain()
Crectanglea(1,1,5,5);
a.PrintRectangle();
a.SetTop
(2);
a.SetLeft(3);
a.SetLength(4);
a.SetWidth(5);
a.IsSquare();
周长为:
a.Perimeter()<
面积为:
a.Area()<
用getwidth等函数得到的值为:
长为:
a.GetLength<
宽为:
a.GetWidth<
6.某次歌手大赛中,有JudgeNum个评委给选手打分,参加比赛的选手有PlayerNum
名,现为比赛积分编写一个CompetitionResult类,类的定义如下:
(定义略)
(1)写出所有的成员函数和实现代码。
(2)编写main()函数对该类进行测试。
在函数体中,定义了一个competitionResult类的对象数组r[PlauerNum],他的每个元素记录了每个选手的所有信息,个评委的打分通过键盘的输入,在屏幕上应有提示信息进行交互式操作,比赛结果按选手得分从高到底排列输出。
(1)CompetitionResult:
CompetitionResult()
num=0;
strcpy(name,"
);
for(inti=0;
i<
JudgeNum;
i++)
score[i]=0.0;
average=0;
CompetitionResult:
CompetitionResult(shortn,char*ps)
num=n;
strcpy(name,ps);
floatCompetitionResult:
MaxScore()
{intmax=score[0];
if(max<
score[i])max=score[i];
returnmax;
MinScore()
{intmin=score[0];
if(min>
score[i])min=score[i];
returnmin;
voidCompetitionResult:
SetAvg()
{inti;
floatsum=0.0;
for(i=0;
sum+=score[i];
sum=sum-MaxScore()-MinScore();
average=sum/(JudgeNum-2);
GetAvg()
{returnaverage;
shortCompetitionResult:
GetNo(inti){returnnum;
SetNo(inti){num=i;
charCompetitionResult:
*GetName(){returnname;
GetScore(intj){returnscore[j];
Setscore(intk,floatav){score[k]=av;
voidSort(CompetitionResult*pr,intn)
inti,j,k;
CompetitionResulttemp;
n-1;
{
k=i;
for(j=i+1;
j<
n;
j++)
if((pr[j].average)>
(pr[k].average))
k=j;
if(k!
=j)
{
temp=pr[i];
pr[i]=pr[k];
pr[k]=temp;
}
(2)
CompetitionResultplayer[PlayerNum]={CompetitionResult(1,"
one"
),
CompetitionResult(2,"
two"
),CompetitionResult(3,"
there"
CompetitionResult(4,"
four"
),CompetitionResult(5,"
five"
CompetitionResult(6,"
six"
),CompetitionResult(7,"
seven"
CompetitionResult(8,"
eight"
),CompetitionResult(9,"
nine"
CompetitionResult(10,"
ten"
)};
floata;
PlayerNum;
请评委给第"
i+1<
个选手打分:
for(intj=0;
{cin>
>
a;
player[i].Setscore(j,a);
player[i].SetAvg();
Sort(player,PlayerNum);
最后的得分情况为:
"
for(intj=0;
{cout<
j+1<
;
setw(6)<
player[j].GetAvg();
player[j].GetName()<
程序运行的结果为:
第九章
1.在下面的程序中,派生类Derived的成员函数sets()能否访问基类base中得变量a和b?
为什么?
如果在基类base中将数据成员a和b定义为私有成员,下面的程序能否通过编译?
classBase{
voidset(inti,intj){a=i;
b=j;
voidshow(){cout<
a="
a<
b="
b<
protected:
inta,b;
classDerived:
publicBase{
voidsets(){s=a*b;
voidshows(){cout<
s="
s<
ints;
intmain(){
Derivedobj;
obj.set(2,3);
obj.show();
obj.sets();
obj.shows();
可以访问a和b;
因为a,b为保护成员,经公有继承,成为派生类中得保护成员,所以派生类Derived的成员函数sets()能访问基类base中得变量a和b。
如果定义为私有,则不能通过编译。
因为派生类中的成员函数不能访问基类中得私有成员
2.声明一个图形基类Shape,在它的基础上派生出矩形类Rectangle和圆形类Circle,它们都有计算面积的和周长、输出图形信息的成员函数,再在Rectangle类的基础上派生方形类Square。
编写程序和各类的定义和实现,以及类的使用。
#include<
classShape{
doublegetArea(){}
doublegetPerimeter(){}
classRectangle:
publicShape{
doubleheight;
doublewidth;
Rectangle(){}
Rectangle(doublea,doubleb)
height=a;
width=b;
doublegetArea()
{returnheight*width;
doublegetPerimeter()
{return2*(height+width);
classCircle:
publicShape{
Circle(doublex):
r(x){}
return2*3.1416*r;
returnr*r*3.1416;
doubler;
classSquare:
publicRectangle
Square(doublex)
height=x;
width=x;
Rectanglea1(2.2,3.2);
Circlea2(4.2);
Squarea3(5.3);
Area="
a1.getArea()<
Perimeter="
a1.getPerimeter()<
a2.getArea()<
a2.getPerimeter()<
a3.getArea()<
a3.getPerimeter()<
5.某销售公司有销售经理和销售员工,月工资的计算办法为:
销售激励的底薪为4000元,并将销售额的2/1000做提成,销售员工无底薪,只提取5/1000为工资。
编写程序:
(1)定义一个积累Employee,它包含三个数据成员number,name和salary,以及编号和姓名的构造函数。
(2)有Employee类派生Salesman类。
Salesman类包含两个新数据成员commrate和sales,还包括了用于输入销售额并计算销售员工工资的成员函数pay()和用于输出打印的print()。
(3)由Salesman派生Salemanager,Salesmmanage类包含新数据成员monthlypay,以及用于输入销售额并计算销售经理工资的成员函数pay()和用于输出的print()。
(4)编写main()函数中测试所设计的类结构。
(1)classEmployee
Employee(longnum,char*n)
number=num;
strcpy(name,n);
longnumber;
charname[20];
doublesalary;
(2)classSalesman:
publicEmployee
Salesman(longnum,char*n,floatc=0.005):
Employee(num,n)
{commrate=c;
voidpay()
Ç
ë
Ê
ä
È
name<
±
¾
Ô
Â
Í
ê
³
É
µ
Ä
Ï
ú
Û
¶
î
cin>
sales;
salary=commrate*sales;
voidprint(){
¹
¤
×
Î
ª
£
º
salary<
floatcommrate;
doublesales;
(3)
classSalesmanager:
publicSalesman
Salesmanager(longnum,char*n,floatc=0.002,doublem=4000):
Salesman(num,n,c){
monthlypay=m;
voidpay(){
salary=monthlypay+commrate*sales;
doublemonthlypay;
(4)intmain()
Salesmans1(1234,"
À
ð
Salesmanagers2(1357,"
¿
s1.pay();
s1.print();
s2.pay();
s2.print();
第十章
1.定义一个辅助类Complex,重载运算符+、-、*、/使之能用于复数的加减乘除运算。
运算符重载函数为Complex类的成员函数。
编写程序,分别求出两个复数之和、差、积、商。
classComplex
Complex(doubler=0.0,doublei=0.0){real=r;
imag=i;
Complexoperator+(Complex);
friendComplexoperator-(Complex,Complex);
Complexoperator*(Complex);
friendComplexoperator/(Complex,Complex);
voiddisplay();
doublereal,imag;
ComplexComplex:
operator+(Complexc)
returnComplex(real+c.real,imag+c.imag);
Complexoperator-(Complexc1,Complexc2)
returnComplex(c1.real-c2.real,c1.imag-c2.imag);
operator*(Complexc)
returnComplex(real*c.real-imag+c.imag,imag*c.imag+real*c.real);
Complexoperator/(Complexc1,Complexc2)
doubler,i;
r=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
i=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return(r,i);
voidComplex:
display()
real<
imag<
Complexc1(7.1,8.4),c2(1.4,5),c3;
c1="
c1.display();
c2="
c2.display();
c3=c1+c2;
c1+c2="
c3.display();
c3=c1-c2;
c1-c2="
c3=c1*c2;
c1*c2="
c3=c1/c2;
c1/c2="
2.定义一个日期类Date,包括年,月,日等私有数据成员。
要求为所定义的Date类设计如下重载运算符函数:
Dateoperator+(intdays);
返回一日起加天数days后得到的日期
Dateoperator-(intdays);
返回一日起减天数days后得到的日期
Intoperator-(Date&
b);
返回两日期相差的天数
intday_tab[12]={31,28,31,30,31,30,31,31,30,31,30,31};
classDate
Date(){}
Date(inty,intm,intd){year=y;
mon=m;
day=d;
voidsetday(intd){day=d;
voidsetmon(intm){mon=m;
voidsetyear(inty){year=y;
intgetday(){returnday;
intgetmon(){returnmon;
intgetyear(){returnyear;
Dateoperator+(intdays)
static