面向对象程序设计作业参考答案.docx

上传人:b****4 文档编号:4986383 上传时间:2023-05-07 格式:DOCX 页数:18 大小:18.03KB
下载 相关 举报
面向对象程序设计作业参考答案.docx_第1页
第1页 / 共18页
面向对象程序设计作业参考答案.docx_第2页
第2页 / 共18页
面向对象程序设计作业参考答案.docx_第3页
第3页 / 共18页
面向对象程序设计作业参考答案.docx_第4页
第4页 / 共18页
面向对象程序设计作业参考答案.docx_第5页
第5页 / 共18页
面向对象程序设计作业参考答案.docx_第6页
第6页 / 共18页
面向对象程序设计作业参考答案.docx_第7页
第7页 / 共18页
面向对象程序设计作业参考答案.docx_第8页
第8页 / 共18页
面向对象程序设计作业参考答案.docx_第9页
第9页 / 共18页
面向对象程序设计作业参考答案.docx_第10页
第10页 / 共18页
面向对象程序设计作业参考答案.docx_第11页
第11页 / 共18页
面向对象程序设计作业参考答案.docx_第12页
第12页 / 共18页
面向对象程序设计作业参考答案.docx_第13页
第13页 / 共18页
面向对象程序设计作业参考答案.docx_第14页
第14页 / 共18页
面向对象程序设计作业参考答案.docx_第15页
第15页 / 共18页
面向对象程序设计作业参考答案.docx_第16页
第16页 / 共18页
面向对象程序设计作业参考答案.docx_第17页
第17页 / 共18页
面向对象程序设计作业参考答案.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

面向对象程序设计作业参考答案.docx

《面向对象程序设计作业参考答案.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计作业参考答案.docx(18页珍藏版)》请在冰点文库上搜索。

面向对象程序设计作业参考答案.docx

面向对象程序设计作业参考答案

习题一

5、分析下面程序运行的结果。

#

usingnamespacestd;

intmain()

{

cout<<”This”<<”is”;

cout<<”a”<<”C++”;

cout<<”program.”<

return0;

}

输出结果:

ThisisaC++program.

6、分析下面程序运行的结果。

#include

usingnamespacestd;

intmain()

{

inta,b,c;

a=10;

b=23;

c=a+b;

cout<<'a+b=";

cout<

cout<

return0;

}

输出结果:

a+b=33

8、在你所用的C++系统上,输入以下程序,进行编译,观察编译情况,如果有错误,请修改程序,在进行编译,直到没有错误,然后进行连接和运行,分析运行结果。

修改后的程序如下:

#include

usingnamespacestd;

intmain()

{

inta,b;

a=b=0;

intc=a+b;

cout<<”a+b=”<

return0;

}

9、输入以下程序,进行编译,观察编译情况,如果有错误,请修改程序,在进行编译,直到没有错误,然后进行连接和运行,分析运行结果。

修改后的程序如下:

#include

usingnamespacestd;

intadd(intx,inty);

intmain()

{

inta=0,b=0;

intc=add(a,b);

cout<<"a+b="<

return0;

}

intadd(intx,inty)

{

intz=x+y;

returnz;

}

习题二

1、请检查下面的程序,找出其中的错误(先不要上机,在纸面上作人工检查),并改正之。

然后上机调试,使之能正常运行。

运行时从键盘输入时、分、秒的值,检查输出是否正确。

改正以后的程序如下:

#include

usingnamespacestd;

classTime

{

public:

voidset_time(void);

voidshow_time(void);

private:

inthour;

intminute;

intsec;

};

Timet;

intmain()

{

t.set_time();

t.show_time();

return0;

}

voidTime:

:

set_time(void)

{

cin>>hour;

cin>>minute;

cin>>sec;

}

voidTime:

:

show_time(void)

{

cout<

"<

"<

}

6、需要求3个长方柱的体积,请编写一个基于对象的程序。

数据成员包括length(长)、width(宽)、height(高)。

要求用成员函数实现一以下功能:

(1)由键盘分别输入3个长方柱的长、宽、高;

(2)计算长方柱的体积;

(3)输出3个长方柱的体积;

请编写程序,上机调试并运行。

参考程序如下:

#include

usingnamespacestd;

classCuboid

