C++611章课后作业参考答案.docx
- 文档编号:8707414
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:24
- 大小:25.96KB
C++611章课后作业参考答案.docx
《C++611章课后作业参考答案.docx》由会员分享,可在线阅读,更多相关《C++611章课后作业参考答案.docx(24页珍藏版)》请在冰豆网上搜索。
C++611章课后作业参考答案
第六章类和对象
1.给出以下程序的执行结果
题目见《C++语言程序设计》—(P212)和《C++程序设计教程》(P115)
答案:
《C++语言程序设计》
运行结果:
String
运行结果:
n=6
n=6
n=6
运行结果:
n=10,k=3
n=20,k=3
n=30,k=3
运行结果:
n=2
n=3
运行结果:
Constructor,i=0,Destructor
运行结果:
Constructor1
Constructor2
i=0
i=10
Destructor
Destructor
运行结果:
AConstructor
BConstructor
Value=0
BDestructor
ADestructor
运行结果:
A=7,b=8
运行结果:
567
运行结果:
Constructor1
Constructor1
Constructor1
Constructor1
Destructor
Constructor2
Destructor
Constructor3
Destructor
x=0,y=0
x=5,y=0
x=2,y=3
Destructor
Destructor
Destructor
运行结果:
(1,2,3):
count=2
(2,3,4):
count=2
运行结果:
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,); =; =; =; } 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; (7); (); (); cout<<"obj1=>"; (); (); (); cout<<"obj2=>"; (); 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*/N;} doubleavg2(){return(sum2*/N;} doubleavg3(){return(sum3*/N;} voiddisp() { cout< } }; intStudent: : sum1=0; intStudent: : sum2=0; intStudent: : sum3=0; voidmain() { double(Student: : *fp)();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()etname(tname); na[i].setdeg(tdeg); } } voidsort()etdeg()>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; (); (); (); system("pause"); } 第七章引用 1.给出以下程序的执行结果 题目见《C++语言程序设计》—(P232)和《C++程序设计教程》(P130) 《C++语言程序设计》—(P232): 参考答案: 运行结果: n: 10,rf: 10 n: 15,rf: 15 n: 23,rf: 23 运行结果: n=15,rf=15 &n=(n的地址),&rf=(rf的地址) n=10,m=20,rf=20 &n=(n的地址),&m=(m的地址),&rf=(rf的地址) 运行结果: n=2 d1=0 d2=8 运行结果: s1=25 s2=64 运行结果: a=5 y=10 a=8 y=18 运行结果: 63 运行结果: 0,8 运行结果: 1,2 运行结果: x=1,y=2 x=30,y=40 《C++程序设计教程》(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! ='#')写一个程序,设计一个点类Point,采用友元函数求两个点之间的距离,并用相关数据进行测试。 #include"iostream" #include"" usingnamespacestd; classPoint { protected: doublex,y; public: Point(doublex1,doubley1) { x=x1;y=y1; } frienddoubledist(Pointp1,Pointp2) { doubled=sqrt(**returnd; } voiddisp() { cout<<"点("< } }; voidmain() { Pointp1(2,2),p2(3,3); (); cout<<"到"; (); 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>) return1; elseif==) 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=***)/**); doubley=**-*)/**); Pointp(x,y); returnp; } voiddisp() { cout< } }; voidmain() { Pointp; Linea(2,3,5),b(-3,4,7); (); (); p=setpoint(a,b); (); } 第八章运算符重载 1.运算符重载能否创建新的运算符。 不能 2.给出以下程序的执行结果 题目见《C++语言程序设计》—(P273)。 参考答案: 运行结果: 运行结果: 类赋值 2,3 11,-6 运行结果: 6下标超界 7下标超界 string 6下标超界 7下标超界 length: 6 运行结果: n=10 n=30 运行结果: 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+,imag+); } Complexoperator-(constComplex&c) { returnComplex,); } Complexoperator*(constComplex&c) { returnComplex(real*-imag*,real*+imag*); } Complexoperator/(constComplex&c) { returnComplex((real*+imag*)/*+*),(imag*-real*)/*+*)); } voiddisp() { if(imag<0) cout< else cout< } }; voidmain() { Complexc1(1,2),c2(4,5); Complexc3; c3=c1+c2; cout<<"c1+c2="; (); c3=c1-c2; cout<<"c1-c2="; (); c3=c1*c2; cout<<"c1*c2="; (); c3=c1/c2; cout<<"c1/c2="; (); system("pause"); } 4.编写一个程序,采用友元函数运算符重载方式实现复数的四则运算。 并用数据进行测试。 5.设计一个日期类Date,包括年、月、日等私有数据成员。 要求实现日期的基本运算,如一日期加上天数、一日期减去天数、两日期相差的天数等。 #include"iostream" usingnamespacestd; staticintdys[]={31,28,31,30,31,30,31,31,30,31,30,31}; classdate { intyear,month,day; public: date(){} date(inty,intm,intd) { year=y; month=m; day=d; } voiddisp() { cout< } frienddateoperator+(dated,intday)计一个函数模板max #include<> template Tmax(Ta[],intn) { Ttemp=a[0]; for(inti=1;i if(temp temp=a[i]; returntemp; } voidmain() { inta[]={6,2,5,8,7}; charb[]={'a','b','c','d','e'}; cout<<"a中最大值: "< cout<<"b中最大值: "< } 3.设计一个类模板store #include<> template classstore计一个”operator==”函数模板,用于比较各类型的数据是否相等。 #include<> template intoperator==(Tx,Ty) { if(x==y)return1; elsereturn0; } voidmain() { inta=2,b=5; if(a==b) cout< elsecout< if('a'=='a') cout<<'a'<<"和"<<'a'<<"相等"< elsecout<<'a'<<"和"<<'a'<<"不相等"< } 第10章派生和继承 1.结出以下程序的执行结果
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 611章课后作业参考答案 611 课后 作业 参考答案
![提示](https://static.bdocx.com/images/bang_tan.gif)