李爱华程磊 面向对象程序设计课后答案Word文件下载.docx

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

李爱华程磊 面向对象程序设计课后答案Word文件下载.docx

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

李爱华程磊 面向对象程序设计课后答案Word文件下载.docx

p++;

}

p=p-20;

for(i=0;

if(*p>

0)countp++;

if(*p<

0)countn++;

cout<

*p<

"

正数有:

countp<

负数有:

countn<

p=init;

delete[]p;

return0;

2-7不做要求

//#include<

string>

voidcheckagescore(stringname,intage)

if(name=="

exit"

)throwname;

if(age<

0||age>

50)

throwage;

stringname;

intage;

for(inti=0;

5;

i++)

{

cin.ignore();

getline(cin,name);

cin>

age;

try

{

checkagescore(name,age);

}

catch(string)

cout<

exception:

nameisexit"

continue;

catch(int)

ageisnotproper"

continue;

}

cout<

name:

name<

age:

age<

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是类的私有成员

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

A(inta1,intb1):

a(a1),b(b1){}

A(inta1):

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

改为Aa

(1);

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

voidprint();

voidPrint(){

-"

x-y<

改为

voidTest:

:

Print(){

main函数中

Init(38,15);

改为:

A.Init(38,15);

Print();

A.Print();

3-8

ConstructingA

ConstructingB

DestructingB

DestructingA

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<

m1<

m2<

3-11

3-12

第四章

4-6

(1)D

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

(6)D

4-7

staticintcount=0;

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

staticintcount;

在类外,main函数前加

intSample:

count=0;

cstdlib>

classCtest

private:

intx;

constinty1;

constinty2;

Ctest(inti1,inti2):

y1(i1),y2(i2)

y1=10;

//y1为常量不能赋值

x=y1;

intreadme()const;

intCtest:

readme()const

inti;

i=x;

x++;

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

returnx;

Ctestc(2,8);

inti=c.y2;

c.y2=i;

//y2为常量,不能改值

i=c.y1;

//y1私有,类外不能访问

将出错语句全部注释

4-8

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

C()

{cout<

constructorC:

运行结果为:

constructorA

constructorB

constructorC

40

3

4

4-9略

4-10

#include<

iostream.h>

string.h>

classStudent

intnumber;

charname[20];

Student(inti=0,char*s="

\0"

)//构造学生对象

{number=i;

strcpy(name,s);

voidPrint()//输出结果

{cout<

Number:

number<

Name:

friendboolgreaterthan(Student&

st1,Student&

st2);

boolgreaterthan(Student&

st2)

returnst1.number>

st2.number;

//返回成员number的比较结果

Studentst[5]={Student(65,"

Li"

),Student(78,"

Zhang"

),Student(80,"

wang"

),Student(92,"

zhao"

),Student(50,"

zhen"

)};

intmax=0;

intmin=0;

for(inti=1;

5;

{if(!

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

max=i;

if(!

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

min=i;

最大成绩:

st[max].Print();

最小成绩:

st[min].Print();

4-11

classBook

char*name;

char*author;

intsale;

Book()

{name='

\0'

author='

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"

author<

name"

price"

sale<

~Book()

name)delete[]name;

author)delete[]author;

Bookb1("

c++"

"

liaihua"

12);

Bookb2;

第五章

5-8

改错题答案不唯一

(1)classDC{

DC(){x=100;

}

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

classBC

protected:

BC(inti=0){x=i}

classDC:

privateBC

DC(inti):

BC(i){}

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

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

classShape

intx,y;

Shape(intix,intiy){x=ix;

y=iy;

virtualvoidshow(){cout<

pos:

'

'

classCircle:

publicShape

intradius;

Circle(intix,intiy,intr):

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

voidshow(){Shape:

show();

circle:

radius<

classRect:

intwidth,higth;

Rect(intix,intiy,intiw,intih):

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

widthandhigth:

width<

higth<

Shapes1(1,1);

Rectr1(2,2,8,8);

Circlec1(3,3,9);

r1.show();

c1.show();

第六章

6-4

d=3

D:

fun();

6-5

C:

print(),cinfo=2

print(),dinfo=4

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

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

6-6

classMammal

virtualvoidSpeak()

inMammal"

classDog:

publicMammal

voidSpeak()

dogbark"

Dogdog;

Mammal*pM;

pM=&

dog;

pM->

Speak();

运行结果:

dogbark

6-7

classBaseClass

virtual~BaseClass()

destructBase"

classDerived:

publicBaseClass

~Derived()

destructderived"

BaseClass*pbase;

pbase=newDerived;

deletepbase;

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

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

virtualfloatarea()=0;

Circle(intr):

radius(r){}

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

classSquare:

intwidth;

Square(intw):

width(w){}

floatarea(){returnwidth*width;

floatareaSum=0;

Shape*pS[3];

pS[1]=newCircle

(1);

pS[2]=newSquare

(2);

areaSum+=pS[1]->

area();

areaSum+=pS[2]->

areaSum<

6-9

classStudent

virtualvoidshow()=0;

classJunior:

publicStudent

voidshow(){cout<

thiswouldbeinfoforjuniorstudents"

classSenior:

thiswouldbeinfoforSeniorstudents"

Student*pstu;

pstu=newJunior;

pstu->

pstu=newSenior;

第七章

7-1

classvector

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

y=iy;

vectoroperator+(vector&

v1)

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

vector&

operator+=(vector&

x+=v1.x;

y+=v1.y;

return*this;

voidshow()

('

'

)'

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

v3=v1+v2;

v1+=v2;

v3.show();

v1.show();

7-2

classComplex

doublereal,image;

Complex(doublex=0.0,doubley=0.0)

{real=x;

image=y;

booloperator!

=(constComplex&

c);

Complexoperator-(constComplex&

booloperator==(constComplex&

Complexoperator-();

Complex&

operator+=(constComplex&

voidPrint();

voidComplex:

Print()

real<

+"

image<

i"

ComplexComplex:

operator-(constComplex&

c)

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

returntemp;

boolComplex:

operator==(constComplex&

return(real==c.real&

&

image==c.image);

boolComplex:

operator!

c)

return(real!

=c.real||image!

=c.image);

operator-()

returnComplex(-real,-image);

Complex:

real+=c.real;

image+=c.image;

return*this;

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

c3=c1-c2;

c3.Print();

if(c3==c1)cout<

c3equalstoc1"

elsecout<

c3doesn?

ˉtequaletoc1"

c3=-c2;

c3+=c2;

if(c3!

=c1)cout<

c3!

=c1"

7-3

boolrn(inty)

boolflag=0;

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

y%100!

=0)

flag=1;

returnflag;

classDate

{private:

intmonth,day,year;

Date(intm,intd,inty);

Date&

operator+(intdays);

voidshowDate();

Date:

Date(inty,intm,intd)

{if(m>

0&

m<

13)

month=m;

if(d>

d<

32)

day=d;

if(y>

y<

3000)

year=y;

Date&

Date:

operator+(intdays)

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;

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