李爱华程磊 面向对象程序设计课后答案.docx

上传人:b****6 文档编号:6156982 上传时间:2023-01-04 格式:DOCX 页数:32 大小:21.27KB
下载 相关 举报
李爱华程磊 面向对象程序设计课后答案.docx_第1页
第1页 / 共32页
李爱华程磊 面向对象程序设计课后答案.docx_第2页
第2页 / 共32页
李爱华程磊 面向对象程序设计课后答案.docx_第3页
第3页 / 共32页
李爱华程磊 面向对象程序设计课后答案.docx_第4页
第4页 / 共32页
李爱华程磊 面向对象程序设计课后答案.docx_第5页
第5页 / 共32页
点击查看更多>>
下载资源
资源描述

李爱华程磊 面向对象程序设计课后答案.docx

《李爱华程磊 面向对象程序设计课后答案.docx》由会员分享,可在线阅读,更多相关《李爱华程磊 面向对象程序设计课后答案.docx(32页珍藏版)》请在冰豆网上搜索。

李爱华程磊 面向对象程序设计课后答案.docx

李爱华程磊面向对象程序设计课后答案

第二章

2-4

#include

usingnamespacestd;

Add(inta,intb);

intmain()

{

intx,y,sum;

cout<<"pleaseinputxandy:

";

cin>>x>>y;

sum=add(x,y);

cout<

}

Add(inta,intb)

{

returna+b;

}

2-5

(1)thisisaC++program.

(2)

x=50.6y=10z=A

x=216.34y=10z=A

x=216.34y=2z=A

x=216.34y=2z=E

(3)

xyz

50010000

50015001500

5002001500

 

2-6

#include

usingnamespacestd;

intmain()

{

 

int*p,*init;

intcountp=0;

intcountn=0;

p=newint[20];

init=p;

for(inti=0;i<20;i++)

{

cin>>*p;

p++;

}

p=p-20;

for(i=0;i<20;i++)

{

if(*p>0)countp++;

if(*p<0)countn++;

cout<<*p<<"";

p++;

}

cout<<"正数有:

"<

cout<<"负数有:

"<

p=init;

delete[]p;

return0;}

2-7不做要求

#include

//#include

usingnamespacestd;

 

voidcheckagescore(stringname,intage)

{

if(name=="exit")throwname;

if(age<0||age>50)

throwage;

}

intmain()

{

stringname;

intage;

for(inti=0;i<5;i++)

{

cin.ignore();

getline(cin,name);

 

cin>>age;

try

{

checkagescore(name,age);

}

catch(string)

{

cout<<"exception:

nameisexit"<

continue;

}

catch(int)

{

cout<<"exception:

ageisnotproper"<

continue;

}

cout<<"name:

"<

"<

}

return0;

}

第三章

3-1

(1)A

(2)C(3)B(4)C(5)C

(6)B(7)B(8)C(9)C

3-7

(1)

main()函数中

p1.age=30;语句是错误的。

age是类的私有成员

(2)

构造函数应当给常数据成员和引用成员初始化,将构造函数改为:

A(inta1,intb1):

a(a1),b(b1){}

A(inta1):

a(a1),b(a){}再将main中的Aa(1,2);改为Aa

(1);

(3)

(1)在Test类中添加语句:

voidprint();

 

voidPrint(){

cout<

}

改为

voidTest:

:

Print(){

cout<

}

 

main函数中

Init(38,15);改为:

A.Init(38,15);

Print();改为:

A.Print();

 

3-8

(1)

ConstructingA

ConstructingB

DestructingB

DestructingA

(2)

doublea,doubleb

point&p

p.x

3-9

classbox

{

intlen1,len2,len3;

public:

box(intl1,intl2,intl3){len1=l1;len2=l2;len3=l3;}

longvolumn(){returnlen1*len2*len3;}

};

3-10

classTest{

intm1,m2;

public:

voidInit(inta,intb){m1=a;m2=b;}

voidPring(){cout<

};

3-11

3-12

 

}

第四章

4-6

(1)D

(2)D(3)D(4)D(5)B

(6)D

4-7

(1)

staticintcount=0;这样初始化静态成员值是不对的将其改为

staticintcount;

在类外,main函数前加

intSample:

:

count=0;

 

(2)

#include

//#include

usingnamespacestd;

classCtest

{

private:

intx;constinty1;

public:

constinty2;

Ctest(inti1,inti2):

y1(i1),y2(i2)

{

y1=10;//y1为常量不能赋值

x=y1;

}

intreadme()const;

};

