李爱华程磊面向对象程序设计课后问题详解完整版.docx

上传人:b****6 文档编号:4565987 上传时间:2022-12-06 格式:DOCX 页数:42 大小:24.11KB
下载 相关 举报
李爱华程磊面向对象程序设计课后问题详解完整版.docx_第1页
第1页 / 共42页
李爱华程磊面向对象程序设计课后问题详解完整版.docx_第2页
第2页 / 共42页
李爱华程磊面向对象程序设计课后问题详解完整版.docx_第3页
第3页 / 共42页
李爱华程磊面向对象程序设计课后问题详解完整版.docx_第4页
第4页 / 共42页
李爱华程磊面向对象程序设计课后问题详解完整版.docx_第5页
第5页 / 共42页
点击查看更多>>
下载资源
资源描述

李爱华程磊面向对象程序设计课后问题详解完整版.docx

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

李爱华程磊面向对象程序设计课后问题详解完整版.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#include

#include

classDate

{

intyear;

intmonth;

intday;

public:

Date(inty,intm,intd)

{

year=y;month=m;day=d;

}

voiddisp()

{

cout<

}

friendintcount_day(Date&d,intk);

friendintl(intyear);

friendinth(Date&d1,Date&d2);

};

intcount_day(Date&d,intk)

{

staticintday_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},

{31,29,31,30,31,30,31,31,30,31,30,31}};

//使用二维数组存放各月天数,第一行对应非闰年,第二行对应闰年

intj,i,s;

if(l(d.year))

j=1;//闰年,取1

elsej=0;//非闰年,取0

if(k)//K非0时

{

s=d.day;

for(i=1;i

s+=day_tab[j][i-1];

}

else//K为0时

{

s=day_tab[j][d.month]-d.day;

for(i=d.month+1;i<=12;i++)

s+=day_tab[j][i-1];

}

returns;//S为相差的天数

}

intl(intyear)

{

if(year%4==0&&year%100!

=0||year%400==0)//是闰年

return1;

else//不是闰年

return0;

}

inth(Date&d1,Date&d2)

{

intdays,day1,day2,y;

if(d1.year

{

days=count_day(d1,0);

for(y=d1.year+1;y

if(l(y))//闰年

days+=366L;

else//非闰年

days+=365L;

days+=count_day(d2,1);

}

elseif(d1.year==d2.year)

{

day1=count_day(d1,1);

day2=count_day(d2,1);

days=day2-day1;

}

else

days=-1;

returndays;

}

voidmain()

{intyear1,year2,month1,month2,day1,day2;

cout<<"输入日期1"<

cin>>year1>>month1>>day1;

cout<<"输入日期2"<

cin>>year2>>month2>>day2;

Dated1(year1,month1,day1),d2(year2,month2,day2);

intds=h(d1,d2);

{

cout<<"输出结果:

"<

}

if(ds>=0)

{

d1.disp();printf("与");

d2.disp();printf("之间有%d天\n\n",ds);

}

else//第一个日期小于第二个日期

cout<<"时间错误!

"<

}

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;

}

5-11

#include

classvehicle//定义汽车类

{

protected:

intdate;//年份

floatprice;//价格

public:

vehicle(intdate,floatprice);

intget_date();

floatget_price();

floatdate_load();

voidshow();

};

classcar:

publicvehicle//定义小车类

{

intpassenger_load;//载人数

public:

car(intdate,floatprice,intpassengers=4);

intget_passengers();

voidshow();

};

classtruck:

publicvehicle//定义卡车类

{

floatpayload;//载重量

public:

truck(intdate,floatprice,floatmax_load=24000.00);

floatefficiency();

voidshow();

};

vehicle:

:

vehicle(intdate,floatprice)

{

vehicle:

:

date=date;

vehicle:

:

price=price;

}

intvehicle:

:

get_date()

{

returndate;

}

floatvehicle:

:

get_price()

{

returnprice;

}

voidvehicle:

:

show()

{

cout<<"年份:

"<

cout<<"价格:

"<

}

car:

:

car(intdate,floatprice,

intpassengers):

vehicle(date,price)

{

passenger_load=passengers;

}

intcar:

:

get_passengers()

{

returnpassenger_load;

}

voidcar:

:

show()

{

cout<<"车型:

小车"<

vehicle:

:

show();

cout<<"载人:

"<

cout<

}

truck:

:

truck(intdate,floatprice,floatmax_load):

vehicle(date,price)

{

payload=max_load;

}

floattruck:

:

efficiency()

{

returnpayload;

}

voidtruck:

:

show()

{

cout<<"车型:

卡车"<

vehicle:

:

show();

cout<<"载重:

"<

cout<

}

voidmain()

{

carcar1(2001,2000,5);

trucktru1(2002,8000,340000);

cout<<"输出结果"<

car1.show();

tru1.show();

}

第六章

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:

virtualdoubleArea()=0;

};

 

classCircle:

publicShape

{

doubleradius;

public:

Circle(doubler):

radius(r){}

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

};

classSquare:

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

当前位置:首页 > 高中教育 > 高中教育

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

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