C611章课后作业参考答案.docx

上传人:b****1 文档编号:748787 上传时间:2023-04-30 格式:DOCX 页数:37 大小:85.66KB
下载 相关 举报
C611章课后作业参考答案.docx_第1页
第1页 / 共37页
C611章课后作业参考答案.docx_第2页
第2页 / 共37页
C611章课后作业参考答案.docx_第3页
第3页 / 共37页
C611章课后作业参考答案.docx_第4页
第4页 / 共37页
C611章课后作业参考答案.docx_第5页
第5页 / 共37页
C611章课后作业参考答案.docx_第6页
第6页 / 共37页
C611章课后作业参考答案.docx_第7页
第7页 / 共37页
C611章课后作业参考答案.docx_第8页
第8页 / 共37页
C611章课后作业参考答案.docx_第9页
第9页 / 共37页
C611章课后作业参考答案.docx_第10页
第10页 / 共37页
C611章课后作业参考答案.docx_第11页
第11页 / 共37页
C611章课后作业参考答案.docx_第12页
第12页 / 共37页
C611章课后作业参考答案.docx_第13页
第13页 / 共37页
C611章课后作业参考答案.docx_第14页
第14页 / 共37页
C611章课后作业参考答案.docx_第15页
第15页 / 共37页
C611章课后作业参考答案.docx_第16页
第16页 / 共37页
C611章课后作业参考答案.docx_第17页
第17页 / 共37页
C611章课后作业参考答案.docx_第18页
第18页 / 共37页
C611章课后作业参考答案.docx_第19页
第19页 / 共37页
C611章课后作业参考答案.docx_第20页
第20页 / 共37页
亲,该文档总共37页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C611章课后作业参考答案.docx

《C611章课后作业参考答案.docx》由会员分享,可在线阅读,更多相关《C611章课后作业参考答案.docx(37页珍藏版)》请在冰点文库上搜索。

C611章课后作业参考答案.docx

C611章课后作业参考答案

第六章类和对象

1.给出以下程序的执行结果

题目见《C++语言程序设计》9.9—9.21(P212)和《C++程序设计教程》6.2-6.4(P115)

答案:

《C++语言程序设计》

9.9

运行结果:

String

9.10

运行结果:

n=6

n=6

n=6

9.11

运行结果:

n=10,k=3

n=20,k=3

n=30,k=3

9.13

运行结果:

n=2

n=3

9.14

运行结果:

Constructor,i=0,Destructor

9.15

运行结果:

Constructor1

Constructor2

i=0

i=10

Destructor

Destructor

9.16

运行结果:

AConstructor

BConstructor

Value=0

BDestructor

ADestructor

9.17运行结果:

A=7,b=8

9.18运行结果:

567

9.19运行结果:

Constructor1

Constructor1

Constructor1

Constructor1

Destructor

Constructor2

Destructor

Constructor3

Destructor

x=0,y=0

x=5,y=0

x=2,y=3

Destructor

Destructor

Destructor

9.20运行结果:

(1,2,3):

count=2

(2,3,4):

count=2

9.21运行结果:

A:

Constructor

B:

Constructor

n=3

m=2

B:

Destructor

A:

Destructor

2.编写一个程序,输入若干个学生的英语和数学成绩,求出总分,并按总分从高到低排序,最后输出结果。

#include"iostream"

#include"string"

#include"iomanip"

usingnamespacestd;

classStudent

{

public:

char*name;

inteng,math,sum;

Student();

voidinscore();

voiddisplay();

~Student();

};

Student:

:

Student()

{

name=newchar[10];

}

voidStudent:

:

inscore()

{

cout<<"姓名:

";cin>>name;

cout<<"英语:

";cin>>eng;

cout<<"数学:

";cin>>math;

sum=eng+math;

}

voidStudent:

:

display()

{

cout<

}

Student:

:

~Student()

{

delete[]name;

}

voidequal(Student&a,Student&b)

{

strcpy(a.name,b.name);

a.eng=b.eng;

a.math=b.math;

a.sum=b.sum;

}

voidsort(Student*p,intn)

