C程序设计教程面向对象分册郑秋生主编第一章 答案.docx
《C程序设计教程面向对象分册郑秋生主编第一章 答案.docx》由会员分享,可在线阅读,更多相关《C程序设计教程面向对象分册郑秋生主编第一章 答案.docx(17页珍藏版)》请在冰豆网上搜索。
C程序设计教程面向对象分册郑秋生主编第一章答案
第1章参考答案
一、选择题
1.C2.B3.C4.A5.C
6.A7.C8C9A10C
二、阅读题
1.x=2,y=3
2.x=2,y=3
x!
=y
3.
Cstatic:
:
va1=0
cs1.vaI=1
cs2.val=2
cs1.val=4
cs2.vaI=4
四、改错题
#include
#include
classperson
{
public:
person(intn,char*nam,chars
{
num=n;
strcpy(name,nam;
sex=s;
cout<<"Constructorcalled."<
}
~person(
{
cout<<"Destructorcalled."<
}
voiddisplay(
{
cout<<"num:
"<
cout<<"name:
"<
cout<<"sex:
"<
}
private:
intnum;
charname[10];
charsex;
};
intmain(
{
persons1(10010,"'Wang_li",'f';
s1.display(;
persons2(10011,"Zhang_fun",'m';
s2.display(;
return0;
}
五、编程题
5.1
#include
usingnamespacestd;
classCBox
{
public:
CBox(doublel=0,doublew=0,doubleh=0;
doublearea(;
doublevolume(;
private:
doublelengh;
doublewidth;
doublehigh;
};
CBox:
:
CBox(doublel,doublew,doubleh
{
lengh=l;
width=w;
high=h;
}
doubleCBox:
:
area(
{
return2*(lengh*width+lengh*high+width*high;
}
doubleCBox:
:
volume(
{
returnlengh*width*high;
}
voidmain(
{
CBoxbox1(4,5,6;
cout<
cout<
}
5.2
#include
usingnamespacestd;
classCPoint
{
public:
CPoint(doublea=0,doubleb=0
{
x=a;
y=b;
}
CPoint(CPoint&p
{
x=p.x;
y=p.y;
}
voidprint(
{
cout<<"("<
}
private:
doublex,y;
};
classCLine
{
public:
CLine(doublex1=0,doubley1=0,doublex2=0,doubley2=0:
p1(x1,y1,p2(x2,y2
{
}
CLine(CPointx,CPointy:
p1(x,p2(y
{
}
CLine(CLine&lin
{
p1=lin.p1;
p2=lin.p2;
}
voidDrawLine(
{
cout<<"Lineform";
p1.print(;
cout<<"to";
p2.print(;
cout<
}
voidLinedel(
{
cout<<"deleteline"<
}
voidmove(CPoint&x,CPoint&y
{
cout<<"moveline"<
p1=x;
p2=y;
}
private:
CPointp1,p2;
};
voidmain(
{
CPointpoint1(1,5,point2(5,8,point3(20,30,point4(40,50;
CLineline1(point1,point2;
CLineline2(2,3,8,12;
line1.DrawLine(;
line2.DrawLine(;
line2.move(point3,point4;
line2.DrawLine(;
line2=line1;
line2.DrawLine(;
line1.Linedel(;
}
5.3
#include
usingnamespacestd;
classCComplex
{
public:
CComplex(double,double;
CComplex(CComplex&c;//复数类的拷贝构造函数声明
doubleGetReal(;
doubleGetImag(;
voidPrint(;
private:
doublereal;
doubleimag;
};
CComplex:
:
CComplex(doubler=0.0,doublei=0.0
{
real=r;
imag=i;
cout<<"调用两个参数的构造函数"<
}
CComplex:
:
CComplex(CComplex&c//复数类的拷贝构造函数定义
{
real=c.real;
imag=c.imag;
cout<<"调用拷贝构造函数"<
}
doubleCComplex:
:
GetReal({returnreal;}
doubleCComplex:
:
GetImag({returnimag;}
voidCComplex:
:
Print(//显示复数值
{
cout<<"("<}
CComplexadd(CComplex&x,CComplex&y//普通函数完成两个数的加法,对象作为函数参数,
{
returnCComplex(x.GetReal(+y.GetReal(,x.GetImag(+y.GetImag(;
}
voidmain(void
{
CComplexa(3.0,4.0,b(5.6,7.9;
CComplexc(a;//调用复数类的拷贝构造函数
cout<<"a=";
a.Print(;
cout<<"b=";
b.Print(;
cout<<"c=";
c.Print(;
cout<<"c=a+b"<
c=add(a,b;
cout<<"c=";
c.Print(;
}
5.4
#include
#include
usingnamespacestd;
classCStudent//类声明
{
public:
CStudent(char*,float,float,float;
CStudent(CStudent&s;
~CStudent(;
voiddisplay(;
friendfloatavg(CStudent&s;
private:
char*name;
floatgrad[3];
};
CStudent:
:
CStudent(char*na,floata,floatb,floatc
{
name=newchar[strlen(na+1];
strcpy(name,na;
grad[0]=a;
grad[1]=b;
grad[2]=c;
}
CStudent:
:
CStudent(CStudent&s
{
name=newchar[strlen(s.name+1];
strcpy(name,s.name;
grad[0]=s.grad[0];
grad[1]=s.grad[1];
grad[2]=s.grad[2];
}
CStudent:
:
~CStudent(
{
delete[]name;
}
voidCStudent:
:
display(
{
inti;
cout<<"name:
"<
for(i=0;i<3;i++
cout<<"grad["<
}
floatavg(CStudent&s//普通函数,需要引用私有成员,声明为学生类的友元函数
{
return(s.grad[0]+s.grad[1]+s.grad[2]/3;
}
intmain(
{
CStudentstud1("张三",79,98,82;//定义对象
stud1.display(;
cout<<"平均成绩:
"<
return0;
}
5.5
#include
usingnamespacestd;
classCString
{
public:
CString(;//缺省构造函数,初始化为空串
CString(charch,intnRepeat;//用一个字符重复n次,初始化字符串
CString(constchar*psz;//用一个字符串初始化
CString(CString&stringser;//拷贝构造函数
~CString(;
intGetLength(const;
boolIsEmpty(const;
charGetAt(intnindexconst;
voidSetat(intnindex,charch;
voidPrint(;
intcompare(CString&string;
intcompare(constchar*pszconst;
voidVacate(;
private:
char*s;
};
CString:
:
CString(
{
s=NULL;
}
CString:
:
CString(CString&stringser
{
s=newchar[strlen(stringser.s+1];
if(s!
=0
strcpy(s,stringser.s;
}
CString:
:
CString(charch,intnRepeat
{
s=newchar[nRepeat+1];
for(inti=0;i
s[i]=ch;
s[nRepeat]='\0';
}
CString:
:
CString(constchar*psz
{
s=newchar[strlen(psz+1];
if(s!
=0
strcpy(s,psz;
}
intCString:
:
GetLength(const
{inti=0;
while(s[i]!
='\0'
i++;
returni;
}
boolCString:
:
IsEmpty(const
{
if(s==NULL
return1;
else
return0;
}
voidCString:
:
Vacate(
{
s[0]='\0';
cout<<"Nowhavevacatedstring."<
}
charCString:
:
GetAt(intnindexconst
{
if(nindex>1&&nindex<=GetLength(+1
returns[nindex-1];
else
return'0';
}
voidCString:
:
Setat(intnindex,charch
{
if(nindex>1&&nindex<=GetLength(+1
s[nindex-1]=ch;
}
voidCString:
:
Print(
{
cout<
}
intCString:
:
compare(CString&string
{
if(strcmp(s,string.s>0
return1;
elseif(strcmp(s,string.s<0
return-1;
else
return0;
}
intCString:
:
compare(constchar*pszconst
{
if(strcmp(s,psz>0
return1;
elseif(strcmp(s,psz<0
return-1;
elsereturn0;
}
CString:
:
~CString(
{
//cout<析构:
"<
if(s!
=NULL
delete[]s;
}
voidmain(
{
chara[4]="y";charb[4];
CStringc1("Hellow",c2(c1;
cout<<"Stringc1is:
"<<"";c1.Print(;
cout<
cout<<"Stringc2is:
"<<"";c2.Print(;
cout<
CStringc3('b',3;
cout<<"Stringc3is:
"<<"";c3.Print(;
cout<
cout<<"*******************以下是对串的基本操作****************"<
intnum=c1.GetLength(;
charch;
cout<<"c1的长度是:
"<<""<
ch=c1.GetAt(5;
cout<<"获得字符串c1中第"<<5<<"个字符是:
"<
cout<<"下面是插入字符串c1中一个特殊字符'd'"<
c1.Setat(5,'d';
cout<<"插入后字符串c1变为:
"<<"";
c1.Print(;
//////////////////////
cout<
if(c1.IsEmpty(==1
cout<<"Stringisempty."<
else
cout<<"Stringisn'tempty."<
cout<<"下面是判断字符串c1清空"<
c1.Vacate(;
cout<<"清空后输出字符串c1:
"<<"\n";
c1.Print(;
cout<<"字符串已被清空"<
cout<<"请按任意键继续"<
cin>>b;
///////////////////////////
cout<<"****************以下是对串的赋值******************"<
CStringc5=c2;
cout<<"Stringc5=c2is:
"<<"";c5.Print(;
cout<
cout<<"****************以下是对串的比较******************"<
inttemp=pare(c3;
cout<<"以下比较c2与c3"<
if(temp>0
cout<<"Stringc2>Stringc3"<
elseif(temp<0
cout<<"Stringc2
else
cout<<"Stringc9==Stringc4"<
cout<
cout<<"以下比较c2与任意字符串Goodboy!
"<
if(pare("Goodboy!
">0
cout<<"Stringc2>'Goodboy!
'"<
elseif(pare("Goodboy!
"<0
cout<<"Stringc2<'Goodboy!
"<
else
cout<<"Stringc2=='Goodboy!
'"<
}