C++课后习题第八章第十二章.docx

上传人:b****6 文档编号:5809882 上传时间:2023-01-01 格式:DOCX 页数:66 大小:33.70KB
下载 相关 举报
C++课后习题第八章第十二章.docx_第1页
第1页 / 共66页
C++课后习题第八章第十二章.docx_第2页
第2页 / 共66页
C++课后习题第八章第十二章.docx_第3页
第3页 / 共66页
C++课后习题第八章第十二章.docx_第4页
第4页 / 共66页
C++课后习题第八章第十二章.docx_第5页
第5页 / 共66页
点击查看更多>>
下载资源
资源描述

C++课后习题第八章第十二章.docx

《C++课后习题第八章第十二章.docx》由会员分享,可在线阅读,更多相关《C++课后习题第八章第十二章.docx(66页珍藏版)》请在冰豆网上搜索。

C++课后习题第八章第十二章.docx

C++课后习题第八章第十二章

第八章

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

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

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

原文:

#include

usingnamespacestd;

classTime{

voidset_time(void);

voidshow_time(void);

inthour;

intminute;

intsec;

};

Timet;

intmain(){

set_time();

show_time();

return0;

}

voidset_time(void){

cin>>t.hour;

cin>>t.minute;

cin>>t.sec;

}

voidshow_time(void){

cout<

"<

"<

}

改:

#include

usingnamespacestd;

classTime{

public:

//成员改为公用的

inthour;

intminute;

intsec;

};

Timet;

voidset_time(void){//在main函数之前定义

cin>>t.hour;

cin>>t.minute;

cin>>t.sec;

}

voidshow_time(void){//在main函数之前定义

cout<

"<

"<

}

intmain(){

set_time();

show_time();

return0;

}

2.改写例8.1程序,要求:

(1)将数据成员改为私有的;

(2)将输入和输出的功能改为由成员函数实现;

(3)在类体内定义成员函数;

#include

usingnamespacestd;

classTime{

public:

voidset_time(void){

cin>>hour;

cin>>minute;

cin>>sec;

}

voidshow_time(void){

cout<

"<

"<

}

private:

inthour;

intminute;

intsec;

};

Timet;

intmain(){

t.set_time();

t.show_time();

return0;

}

3.在第2题的基础上进行如下修改:

在类体内声明成员函数,而在类外定义成员函数。

#include

usingnamespacestd;

classTime{

public:

voidset_time(void);

voidshow_time(void);

private:

inthour;

intminute;

intsec;

};

voidTime:

:

set_time(void){

cin>>hour;

cin>>minute;

cin>>sec;

}

voidTime:

:

show_time(void){

cout<

"<

"<

}

Timet;

intmain(){

t.set_time();

t.show_time();

return0;

}

4.在第8.3.3节中分别给出了包含类定义的头文件student.h,包含成员函数定义的源文件studnet.cpp以及包含主函数的源文件main.cpp。

请对该程序完善化,在类中增加一个对数据成员赋初值的成员函数set_value。

原文8.3.3:

#include

usingnamespacestd;

classStudent{

public:

voiddisplay(){

cout<<"num:

"<

cout<<"name:

"<

cout<<"sex:

"<

};

private:

intnum;

charname[20];

charsex;

};

intmain(){

Studentstud;

stud.display();

return0;

}

改:

main.cpp

#include

#include"student.h"

usingnamespacestd;

intmain(){

Students;

s.SetValue();

s.Display();

return0;

}

Student.h

usingnamespacestd;

classStudent{

public:

voidDisplay();

voidSetValue();

private:

intnum;

charname[20];

charsex;

};

Student.cpp

#include

#include"student.h"

usingnamespacestd;

voidStudent:

:

Display(){

cout<<"num:

"<

cout<<"name:

"<

cout<<"sex:

"<

}

voidStudent:

:

SetValue(){

cin>>num>>name>>sex;

}

5.将例8.4改写为一个多文件的程序:

(1)将类定义放在头文件arraymax.h中;

(2)将成员函数定义放在源文件arrymax.cpp中;

(3)主函数放在源文件file1.cpp中。

原文例8.4:

#include

usingnamespacestd;

classArray_max{

public:

voidset_value();

voidmax_value();

voidshow_value();

private:

intarray[10];

intmax;

};

voidArray_max:

:

set_value(){

inti;

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

cin>>array[i];

}

voidArray_max:

:

max_value(){

inti;

max=array[0];

for(i=1;i<10;i++)

if(array[i]>max)max=array[i];

}

voidArray_max:

:

show_value(){

cout<<"max="<

}

intmain(){

Array_maxarrmax;

arrmax.set_value();

arrmax.max_value();

arrmax.show_value();

return0;

}

改:

main.cpp

#include

#include"arraymax.h"

usingnamespacestd;

intmain(){

ArrayMaxarrmax;

arrmax.SetValue();

arrmax.MaxValue();

arrmax.ShowMax();

return0;

}

arraymax.h

classArrayMax{

public:

voidSetValue();//设置数组元素值

voidMaxValue();//找出最大值

voidShowMax();//输出最大值

private:

intarray[10];//整型数组

intmax;//数组最大值

};

arraymax.cpp

#include

#include"arraymax.h"

usingnamespacestd;

voidArrayMax:

:

SetValue(){

inti;

for(i=0;i<10;i++){

cin>>array[i];

}

}

voidArrayMax:

