版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第六章 类和对象1给出以下程序的执行结果题目见C+语言程序设计9.99.21(P212)和C+程序设计教程6.2-6.4(P115)答案:C+语言程序设计9.9运行结果:String9.10运行结果:n=6n=6n=69.11运行结果:n=10, k=3n=20, k=3n=30, k=39.13运行结果:n=2n=39.14运行结果:Constructor, i=0,Destructor9.15运行结果:Constructor1Constructor2i=0i=10DestructorDestructor9.16运行结果:A ConstructorB ConstructorValue=0B
2、DestructorA Destructor9.17运行结果:A=7, b=89.18运行结果:5 6 79.19运行结果:Constructor1Constructor1Constructor1Constructor1DestructorConstructor2DestructorConstructor3Destructorx=0, y=0x=5, y=0x=2, y=3DestructorDestructorDestructor9.20运行结果:(1,2,3):count=2(2,3,4):count=29.21运行结果:A: ConstructorB: Constructorn=3m=2
3、B: DestructorA: Destructor2编写一个程序,输入若干个学生的英语和数学成绩,求出总分,并按总分从高到低排序,最后输出结果。#include "iostream"#include "string"#include "iomanip"using namespace std;class Studentpublic:char *name;int eng,math,sum;Student();void inscore();void display();Student();Student:Student() name=ne
4、w char10;void Student:inscore ()cout << "姓名:" cin >> name;cout <<"英语:"cin >> eng;cout << "数学:"cin >> math;sum=eng+math;void Student:display ()cout << setw(10) << name << setw(6) << eng << setw(6) <<
5、 math << setw(10) << sum << endl;Student:Student() delete name;void equal(Student &a,Student &b)strcpy( , );a.eng =b.eng ;a.math =b.math ;a.sum =b.sum ;void sort(Student *p,int n)int i,j,exchange;Student tmp;for (i=0;i<n-1;i+)exchange=0;for (j=n-2;j>=i;j-)
6、if (pj+1.sum >pj.sum )equal(tmp,pj+1);equal(pj+1,pj);equal(pj,tmp);exchange=1;if(!exchange)break;void main()int n,i;Student *p;cout <<"请输入学生个数:" cin >> n;p=new Studentn;for (i=0;i<n;i+)pi.inscore ();cout << "排序前:" <<endl;for (i=0;i<n;i+)pi.display
7、 ();sort(p,n);cout << "排序后:" << endl;for (i=0;i<n;i+)pi.display ();system("pause");3设计一个立方体类Box,它能提供立方体的体积和表面积。#include "iostream"using namespace std;class Boxfloat a;float volume;float area;public:Box() Box(float r) a=r;void seta(float r) a=r;void getvol
8、ume() volume=a*a*a;void getarea() area=6*a*a;void disp()cout << "体积:" << volume << ",表面积:" << area << endl;void main()Box obj1(5),obj2;obj2.seta (7);obj1.getarea ();obj1.getvolume ();cout << "obj1=>"obj1.disp ();obj2.getarea ();obj
9、2.getvolume ();cout << "obj2=>"obj2.disp ();system("pause");4编写一个程序,已有若干个学生数据,这些数据包括学号、姓名、语文成绩、数学成绩和英语成绩,求各门课程的平均分。要求设计不同的成员函数来求各门课程的平均分,并使用成员函数指针来调用它们。#include "iostream"#include "iomanip"#include "string"#define N 3using namespace std;clas
10、s Studentint no;char name10;int chi;int math;int eng;static int sum1;static int sum2;static int sum3;public:Student(int n,char na, int d1,int d2,int d3)no=n;strcpy(name,na);chi=d1;math=d2;eng=d3;sum1+=chi;sum2+=math;sum3+=eng;double avg1() return(sum1*1.0)/N;double avg2() return(sum2*1.0)/N;double a
11、vg3() return(sum3*1.0)/N;void disp()cout << setw(4) << no << setw(10) << name << setw(6) << chi << setw(6) << math << setw(6) << eng << endl;int Student:sum1 =0;int Student:sum2 =0;int Student:sum3 =0;void main()double (Student:*fp)()
12、; /定义成员函数指针,本部分没讲。该题可换成其它方法实现Student s1(1,"Li",89,77,98);Student s2(2,"Zhang",98,65,82);Student s3(3,"Mary",67,65,87);cout << "输出结果" << endl;s1.disp ();s2.disp ();s3.disp ();fp=&Student:avg1 ;cout << "语文平均分:" << (s1.*fp)()
13、 << endl;fp=&Student:avg2 ;cout << "语文平均分:" << (s1.*fp)() << endl;fp=&Student:avg3 ;cout << "语文平均分:" << (s1.*fp)() << endl;system("pause");5编写一个程序,统计学生成绩,其功能包括输入学生的姓名和成绩,按成绩从高到低排列打印输出,对前70%的学生定为合格(PASS),而后30%的学生定义不及格(FAI
14、L)。要求采用面向对象方法编程。#include "iostream"#include "iomanip"#include "string"#define N 10using namespace std;class Studentchar name10;int deg;public:void setname(char na) strcpy(name,na);char *getname() return name;void setdeg(int d) deg=d;int getdeg() return deg;class Compute
15、int n;Student naN;public:void getdata() /读入学生的信息int i,tdeg;char tname10;cout << "学生人数:"cin >> n;for (i=0;i<n;i+)cout << "第" << i+1 << "个学生的姓名和成绩"cin >> tname >> tdeg;nai.setname (tname);nai.setdeg (tdeg);void sort() /对成绩进行排序i
16、nt i,j,pick;Student temp;for (i=0;i<n-1;i+)pick=i;for(j=i+1;j<n;j+)if (naj.getdeg () > napick.getdeg ()pick=j;temp=nai;nai=napick;napick=temp;void disp()int cutoff,i;cout << "输出结果" << endl;cout << "姓名 成绩 合格否" << endl;cout << "-" &l
17、t;< endl;cutoff=n*7/10-1;for(i=0;i<n;i+)cout << setw(6) << nai.getname () << setw(3) << nai.getdeg ();if(i<=cutoff)cout << " PASS" << endl;elsecout << " FAIL" << endl;void main()Compute obj;obj.getdata ();obj.sort ();obj.di
18、sp ();system("pause");第七章 引用1给出以下程序的执行结果题目见C+语言程序设计10.210.10(P232)和C+程序设计教程7.1-7.2(P130)C+语言程序设计10.210.10(P232):参考答案:10.2运行结果:n :10, rf:10n:15, rf:15n:23,rf:2310.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 d
19、1=0 d2=810.5运行结果: s1=25 s2=6410.6运行结果: a=5 y=10 a=8 y=1810.7运行结果:6310.8运行结果:0, 810.9运行结果:1, 210.10运行结果:x=1, y=2 x=30, y=40C+程序设计教程7.1-7.2(P130):71722.编写一个程序,通过执行结果分析在引用类对象时是否执行类的构造函数与析构函数。#include <iostream.h>class Sampleint x,y;public:Sample() cout <<"执行类的构造函数!"<< endl;S
20、ample() cout << "执行类的析构函数!"<< endl;void main()Sample s;cout << "-" << endl;Sample &b=s;结果说明:没有执行类的构造函数与析构函数3.编写一个程序,从键盘输入一些数字和字符,编程统计其中数字字符的个数和非数字字符的个数。#include "iostream"using namespace std;void fun(char ch,int &n,int &c)if (ch >=
21、'0' && ch <= '9')n+;elsec+;void main()int tn=0,tc=0;char ch;cout << "输入一个字符串"cin >> ch;while(ch!='#') /字符串以#结束fun(ch,tn,tc);cin >> ch;cout << "数字字符个数:"<< tn << endl;cout << "其它字符个数:" << t
22、c << endl;system("pause");第八章 友元1给出以下程序的执行结果题目见C+语言程序设计11.211.5(P243)和C+程序设计教程8.1-8.2(P142)C+语言程序设计11.211.5参考答案:11.2运行结果: A: disp(): b1.num=100 A: disp(): b2.num=200 b1.num=100 b1.num=100 b2.num=200 b2.num=20011.3运行结果:n=10011.4运行结果:211.5运行结果: x=5, y=10 x=6, y=9 x=5, y=9C+程序设计教程8.1-8.
23、2参考答案:8.1运行结果:n=1008.2运行结果:the student is Li Hu the teacher is Wan Ping2.编写一个程序,设计一个点类Point,采用友元函数求两个点之间的距离,并用相关数据进行测试。#include "iostream"#include "math.h"using namespace std;class Pointprotected:double x,y;public:Point(double x1,double y1)x=x1;y=y1;friend double dist(Point p1,Po
24、int p2)double d=sqrt(p1.x -p2.x)*(p1.x-p2.x)+(p1.y -p2.y)*(p1.y-p2.y);return d;void disp()cout << "点(" << x << "," << y << ")"void main()Point p1(2,2),p2(3,3);p1.disp ();cout << "到"p2.disp ();cout << "距离为"<
25、;< dist(p1,p2)<<endl;system("pause");3.有一个学生类student,包括学生姓名、成绩,设计一个友元函数比较两个学生成绩的高低,并求出最高分者和最低分者。#include "iostream"#include "string"using namespace std;class studentchar name10;int deg;public:student(char na,int d)strcpy(name,na);deg=d;char *getname() return na
26、me;friend int compare(student &s1,student &s2)if (s1.deg > s2.deg )return 1;else if (s1.deg = s2.deg )return 0;else return -1;void main()student st=student("王华",78), student("李明",92),student("张伟",62), student("孙强",88);int i,min=0,max=0;for (i=1;i<
27、;4;i+)if(compare(stmax,sti)=-1)max=i;else if(compare(stmin,sti)=1)min=i;cout <<"输出结果:" << endl;cout << " 最高分者:" << stmax.getname () << endl;cout << " 最低分者:" << stmin.getname () << endl;system("pause");4.设计一个直线类Li
28、ne,其中包含3个数据成员,即a、b和c,以及一个求两直线交点的友元函数setpoint和显示数据成员的disp成员函数,并用数据进行测试。两直线的交点为(x,y)的计算公式为:#include <iostream.h>#include <math.h>class Pointdouble x,y;public:Point() ;Point(double x1,double y1)x=x1;y=y1;void disp()cout << "(" << x << "," << y <
29、;< ")" << endl;class Lineint a,b,c;public:Line(int a1,int b1,int c1)a=a1;b=b1;c=c1;friend Point setpoint(Line l1, Line l2)double x=(1.0 * l1.b *l2.c -l2.b *l1.c )/(l1.a *l2.b -l2.a *l1.b );double y=(1.0* l1.c *l2.a - l2.c *l1.a )/(l1.a *l2.b -l2.a *l1.b );Point p(x,y);return p;voi
30、d disp()cout << a << "x*x+" << b << "x+" << c << "=0" << endl;void main()Point p;Line a(2,3,5), b(-3,4,7);a.disp ();b.disp ();p=setpoint(a,b);p.disp ();第九章 运算符重载1.运算符重载能否创建新的运算符。 不能2给出以下程序的执行结果题目见C+语言程序设计12.312.7(P273)。参考答案:12.3
31、运行结果:2.512.4运行结果:类赋值2,311,-612.5运行结果:6下标超界7下标超界string6下标超界7下标超界length:612.6运行结果:n=10 n=3012.7运行结果:1 2 3 4 5 6 7 8 9 103.编写一个程序,采用成员函数运算符重载方式实现复数的四则运算。并用数据进行测试。#include "iostream"using namespace std;class Complexdouble real,imag;public:Complex() real=imag=0;Complex(double r,double i) real=r
32、;imag=i;Complex operator +(const Complex &c)return Complex(real+c.real ,imag+c.imag );Complex operator -(const Complex &c)return Complex(real-c.real ,imag-c.imag );Complex operator *(const Complex &c)return Complex(real*c.real -imag*c.imag ,real*c.imag +imag*c.real );Complex operator /(c
33、onst Complex &c)return Complex(real*c.real +imag*c.imag )/(c.real *c.real +c.imag *c.imag ),(imag*c.real -real*c.imag )/(c.real *c.real +c.imag *c.imag );void disp()if(imag <0)cout << real << imag << "i" << endl;elsecout << real << "+" &
34、lt;< imag << "i" << endl;void main()Complex c1(1,2),c2(4,5);Complex c3;c3=c1+c2;cout << "c1+c2="c3.disp();c3=c1-c2;cout << "c1-c2="c3.disp();c3=c1*c2;cout << "c1*c2="c3.disp();c3=c1/c2;cout << "c1/c2="c3.disp();
35、system("pause");4. 编写一个程序,采用友元函数运算符重载方式实现复数的四则运算。并用数据进行测试。5.设计一个日期类Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,如一日期加上天数、一日期减去天数、两日期相差的天数等。#include "iostream"using namespace std;static int dys=31,28,31,30,31,30,31,31,30,31,30,31;class dateint year,month,day;public:date() date(int y,int m,int
36、d)year=y;month=m;day=d;void disp()cout << year << "年"<< month << "月" << day <<"日" << endl; friend date operator+ (date d, int day) /运算符重载友元函数date dt = d;day = dt.day +day;while (day > dysdt.month -1) /对月和年的计算day= day - dysdt.
37、month-1;dt.month +;if (dt.month = 13)dt.month=1;dt.year +;dt.day = day;return dt; date operator - (int day) /运算符重载成员函数date dt=*this;day=dt.day -day;while (day <=0) /如果,说明已减到上一个月day+=dysdt.month -1; /得到对应的上个月的某号dt.month -;if (dt.month = 0) /若为,说明已减到上一年dt.month =12;dt.year -;dt.day =day;return dt;v
38、oid main()date d1(2003,11,25),d2,d3;d1.disp ();d2=d1-1;d2.disp ();d3=d1+10;d3.disp ();system("pause");第10章 模板1给出以下程序的执行结果题目见C+语言程序设计13.313.7(P290)。参考答案:13.3运行结果:d=8, i=8 D=2.5, I =123413.4运行结果:n=13.813.5运行结果:n=3013.6运行结果:n=10 13.7运行结果:n=162.设计一个函数模板max<T>求一个数组中最大的元素,并以整数数组和字符数组进行调用,并
39、采用相关数据进行测试。#include <iostream.h>template <class T>T max(T a,int n)T temp=a0;for (int i=1;i<n;i+)if (temp<ai)temp = ai;return temp;void main()int a=6,2,5,8,7;char b='a','b','c','d','e'cout << "a中最大值:" << max(a,5) <<
40、 endl;cout << "b中最大值:" << max(b,5) << endl;3.设计一个类模板store<T>用于存储某一类型的数据,并以整数和字符串进行实例化。#include <iostream.h>template <class T>class store /声明了类模板store<T>T item;public:T getitem()return item;void putitem(T x)item=x;void main()int n=10;store<int>
41、; s1; /类模板的实例化s1.putitem (n);cout << "n=" << s1.getitem () << endl;char str="string"store<char *> s2;s2.putitem (str);cout << "str=" << s2.getitem () << endl;4.设计一个”operator=”函数模板,用于比较各类型的数据是否相等。#include <iostream.h>templa
42、te <class T>int operator=(T x,T y)if(x=y) return 1;else return 0;void main()int a=2,b=5;if (a=b) cout << a<< "和"<< b << "相等"<<endl;else cout << a<< "和"<< b <<"不相等"<<endl;if ('a'='a
43、39;) cout << 'a'<< "和"<< 'a' << "相等"<<endl;else cout << 'a'<< "和"<< 'a' <<"不相等"<<endl;第11章 派生和继承1 结出以下程序的执行结果C+语言程序设计P32914.4运行结果: constructing A class constructing sub
44、 class destructing sub class destructing A class14.5运行结果: constructing A class n=1 constructing A class n=3 constructing sub class m=2 destructing sub class destructing A class destructing A class14.6运行结果:(虚基类) x=0, y =100 x=0, z=300 m=500 x=400, y=100 x=400,z=300 m=50014.10运行结果: x=3, y=5 x=4, y=5 x=2, y=314.11运行结果: constructing class A constructing class B1 constructing clas
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年山西机电职业技术学院单招综合素质笔试备考试题含详细答案解析
- 2026年广西英华国际职业学院单招综合素质考试备考试题含详细答案解析
- 2026辽宁铁岭市市本级3家单位补充招聘公益性岗位工作人员5人笔试备考试题及答案解析
- 2026年上海外国语大学贤达经济人文学院单招综合素质笔试参考题库含详细答案解析
- 2026上半年天津事业单位统考东丽区招聘20人笔试备考题库及答案解析
- 4.6.4 激素调节教学设计(2025-2026学年人教版2024八年级上册生物)
- 2026年宁德福安市赛岐镇建设投资开发有限公司招聘1人笔试备考试题及答案解析
- 2026新疆八团连队特聘农技员招募12人笔试备考题库及答案解析
- 2026江西南昌新建区晨鸣专项招聘笔试备考试题及答案解析
- 2026江西南昌市劳动保障事务代理中心派遣制收费员招聘2人笔试备考试题及答案解析
- 2025年重庆高考物理试卷试题真题及答案详解(精校打印)
- 土改田施工方案(3篇)
- 河北省衡水中学2026届数学高一上期末质量跟踪监视模拟试题含解析
- 安乐死协议书模板
- 2026内蒙古自治区行政执法人员专场招收1991人参考笔试试题及答案解析
- 断路器绝缘电阻试验方法
- 智能机械臂路径规划算法的创新探索
- 成自铁路成都罗家湾牵引站220千伏供电工程环境影响报告表
- 作业人员安全管理档案
- 开票税点自动计算器
- 2020泰和安TS-C-6001AG TS-C-6001AT应急照明控制器安装使用说明书 Ver.1.0,2020.09
评论
0/150
提交评论