C++程序的设计教程面向对象分册郑秋生完整答案.docx

上传人:b****6 文档编号:8083130 上传时间:2023-01-28 格式:DOCX 页数:31 大小:24.27KB
下载 相关 举报
C++程序的设计教程面向对象分册郑秋生完整答案.docx_第1页
第1页 / 共31页
C++程序的设计教程面向对象分册郑秋生完整答案.docx_第2页
第2页 / 共31页
C++程序的设计教程面向对象分册郑秋生完整答案.docx_第3页
第3页 / 共31页
C++程序的设计教程面向对象分册郑秋生完整答案.docx_第4页
第4页 / 共31页
C++程序的设计教程面向对象分册郑秋生完整答案.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

C++程序的设计教程面向对象分册郑秋生完整答案.docx

《C++程序的设计教程面向对象分册郑秋生完整答案.docx》由会员分享,可在线阅读,更多相关《C++程序的设计教程面向对象分册郑秋生完整答案.docx(31页珍藏版)》请在冰豆网上搜索。

C++程序的设计教程面向对象分册郑秋生完整答案.docx

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(intnindex)const;

voidSetat(intnindex,charch);

voidPrint();

intcompare(CString&string);

intcompare(constchar*psz)const;

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(intnindex)const

{

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*psz)const

{

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!

'"<

}

第二章继承和派生

一、选择题

1.D2.B3.D

一、阅读程序题

四、编程题

4.1

#include

#include

#definePAI3.14

classCircle

{

public:

Circle(){r=0;}

Circle(doubled){r=d;}

doublearea(){return(PAI*r*r);}

voiddisplay1(){cout<<"桌子的面积:

"<

private:

doubler;

};

classTable

{

public:

Table(){heig=0;}

Table(doubleh){heig=h;}

voiddisplay2(){cout<<"桌子的高度:

"<

private:

doubleheig;

};

classRoundtable:

publicCircle,publicTable

{

public:

Roundtable(){strcpy(color,"白色");}

Roundtable(doublea,doubleb,char*c):

Circle(a),Table(b){strcpy(color,c);}

voiddisplay()

{display1();

display2();

cout<<"桌子的颜色:

"<

private:

charcolor[20];

};

voidmain()

{

Roundtables(2.0,3.0,"黑色");

s.display();

}

4.2

如果Age在基类中被定义为私有的,SetAge(intn)不能直接给Age赋值,如果Age是基类的公有成员变量,则可以直接赋值。

classAnimal

{

public:

Animal(){};

intAge;

};

classDog:

publicAnimal

{

public:

Dog(){};

VoidSetAge(intn)

{

Age=n;

}

};

4.3

#include

classRectangle

{

public:

Rectangle(doublel,doublew){length=l,width=w}

doublearea(){returnlength*width;}

voiddisplay1();

private:

doublelength;

doublewidth;

};

voidRectangle:

:

display1()

{cout<<"长:

"<

cout<<"宽:

"<

classCuboid:

publicRectangle

{

public:

Cuboid(doubleL,doublew,doubleh):

Rectangle(L,w){high=h,volume=L*w*high};

doublevol(){returnarea()*high;}

voidshow();

private:

doublehigh;

doublevolume;

};

voidCuboid:

:

show()

{

display1();

cout<<"高:

"<

cout<<"体积:

"<

}

voidmain()

{

Cuboidcub(10,20,30);

cub.show();

}

4.4

#include

classVehicle

{

public:

Vehicle():

maxspeed(0),weight(0){}

Vehicle(doublem,doublew):

maxspeed(m),weight(w){};

voidrun()

{cout<<"可以奔跑"<

voidstop()

{cout<<"可以停止奔跑"<

private:

doublemaxspeed;

doubleweight;

};

classBicycle:

virtualpublicVehicle

{

public:

Bicycle(doublem,doublew,doubleh):

Vehicle(m,w),height(h){}

private:

doubleheight;

};

classMotorcar:

virtualpublicVehicle

{

public:

Motorcar(doublem,doublew,doubles):

Vehicle(m,w),setnum(s){}

private:

doublesetnum;

};

classMotorcycle:

publicBicycle,publicMotorcar

{

public:

Motorcycle(doublem,doublew,doubleh,doubles):

Bicycle(m,w,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1