intCtest:

:

readme()const

{

inti;

i=x;

x++;//常函数内不能改变成员值

returnx;

}

intmain()

{

Ctestc(2,8);

inti=c.y2;

c.y2=i;//y2为常量,不能改值

i=c.y1;//y1私有,类外不能访问

return0;

}

将出错语句全部注释

4-8

(1)

题中印刷错误,将classC构造函数改为:

C()

{cout<<"constructorC:

";}

运行结果为:

constructorA

constructorB

constructorC

 

(2)

40

(3)

3

4

3

 

4-9略

4-10

#include

#include

classStudent

{

intnumber;

charname[20];

 

public:

Student(inti=0,char*s="\0")//构造学生对象

{number=i;

strcpy(name,s);

}

voidPrint()//输出结果

{cout<<"Number:

"<

cout<<"Name:

"<

}

friendboolgreaterthan(Student&st1,Student&st2);

};

boolgreaterthan(Student&st1,Student&st2)

{

returnst1.number>st2.number;//返回成员number的比较结果

}

intmain()

{

Studentst[5]={Student(65,"Li"),Student(78,"Zhang"),Student(80,"wang"),Student(92,"zhao"),Student(50,"zhen")};

intmax=0;

intmin=0;

for(inti=1;i<5;i++)

{if(!

greaterthan(st[max],st[i]))

max=i;

if(!

greaterthan(st[i],st[min]))

min=i;

}

cout<<"最大成绩:

"<

st[max].Print();

cout<<"最小成绩:

"<

st[min].Print();

return0;

}

 

4-11

#include

#include

usingnamespacestd;

classBook

{

char*name;

char*author;

intsale;

public:

Book()

{name='\0';

author='\0';

sale=-1;

}

Book(char*a,char*b,intc)

{

name=newchar[strlen(a)+1];

strcpy(name,a);

author=newchar[strlen(b)+1];

strcpy(author,b);

sale=c;

}

voidprint()

{cout<<"autor"<

cout<<"name"<

cout<<"price"<

}

~Book()

{

if(!

name)delete[]name;

if(!

author)delete[]author;

}

};

intmain()

{

Bookb1("c++","liaihua",12);

Bookb2;

return0;

}

 

第五章

5-8

改错题答案不唯一

(1)classDC{

intx;

public:

DC(){x=100;}

};

(2)编译无错,但逻辑错误,可改为:

classBC

{

protected:

intx;

public:

BC(inti=0){x=i}

};

classDC:

privateBC

{

public:

DC(inti):

BC(i){}

};

(3)将DC构造函数改为:

DC(inti):

BC(i){y=0;}

5-9

(1)baseclass

(2)(10,5)

(3,9-18,33)

(13,19)

(13,19-18,33)

(13,19)

5-10

#include

usingnamespacestd;

classShape

{

intx,y;

public:

Shape(intix,intiy){x=ix;y=iy;}

virtualvoidshow(){cout<<"pos:

"<

};

 

classCircle:

publicShape

{

intradius;

public:

Circle(intix,intiy,intr):

Shape(ix,iy),radius(r){}

voidshow(){Shape:

:

show();

cout<<"circle:

"<

};

classRect:

publicShape

{

intwidth,higth;

public:

Rect(intix,intiy,intiw,intih):

Shape(ix,iy),width(iw),higth(ih){}

voidshow(){Shape:

:

show();

cout<<"widthandhigth:

"<

};

 

intmain()

{

Shapes1(1,1);

Rectr1(2,2,8,8);

Circlec1(3,3,9);

r1.show();

c1.show();

return0;

}

 

第六章

6-4

d=3

D:

:

fun();

6-5

C:

:

print(),cinfo=2

C:

:

print(),cinfo=2

D:

:

print(),dinfo=4

B类不能定义对象,否则编译通不过,因为B未定义基类A中的虚函数

print(),它也是个虚基类。

6-6

#include

usingnamespacestd;

classMammal

{

public:

virtualvoidSpeak()

{cout<<"inMammal"<

};

classDog:

publicMammal

{

public:

voidSpeak()

{cout<<"dogbark"<

};

intmain()

{

Dogdog;

Mammal*pM;

pM=&dog;

pM->Speak();

return0;

}

运行结果:

dogbark

6-7

#include

usingnamespacestd;

classBaseClass

{

public:

virtual~BaseClass()

{cout<<"destructBase"<

};

classDerived:

publicBaseClass

{

public:

~Derived()

{

cout<<"destructderived"<

}

};

intmain()

{

BaseClass*pbase;

pbase=newDerived;

deletepbase;

}

结果将不能正常执行子类析构

6-8以圆形和正方形为例

#include

usingnamespacestd;

classShape

{

public:

virtualfloatarea()=0;

};

 

classCircle:

publicShape

{

intradius;

public:

Circle(intr):

radius(r){}

floatarea(){return3.14*radius*radius;}

};

classSquare:

publicShape

{

intwidth;

public:

Square(intw):

width(w){}

floatarea(){returnwidth*width;}

};

 

intmain()

{

floatareaSum=0;

Shape*pS[3];

pS[1]=newCircle

(1);

pS[2]=newSquare

(2);

areaSum+=pS[1]->area();

areaSum+=pS[2]->area();

cout<

return0;

}

6-9

#include

usingnamespacestd;

classStudent

{

public:

virtualvoidshow()=0;

};

classJunior:

publicStudent

{

public:

voidshow(){cout<<"thiswouldbeinfoforjuniorstudents"<

};

classSenior:

publicStudent

{

public:

voidshow(){cout<<"thiswouldbeinfoforSeniorstudents"<

};

 

intmain()

{

Student*pstu;

pstu=newJunior;

pstu->show();

pstu=newSenior;

pstu->show();

return0;

}

第七章

7-1

#include

#include

usingnamespacestd;

classvector

{

intx,y;

public:

vector(intix=0,intiy=0){x=ix;y=iy;}

vectoroperator+(vector&v1)

{

returnvector(x+v1.x,y+v1.y);

}

vector&operator+=(vector&v1)

{

x+=v1.x;

y+=v1.y;

return*this;

}

voidshow()

{

cout<<'('<

}

};

intmain()

{

vectorv1(1,2),v2(3,4),v3;

v3=v1+v2;

v1+=v2;

v3.show();

v1.show();

return0;

}

 

7-2

#include

classComplex

{

private:

doublereal,image;

public:

Complex(doublex=0.0,doubley=0.0)

{real=x;image=y;}

booloperator!

=(constComplex&c);

Complexoperator-(constComplex&c);

booloperator==(constComplex&c);

Complexoperator-();

Complex&operator+=(constComplex&c);

voidPrint();

};

voidComplex:

:

Print()

{

cout<

}

ComplexComplex:

:

operator-(constComplex&c)

{

Complextemp(real-c.real,image-c.image);

returntemp;

}

boolComplex:

:

operator==(constComplex&c)

{

return(real==c.real&&image==c.image);

}

boolComplex:

:

operator!

=(constComplex&c)

{

return(real!

=c.real||image!

=c.image);

}

ComplexComplex:

:

operator-()

{

returnComplex(-real,-image);

}

Complex&Complex:

:

operator+=(constComplex&c)

{

real+=c.real;

image+=c.image;

return*this;

}

intmain()

{

Complexc1(2,7),c2(4,2),c3;

c3=c1-c2;

c3.Print();

if(c3==c1)cout<<"c3equalstoc1"<

elsecout<<"c3doesn?

ˉtequaletoc1"<

c3=-c2;

c3.Print();

c3+=c2;

c3.Print();

if(c3!

=c1)cout<<"c3!

=c1"<

return0;

}

 

7-3

#include

usingnamespacestd;

boolrn(inty)

{

boolflag=0;

if(y%400==0||y%4==0&&y%100!

=0)

flag=1;

returnflag;

}

classDate

{private:

intmonth,day,year;

public:

Date(intm,intd,inty);

Date&operator+(intdays);

voidshowDate();

};

Date:

:

Date(inty,intm,intd)

{if(m>0&&m<13)

month=m;

if(d>0&&d<32)

day=d;

if(y>0&&y<3000)

year=y;

}

Date&Date:

:

operator+(intdays)

{

inti;

for(i=days;i>0;)

{

intdiff;

switch(month)

{

case1:

case3:

case5:

case7:

case8:

case10:

case12:

diff=31-day;break;

case4:

case6:

case9:

case11:

diff=30-day;break;

case2:

if(rn(year))diff=29-day;

elsediff=28-day;

break;

}

if(i>diff)

{

i-=diff+1;

day=1;

month++;

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

当前位置:首页 > 自然科学

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

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