{

public:

voidSetValue()

{

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

{

cin>>length[i];

cin>>width[i];

cin>>height[i];

}

}

voidcalArea()

{

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

{

area[i]=length[i]*width[i]*height[i];

}

}

voidshowArea()

{

calArea();

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

{

cout<<"长方柱"<

"<

}

}

private:

doublelength[3],width[3],height[3],area[3];

};

intmain()

{

Cuboidc;

c.SetValue();

c.showArea();

return0;

}

习题三

1、构造函数和析构函数的作用是什么?

什么时候需要自己定义构造函数和析构函数?

答:

构造函数的作用:

用来初始化对象。

析构函数的作用:

在删除一个对象前被调用,释放该对象成员的内存空间,以及其它一些清理工作。

用户需要按照一定的需求去初始化对象时,需要定义构造函数。

同理,释放对象时,用户需要按照一定的需求去释放内存或者其他清理工作,需要定义析构函数。

2、分析下面的程序,写出其运行时的输出结果。

#include

usingnamespacestd;

classDate

{

public:

Date(intm,intd,inty):

month(m),day(d),year(y){}

Date(intm,intd):

month(m),day(d){year=2005;}

Date(intm):

month(m){day=1;year=2005;}

Date(){month=1;day=1;year=2005;}

voiddisplay(){cout<

private:

intyear,month,day;

};

intmain(){

Dated1(10,13,2005);

Dated2(12,30);

Dated3(10);

Dated4;

d1.display();

d2.display();

d3.display();

d4.display();

return0;

}

输出结果如下:

10/13/2005

12/30/2005

10/1/205

1/1/2005

3、题目略。

答:

有问题,Dated2(12,30);这行代码具有二义性,可以调用第一个构造函数,也可以调用第四个构造函数。

Dated3(10)这行代码具有二义性,可以调用第一个构造函数,也可以调用第三个构造函数

Dated4;这行代码具有二义性,可以调用第一个构造函数,也可以调用第二个构造函数。

解决的办法就是去掉第二、三和四个构造函数。

4、建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。

程序代码如下:

#include

usingnamespacestd;

classStudent

{

public:

Student(intn,floats):

num(n),score(s){}

voiddisplay(){cout<

private:

intnum;

floatscore;

};

intmain()

{

Students[5]={Student(1,91),Student(2,92),Student(3,93),Student(4,94),Student(5,95)};

Student*ps=s;

ps->display();

ps++;

ps++;

ps->display();

ps++;

ps++;

ps->display();

return0;

}

6、阅读下面的程序,分析其执行结果,写出输入结果。

#include

usingnamespacestd;

classStudent

{

public:

Student(intn,floats):

num(n),score(s){}

voidchange(intn,floats){num=n;score=s;}

voiddisplay(){cout<

private:

intnum;

floatscore;

};

intmain()

{

Studentstud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return0;

}

输出结果如下:

10178.5

10180.5

7、将第6题得程序分别做如下修改,分析所修改部分的函数以及编译和运行的情况。

(1)将main函数第2行改为

constStudentstud(101,78.5);

答:

有错误,常对象只能调用常成员函数,而display和change函数都不是常成员函数。

(2)在

(1)的基础上修改程序,是只能正常运行,用change函数修改数据成员num和score的值。

答:

首先将change和display成员函数修改为常成员函数,然后将数据成员num和score修改为可变类型(在num和score的数据类型前加mutable)。

(3)将main函数改为

intmain()

{

Studentstud(101,78.5);

Student*p=&stud;

p->display();

p->change(101,80.5);

p->display();

return0;

}

其他部分仍同第6题的程序。

(4)在(3)的基础上将main函数改为

constStudent*p=&stud;

答:

有错误。

p指针是个常量指针,指向的是一个常对象,故而解决的办法同题

(2)。

(5)在把main函数第3行改为

Student*constp=&stud;

答:

没有错误。

p指针是个指针常量,指向stud以后不能再指向其他对象。

习题四

1、定义一个复数类Complex,重载运算符“+”、“=”,“*”,“、”,使之能进行复数的加、减、乘、除。

运算符重载函数作为Complex类得成员函数。

编程序,分别求两个复数之和、差、积和商。

#include

#include

usingnamespacestd;

classComplex{

public:

Complex(doubler=0,doublei=0);

Complexoperator+(constComplex&c);

Complexoperator-(constComplex&c);

Complexoperator*(constComplex&c);

Complexoperator/(constComplex&c);

voidprint()const;

private:

doublereal,imag;

};

Complex:

:

Complex(doubler,doublei){

real=r;

imag=i;

}

ComplexComplex:

:

operator+(constComplex&c){

doubler=real+c.real;

doublei=imag+c.imag;

returnComplex(r,i);

}

ComplexComplex:

:

operator-(constComplex&c){

doubler=real-c.real;

doublei=imag-c.imag;

returnComplex(r,i);

}

ComplexComplex:

:

operator*(constComplex&c){

doubler=real*c.real-imag*c.imag;

doublei=real*c.imag+imag*c.real;

returnComplex(r,i);

}

ComplexComplex:

:

operator/(constComplex&c){

doublet=pow(c.imag,2)+pow(c.real,2);

doubler=real*c.real+imag*c.imag;

doublei=imag*c.real-real*c.imag;

returnComplex(r/t,i/t);

}

voidComplex:

:

print()const{

cout<<'('<

}

intmain()

{

Complexa(3,2),b(0,3),c;

c=a+b;

c.print();

c=a-b;

c.print();

c=a*b;

c.print();

c=a/b;

c.print();

return0;

}

习题五

7、有以下程序,请完成下面的工作:

①阅读程序,写出运行时输出的结果。

#include

usingnamespacestd;

classA{

public:

A(){a=0;b=0;}

A(inti){a=i;b=0;}

A(inti,intj){a=i;b=j;}

voiddisplay(){cout<<"a="<

private:

inta;

intb;

};

classB:

publicA

{

public:

B(){c=0;}

B(inti):

A(i){c=0;}

B(inti,intj):

A(i,j){c=0;}

B(inti,intj,intk):

A(i,j){c=k;}

voiddisplay1()

{

display();

cout<<"c="<

}

private:

intc;

};

intmain()

{

Bb1;

Bb2

(1);

Bb3(1,3);

Bb4(1,3,5);

b1.display1();

b2.display1();

b3.display1();

b4.display1();

return0;

}

输出结果如下:

a=0b=0c=0

a=1b=0c=0

a=1b=3c=0

a=1b=3c=5

8、有以下程序,请完成下面的工作:

①阅读程序,写出运行时输出的结果。

#include

#include

usingnamespacestd;

classA{

public:

A(){cout<<"constrctingA"<

~A(){cout<<"destrctingA"<

};

classB:

publicA

{

public:

B(){cout<<"constrctingB"<

~B(){cout<<"destrctingB"<

};

classC:

publicB

{

public:

C(){cout<<"constrctingC"<

~C(){cout<<"destrctingC"<

};

intmain()

{

Cc1;

return0;

}

输出结果如下:

constrctingA

constrctingB

constrctingC

destrctingC

destrctingB

destrctingA

习题六

4、写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上三只的面积,3个图形的数据在定义对象时给出。

代码如下:

#include

usingnamespacestd;

///基类Shape

classShape

{

public:

voidvirtualprintArea()=0;//纯虚函数

};

//派生类Circle

classCircle:

publicShape

{

public:

Circle(doubler=0):

radius(r){}//构造函数

voidprintArea(){cout<<3.14159*radius*radius<

private:

doubleradius;//数据成员,表示半径

};

//派生类Rectangle

classRectangle:

publicShape

{

public:

Rectangle(doublew,doubleh):

width(w),height(h){}//构造函数

voidprintArea(){cout<

private:

doublewidth;//数据成员,表示宽

doubleheight;//数据成员,表示长

};

//派生类Triangle

classTriangle:

publicShape

{

public:

Triangle(doublew,doubleh):

width(w),height(h){}//构造函数

voidprintArea(){cout<

private:

doublewidth;//数据成员,表示底

doubleheight;//数据成员,表示高

};

//主函数

intmain()

{

Shape*s;

Circlecircle(1.0);

Rectanglerectangle(1.0,1.0);

Triangletriangle(1.0,1.0);

s=&circle;

s->printArea();

s=&rectangle;

s->printArea();

s=▵

s->printArea();

return0;

}

 

(注:

文档可能无法思考全面,请浏览后下载,供参考。

可复制、编制,期待你的好评与关注!

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

当前位置:首页 > 表格模板

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2