:

MaxValue(){

inti;

max=array[0];

for(i=1;i<10;i++){

if(max

max=array[i];

}

}

}

voidArrayMax:

:

ShowMax(){

cout<<"max="<

}

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

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

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

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

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

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

#include

usingnamespacestd;

classBox{

public:

voidget_value();

floatvolume();

voiddisplay();

public:

floatlengh;

floatwidth;

floatheight;

};

voidBox:

:

get_value(){

cout<<"pleaseinputlengh,width,height:

";

cin>>lengh;//由键盘分别输入3个长方柱的长、宽和高。

cin>>width;

cin>>height;

}

floatBox:

:

volume(){

return(lengh*width*height);//计算长方体体积。

}

voidBox:

:

display(){

cout<

}

intmain(){

Boxbox1,box2,box3;//输出3个长方体的体积。

box1.get_value();

cout<<"volmueofbax1is";

box1.display();

box2.get_value();

cout<<"volmueofbax2is";

box2.display();

box3.get_value();

cout<<"volmueofbax3is";

box3.display();

return0;

}

 

第九章

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

#include

usingnamespacestd;

classDate{

public:

Date(int,int,int);

Date(int,int);

Date(int);

Date();

voiddisplay();

private:

intmonth;

intday;

intyear;

};

Date:

:

Date(intm,intd,inty):

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

Date:

:

Date(intm,intd):

month(m),day(d){

year=2005;

}

Date:

:

Date(intm):

month(m){

day=1;

year=2005;

}

Date:

:

Date(){

month=1;

day=1;

year=2005;

}

voidDate:

:

display(){

cout<

}

intmain(){

Dated1(10,13,2005);

Dated2(12,30);

Dated3(10);

Dated4;

d1.display();

d2.display();

d3.display();

d4.display();

return0;

}

3.如果将第2题程序的第5行改为用默认参数,即Date(int=1;int=1;int=2005);分析程序有无问题。

上机编译,分析出错信息,修改程序使之能通过编译。

要求保留上面一行给出的构造函数,同时能输出与第2题程序相同的输出结果。

改:

#include

usingnamespacestd;

classDate{

public:

Date(int=1,int=1,int=2005);

voiddisplay();

private:

intmonth;

intday;

intyear;

};

Date:

:

Date(intm,intd,inty):

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

voidDate:

:

display(){

cout<

}

intmain(){

Dated1(10,13,2005);

Dated2(12,30);

Dated3(10);

Dated4;

d1.display();

d2.display();

d3.display();

d4.display();

return0;

}

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

#include

usingnamespacestd;

classStudent{

public:

Student(intn,floats):

num(n),score(s){}

voiddisplay();

private:

intnum;

floatscore;

};

voidStudent:

:

display(){

cout<<"num="<

}

intmain(){

Studentstud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5),

Student(104,100.0),Student(105,95.5)

};

Student*p=stud;

for(inti=0;i<=2;p=p+2,i++)

p->display();

return0;

}

5.建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针做函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。

#include

usingnamespacestd;

classStudent{

public:

Student(intn,floats):

num(n),score(s){}

intnum;

floatscore;

};

intmain(){

Studentstud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5),

Student(104,100.0),Student(105,95.5)

};

voidmax(Student*);

Student*p=&stud[0];

max(p);

return0;

}

voidmax(Student*arr){

floatmax_score=arr[0].score;

intk=0;

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

if(arr[i].score>max_score){

max_score=arr[i].score;

k=i;

}

cout<<"num="<

}

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;

}

7.将第6题程序分别作一下修改,分析所修改部分的含义以及编译和运行的情况。

(1)将main函数第2行改为constStudentstud(01,78.5);

(2)在

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

(3)将main函数改为

intmain(){

Studentstud(101,78.5);

Student*p=&stud;

P->display();

P->change(101,80.5);

P-display();

return0;

}

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

(4)在

(2)的基础上将main函数第3行改为constStudent*p=&stud;

(5)再把main函数第3行改为Student*constp=&stud;

(1)

[Error]passing'constStudent'as'this'argumentof'voidStudent:

:

display()'discardsqualifiers[-fpermissive]

[Error]passing'constStudent'as'this'argumentof'voidStudent:

:

change(int,float)'discardsqualifiers[-fpermissive]

[Error]passing'constStudent'as'this'argumentof'voidStudent:

:

display()'discardsqualifiers[-fpermissive]

(2)

#include

usingnamespacestd;

classStudent{

public:

Student(intn,floats):

num(n),score(s){}

voidchange(intn,floats)const{

num=n;

score=s;

}voiddisplay()const{

cout<

}

private:

mutableintnum;

mutablefloatscore;

};

intmain(){

constStudentstud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return0;

}

(3)

#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);

Student*p=&stud;

p->display();

p->change(101,80.5);

p->display();

return0;

}

(4)

#include

usingnamespacestd;

classStudent{

public:

Student(intn,floats):

num(n),score(s){}

voidchange(intn,floats){

num=n;

score=s;

}

voiddisplay()const{

cout<

}

private:

intnum;

floatscore;

};

intmain(){

Studentstud(101,78.5);

constStudent*p=&stud;

p->display();

stud.change(101,80.5);

p->display();

return0;

}

(5)

#include

usingnamespacestd;

classStudent{

public:

Student(intn,floats):

num(n

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

当前位置:首页 > 经管营销

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

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