C++课后习题第八章第十二章Word文档格式.docx
《C++课后习题第八章第十二章Word文档格式.docx》由会员分享,可在线阅读,更多相关《C++课后习题第八章第十二章Word文档格式.docx(66页珍藏版)》请在冰豆网上搜索。
:
t.minute<
t.sec<
endl;
改:
public:
//成员改为公用的
voidset_time(void){//在main函数之前定义
voidshow_time(void){//在main函数之前定义
2.改写例8.1程序,要求:
(1)将数据成员改为私有的;
(2)将输入和输出的功能改为由成员函数实现;
(3)在类体内定义成员函数;
voidset_time(void){
cin>
hour;
minute;
sec;
}
voidshow_time(void){
cout<
hour<
minute<
sec<
private:
t.set_time();
t.show_time();
3.在第2题的基础上进行如下修改:
在类体内声明成员函数,而在类外定义成员函数。
voidset_time(void);
voidTime:
set_time(void){
show_time(void){
4.在第8.3.3节中分别给出了包含类定义的头文件student.h,包含成员函数定义的源文件studnet.cpp以及包含主函数的源文件main.cpp。
请对该程序完善化,在类中增加一个对数据成员赋初值的成员函数set_value。
原文8.3.3:
classStudent{
voiddisplay(){
num:
num<
name:
name<
sex:
sex<
};
intnum;
charname[20];
charsex;
Studentstud;
stud.display();
main.cpp
#include<
#include"
student.h"
Students;
s.SetValue();
s.Display();
Student.h
voidDisplay();
voidSetValue();
charsex;
Student.cpp
#include"
voidStudent:
Display(){
cout<
"
<
num<
endl;
name<
sex<
SetValue(){
cin>
num>
name>
sex;
5.将例8.4改写为一个多文件的程序:
(1)将类定义放在头文件arraymax.h中;
(2)将成员函数定义放在源文件arrymax.cpp中;
(3)主函数放在源文件file1.cpp中。
原文例8.4:
classArray_max{
voidset_value();
voidmax_value();
voidshow_value();
intarray[10];
intmax;
voidArray_max:
set_value(){
inti;
for(i=0;
i<
10;
i++)
cin>
array[i];
max_value(){
max=array[0];
for(i=1;
if(array[i]>
max)max=array[i];
show_value(){
max="
max<
Array_maxarrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
arraymax.h"
ArrayMaxarrmax;
arrmax.SetValue();
arrmax.MaxValue();
arrmax.ShowMax();
arraymax.h
classArrayMax{
//设置数组元素值
voidMaxValue();
//找出最大值
voidShowMax();
//输出最大值
//整型数组
//数组最大值
arraymax.cpp
voidArrayMax:
for(i=0;
i<
10;
i++){
cin>
array[i];
}
MaxValue(){
max=array[0];
for(i=1;
if(max<
array[i]){
max=array[i];
ShowMax(){
max<
6.需要求3个长方柱的体积,请编写一个基于对象的程序。
数据成员包括length(长)、width(宽)和weight(高)。
要求用成员函数实现以下功能。
(1)由键盘分别输入3个长方柱的长、宽和高。
(2)计算长方柱的体积;
(3)输出3个长方柱的体积。
classBox{
voidget_value();
floatvolume();
voiddisplay();
floatlengh;
floatwidth;
floatheight;
voidBox:
get_value(){
pleaseinputlengh,width,height:
;
lengh;
//由键盘分别输入3个长方柱的长、宽和高。
width;
height;
floatBox:
volume(){
return(lengh*width*height);
//计算长方体体积。
display(){
volume()<
Boxbox1,box2,box3;
//输出3个长方体的体积。
box1.get_value();
volmueofbax1is"
box1.display();
box2.get_value();
volmueofbax2is"
box2.display();
box3.get_value();
volmueofbax3is"
box3.display();
第九章
2.分析下面的程序,写出其运行时的输出结果。
classDate{
Date(int,int,int);
Date(int,int);
Date(int);
Date();
intmonth;
intday;
intyear;
Date:
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;
Date(){
month=1;
voidDate:
month<
/"
day<
year<
Dated1(10,13,2005);
Dated2(12,30);
Dated3(10);
Dated4;
d1.display();
d2.display();
d3.display();
d4.display();
3.如果将第2题程序的第5行改为用默认参数,即Date(int=1;
int=1;
int=2005);
分析程序有无问题。
上机编译,分析出错信息,修改程序使之能通过编译。
要求保留上面一行给出的构造函数,同时能输出与第2题程序相同的输出结果。
Date(int=1,int=1,int=2005);
4.建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5学生的数据。
Student(intn,floats):
num(n),score(s){}
floatscore;
voidStudent:
num="
score="
score<
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;
=2;
p=p+2,i++)
p->
display();
5.建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针做函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
voidmax(Student*);
Student*p=&
stud[0];
max(p);
voidmax(Student*arr){
floatmax_score=arr[0].score;
intk=0;
for(inti=1;
5;
if(arr[i].score>
max_score){
max_score=arr[i].score;
k=i;
arr[k].num<
max_score="
max_score<
6.阅读下面程序,分析其执行过程,写出输出结果
num(n),score(s){}voidchange(intn,floats){
num=n;
score=s;
}voiddisplay(){
Studentstud(101,78.5);
stud.change(101,80.5);
7.将第6题程序分别作一下修改,分析所修改部分的含义以及编译和运行的情况。
(1)将main函数第2行改为constStudentstud(01,78.5);
(2)在
(1)的基础上修改程序,使之能正常运行,用change函数修改数据成员mun和score的值。
(3)将main函数改为
intmain(){
stud;
P->
change(101,80.5);
P-display();
其他部分仍同第6题程序。
(4)在
(2)的基础上将main函数第3行改为constStudent*p=&
(5)再把main函数第3行改为Student*constp=&
(1)
[Error]passing'
constStudent'
as'
this'
argumentof'
display()'
discardsqualifiers[-fpermissive]
change(int,float)'
(2)
voidchange(intn,floats)const{
}voiddisplay()const{
mutableintnum;
mutablefloatscore;
constStudentstud(101,78.5);
(3)
voidchange(intn,floats){
}
voiddisplay(){
p->
(4)
voiddisplay()const{
constStudent*p=&
(5)
num(n