实验4 多结构及静态成员和对象数组的使用.doc

上传人:新** 文档编号:561395948 上传时间:2023-12-23 格式:DOC 页数:7 大小:84.50KB
返回 下载 相关 举报
实验4 多结构及静态成员和对象数组的使用.doc_第1页
第1页 / 共7页
实验4 多结构及静态成员和对象数组的使用.doc_第2页
第2页 / 共7页
实验4 多结构及静态成员和对象数组的使用.doc_第3页
第3页 / 共7页
实验4 多结构及静态成员和对象数组的使用.doc_第4页
第4页 / 共7页
实验4 多结构及静态成员和对象数组的使用.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《实验4 多结构及静态成员和对象数组的使用.doc》由会员分享,可在线阅读,更多相关《实验4 多结构及静态成员和对象数组的使用.doc(7页珍藏版)》请在金锄头文库上搜索。

1、实验4 多文件结构及静态成员和对象数组的使用一、实验目的1、掌握自定义头文件的方法;2、学会建立和调试多文件程序;3、了解静态成员的使用;4、掌握对象数组的使用。二、实验内容1、编写一个函数,求数列运算中从n个不同的数中取r个数的所有选择的个数。要求:(1)将main()函数放在一个文件中;(2)将另外两个函数fn(int n) ,Cnr(int n, int r)放在另一个文件中;(3)将函数原型说明放在一个头文件中;(4)建立一个项目,将这三个文件加到你的项目中,编译连接使你的程序正常运行。2、设计一个Employee类,并在Employee类中增加一个静态数据成员来设置本公司员工编号基数

2、,新增加的员工编号将在创建对象的同时自动在基数上增加。另外,将Employee类的声明部分和实现部分分成两个文件来实现。编程实现之。3、设计一个点类point,该类具有两个实数坐标。希望主程序使用这个类完成下述功能:(l)主程序为类point申请10个连续存储空间。(2)要求调用一个函数Set()从键盘输入10个对象的属性,并顺序存入申请的内存中。(3)要求调用一个函数Display()显示10个对象的值。(4)要求调用一个函数Lenth(),计算将这些点连成一条折线时,这条折线的长度。(5)程序结束时,删除申请的内存。(6)演示析构对象(动态对象或堆对象)的执行顺序。设计这个类和各个函数并验

3、证运算结果的正确性。三实验源代码1./以下是cnr.h#ifndef cnr_h#define cnr_hint cnr(int ,int );#endif/以下是cnr.cpp#include cnr.h;int cnr(int n,int r)if(n=r)return 1;else if(r=1)return n;else return cnr(n-1,r)+cnr(n-1,r-1);/以下是main.cpp#include#includecnr.h;void main()int n,r;coutPlease input n,r:nr;coutC(n,r)为cnr(n,r)endl;2.

4、/以下是employee.h#ifndef employee_h#define employee_hclass employeepublic:employee(int,char *,char,int);employee();void setemployee(int,char*,char,int);void print();private:static int basenum;int num;char * name;char sex;int wage;#endif/以下是employee.cpp#include employee.h#include#include#includeint empl

5、oyee:basenum=10;employee:employee(int nu,char * na,char s,int w)name=new charstrlen(na)+1;assert(name!=0);strcpy(name,na);num=nu+basenum;sex=s;wage=w;employee:employee()delete name;void employee:setemployee(int nu,char *na,char s,int w)delete name;name=new charstrlen(na)+1;assert(name!=0);strcpy(nam

6、e,na);num=nu+basenum;sex=s;wage=w;void employee:print()coutnum:numtname:nameendlsex:sextwage:wageendl; /以下是main.cpp#include#includeemployee.h;void main()employee num1(2,oo,q,5000),num2(3,rr,q,10000);coutnum1:endl;num1.print();coutnum2:endl;num2.print();num1.setemployee(2,pp,a,1000);cout(after set) n

7、um1:endl;num1.print();3./以下是point.h#ifndef point_h#define point_hclass pointpublic:point(int=0,int=0);point();void set();void display();double length(const point &);private:int x;int y;#endif/以下是point.cpp#include#include#includepoint.hpoint:point(int a,int b)x=a,y=b;cout构造了点(x,y)t;point:point()cout析

8、构了点(x,y)t;void point:set()int a,b;coutPlease input x and yab;x=a,y=b;void point:display()cout(x,y)t;double point:length(const point &a)return sqrt(a.x-x)*(a.x-x)+(a.y-y)*(a.y-y);/以下是main.cpp#include#include#includepoint.h#define N 10void main()int i;double length1=0;point *a;a=new pointN;assert(a!=0);for(i=0;iN;i+)ai.set();for(i=0;iN;i+)ai.display();for(i=0;iN-1;i+)length1+=ai.length(ai+1);cout这些点所构成的折线长度为length1endl;delete a;四实验感想这次试验对我来说真的比较难,实在是不太会,在参考了同学的程序N次之后,终于明白了建立工程的重要性,了解到各个部分之间是如何联系起来的。程序中的类建立起来还是比较容易的,只是在有些细节上还有待改进。希望以后的实验能越做越顺利。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号