{

inti,j,exchange;

Studenttmp;

for(i=0;i

{

exchange=0;

for(j=n-2;j>=i;j--)

if(p[j+1].sum>p[j].sum)

{

equal(tmp,p[j+1]);

equal(p[j+1],p[j]);

equal(p[j],tmp);

exchange=1;

}

if(!

exchange)

break;

}

}

voidmain()

{

intn,i;

Student*p;

cout<<"请输入学生个数:

";cin>>n;

p=newStudent[n];

for(i=0;i

p[i].inscore();

cout<<"排序前:

"<

for(i=0;i

p[i].display();

sort(p,n);

cout<<"排序后:

"<

for(i=0;i

p[i].display();

system("pause");

}

3.设计一个立方体类Box,它能提供立方体的体积和表面积。

#include"iostream"

usingnamespacestd;

classBox

{

floata;

floatvolume;

floatarea;

public:

Box(){}

Box(floatr){a=r;}

voidseta(floatr){a=r;}

voidgetvolume(){volume=a*a*a;}

voidgetarea(){area=6*a*a;}

voiddisp()

{

cout<<"体积:

"<

"<

}

};

voidmain()

{

Boxobj1(5),obj2;

obj2.seta(7);

obj1.getarea();

obj1.getvolume();

cout<<"obj1=>";

obj1.disp();

obj2.getarea();

obj2.getvolume();

cout<<"obj2=>";

obj2.disp();

system("pause");

}

4.编写一个程序,已有若干个学生数据,这些数据包括学号、姓名、语文成绩、数学成绩和英语成绩,求各门课程的平均分。

要求设计不同的成员函数来求各门课程的平均分,并使用成员函数指针来调用它们。

#include"iostream"

#include"iomanip"

#include"string"

#defineN3

usingnamespacestd;

classStudent

{

intno;

charname[10];

intchi;

intmath;

inteng;

staticintsum1;

staticintsum2;

staticintsum3;

public:

Student(intn,charna[],intd1,intd2,intd3)

{

no=n;

strcpy(name,na);

chi=d1;math=d2;eng=d3;

sum1+=chi;sum2+=math;sum3+=eng;

}

doubleavg1(){return(sum1*1.0)/N;}

doubleavg2(){return(sum2*1.0)/N;}

doubleavg3(){return(sum3*1.0)/N;}

voiddisp()

{

cout<

}

};

intStudent:

:

sum1=0;

intStudent:

:

sum2=0;

intStudent:

:

sum3=0;

voidmain()

{

double(Student:

:

*fp)();//定义成员函数指针,本部分没讲。

该题可换成其它方法实现

Students1(1,"Li",89,77,98);

Students2(2,"Zhang",98,65,82);

Students3(3,"Mary",67,65,87);

cout<<"输出结果"<

s1.disp();

s2.disp();

s3.disp();

fp=&Student:

:

avg1;

cout<<"语文平均分:

"<<(s1.*fp)()<

fp=&Student:

:

avg2;

cout<<"语文平均分:

"<<(s1.*fp)()<

fp=&Student:

:

avg3;

cout<<"语文平均分:

"<<(s1.*fp)()<

system("pause");

}

5.编写一个程序,统计学生成绩,其功能包括输入学生的姓名和成绩,按成绩从高到低排列打印输出,对前70%的学生定为合格(PASS),而后30%的学生定义不及格(FAIL)。

要求采用面向对象方法编程。

#include"iostream"

#include"iomanip"

#include"string"

#defineN10

usingnamespacestd;

classStudent

{

charname[10];

intdeg;

public:

voidsetname(charna[]){strcpy(name,na);}

char*getname(){returnname;}

voidsetdeg(intd){deg=d;}

intgetdeg(){returndeg;}

};

classCompute

{

intn;

Studentna[N];

public:

voidgetdata()//读入学生的信息

{

inti,tdeg;

chartname[10];

cout<<"学生人数:

";

cin>>n;

for(i=0;i

{

cout<<"第"<

cin>>tname>>tdeg;

na[i].setname(tname);

na[i].setdeg(tdeg);

}

}

voidsort()//对成绩进行排序

{

inti,j,pick;

Studenttemp;

for(i=0;i

{

pick=i;

for(j=i+1;j

{

if(na[j].getdeg()>na[pick].getdeg())

pick=j;

}

temp=na[i];

na[i]=na[pick];

na[pick]=temp;

}

}

voiddisp()

{

intcutoff,i;

cout<<"输出结果"<

cout<<"姓名成绩合格否"<

cout<<"------------------"<

cutoff=n*7/10-1;

for(i=0;i

{

cout<

if(i<=cutoff)

cout<<"PASS"<

else

cout<<"FAIL"<

}

}

};

voidmain()

{

Computeobj;

obj.getdata();

obj.sort();

obj.disp();

system("pause");

}

第七章引用

1.给出以下程序的执行结果

题目见《C++语言程序设计》10.2—10.10(P232)和《C++程序设计教程》7.1-7.2(P130)

《C++语言程序设计》10.2—10.10(P232):

参考答案:

10.2运行结果:

n:

10,rf:

10

n:

15,rf:

15

n:

23,rf:

23

10.3运行结果:

n=15,rf=15

&n=(n的地址),&rf=(rf的地址)

n=10,m=20,rf=20

&n=(n的地址),&m=(m的地址),&rf=(rf的地址)

10.4运行结果:

n=2

d1=0

d2=8

10.5运行结果:

s1=25

s2=64

10.6运行结果:

a=5

y=10

a=8

y=18

10.7运行结果:

63

10.8运行结果:

0,8

10.9运行结果:

1,2

10.10运行结果:

x=1,y=2

x=30,y=40

《C++程序设计教程》7.1-7.2(P130):

7.1

7.2

2.编写一个程序,通过执行结果分析在引用类对象时是否执行类的构造函数与析构函数。

#include

classSample

{

intx,y;

public:

Sample(){cout<<"执行类的构造函数!

"<

~Sample(){cout<<"执行类的析构函数!

"<

};

voidmain()

{

Samples;

cout<<"-----------------"<

Sample&b=s;

}

结果说明:

没有执行类的构造函数与析构函数.

3.编写一个程序,从键盘输入一些数字和字符,编程统计其中数字字符的个数和非数字字符的个数。

#include"iostream"

usingnamespacestd;

voidfun(charch,int&n,int&c)

{

if(ch>='0'&&ch<='9')

n++;

else

c++;

}

voidmain()

{

inttn=0,tc=0;

charch;

cout<<"输入一个字符串";

cin>>ch;

while(ch!

='#')//字符串以#结束

{

fun(ch,tn,tc);

cin>>ch;

}

cout<<"数字字符个数:

"<

cout<<"其它字符个数:

"<

system("pause");

}

第八章友元

1.给出以下程序的执行结果

题目见《C++语言程序设计》11.2—11.5(P243)和《C++程序设计教程》8.1-8.2(P142)

《C++语言程序设计》11.2—11.5参考答案:

11.2运行结果:

A:

:

disp():

b1.num=100

A:

:

disp():

b2.num=200

b1.num=100

b1.num=100

b2.num=200

b2.num=200

11.3运行结果:

n=100

11.4运行结果:

2

11.5运行结果:

x=5,y=10

x=6,y=9

x=5,y=9

《C++程序设计教程》8.1-8.2参考答案:

8.1运行结果:

n=100

8.2运行结果:

thestudentisLiHu

theteacherisWanPing

2.编写一个程序,设计一个点类Point,采用友元函数求两个点之间的距离,并用相关数据进行测试。

#include"iostream"

#include"math.h"

usingnamespacestd;

classPoint

{

protected:

doublex,y;

public:

Point(doublex1,doubley1)

{

x=x1;y=y1;

}

frienddoubledist(Pointp1,Pointp2)

{

doubled=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));

returnd;

}

voiddisp()

{

cout<<"点("<

}

};

voidmain()

{

Pointp1(2,2),p2(3,3);

p1.disp();

cout<<"到";

p2.disp();

cout<<"距离为"<

system("pause");

}

3.有一个学生类student,包括学生姓名、成绩,设计一个友元函数比较两个学生成绩的高低,并求出最高分者和最低分者。

#include"iostream"

#include"string"

usingnamespacestd;

classstudent

{

charname[10];

intdeg;

public:

student(charna[],intd)

{

strcpy(name,na);

deg=d;

}

char*getname(){returnname;}

friendintcompare(student&s1,student&s2)

{

if(s1.deg>s2.deg)

return1;

elseif(s1.deg==s2.deg)

return0;

elsereturn-1;

}

};

voidmain()

{

studentst[]={student("王华",78),student("李明",92),student("张伟",62),student("孙强",88)};

inti,min=0,max=0;

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

{

if(compare(st[max],st[i])==-1)

max=i;

elseif(compare(st[min],st[i])==1)

min=i;

}

cout<<"输出结果:

"<

cout<<"最高分者:

"<

cout<<"最低分者:

"<

system("pause");

}

4.设计一个直线类Line,其中包含3个数据成员,即a、b和c,以及一个求两直线交点的友元函数setpoint和显示数据成员的disp成员函数,并用数据进行测试。

两直线

的交点为(x,y)的计算公式为:

#include

#include

classPoint

{

doublex,y;

public:

Point(){};

Point(doublex1,doubley1)

{

x=x1;y=y1;

}

voiddisp()

{

cout<<"("<

}

};

classLine

{

inta,b,c;

public:

Line(inta1,intb1,intc1)

{

a=a1;b=b1;c=c1;

}

friendPointsetpoint(Linel1,Linel2)

{

doublex=(1.0*l1.b*l2.c-l2.b*l1.c)/(l1.a*l2.b-l2.a*l1.b);

doubley=(1.0*l1.c*l2.a-l2.c*l1.a)/(l1.a*l2.b-l2.a*l1.b);

Pointp(x,y);

returnp;

}

voiddisp()

{

cout<

}

};

voidmain()

{

Pointp;

Linea(2,3,5),b(-3,4,7);

a.disp();

b.disp();

p=setpoint(a,b);

p.disp();

}

第九章运算符重载

1.运算符重载能否创建新的运算符。

不能

2.给出以下程序的执行结果

题目见《C++语言程序设计》12.3—12.7(P273)。

参考答案:

12.3运行结果:

2.5

12.4运行结果:

类赋值

2,3

11,-6

12.5运行结果:

6下标超界

7下标超界

string

6下标超界

7下标超界

length:

6

12.6运行结果:

n=10

n=30

12.7运行结果:

12345678910

3.编写一个程序,采用成员函数运算符重载方式实现复数的四则运算。

并用数据进行测试。

#include"iostream"

usingnamespacestd;

classComplex

{

doublereal,imag;

public:

Complex(){real=imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

Complexoperator+(constComplex&c)

{

returnComplex(real+c.real,imag+c.imag);

}

Complexoperator-(constComplex&c)

{

returnComplex(real-c.real,imag-c.imag);

}

Complexoperator*(constComplex&c)

{

returnComplex(real*c.real-imag*c.imag,real*c.imag+imag*c.real);

}

Complexoperator/(constComplex&c)

{

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

当前位置:首页 > 求职职场 > 简历

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

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