预览加载中,请您耐心等待几秒...
1/7
2/7
3/7
4/7
5/7
6/7
7/7

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

实验4多文件结构及静态成员和对象数组的使用 一、实验目的 1、掌握自定义头文件的方法; 2、学会建立和调试多文件程序; 3、了解静态成员的使用; 4、掌握对象数组的使用。 二、实验内容 1、编写一个函数,求数列运算中从n个不同的数中取r个数的所有选择的个数。 要求: (1)将main()函数放在一个文件中; (2)将另外两个函数fn(intn),Cnr(intn,intr)放在另一个文件中; (3)将函数原型说明放在一个头文件中; (4)建立一个项目,将这三个文件加到你的项目中,编译连接使你的程序正常运行。 2、设计一个Employee类,并在Employee类中增加一个静态数据成员来设置本公司员工编号基数,新增加的员工编号将在创建对象的同时自动在基数上增加。另外,将Employee类的声明部分和实现部分分成两个文件来实现。编程实现之。 3、设计一个点类point,该类具有两个实数坐标。希望主程序使用这个类完成下述功能: (l)主程序为类point申请10个连续存储空间。 (2)要求调用一个函数Set()从键盘输入10个对象的属性,并顺序存入申请的内存中。 (3)要求调用一个函数Display()显示10个对象的值。 (4)要求调用一个函数Lenth(),计算将这些点连成一条折线时,这条折线的长度。 (5)程序结束时,删除申请的内存。 (6)演示析构对象(动态对象或堆对象)的执行顺序。 设计这个类和各个函数并验证运算结果的正确性。 三.实验源代码 1. //以下是cnr.h #ifndefcnr_h #definecnr_h intcnr(int,int); #endif //以下是cnr.cpp #include"cnr.h"; intcnr(intn,intr) { if(n==r) return1; elseif(r==1) returnn; elsereturncnr(n-1,r)+cnr(n-1,r-1); } //以下是main.cpp #include<iostream.h> #include"cnr.h"; voidmain() { intn,r; cout<<"Pleaseinputn,r:"<<endl; cin>>n>>r; cout<<"C(n,r)为"<<cnr(n,r)<<endl; } 2. //以下是employee.h #ifndefemployee_h #defineemployee_h classemployee { public: employee(int,char*,char,int); ~employee(); voidsetemployee(int,char*,char,int); voidprint(); private: staticintbasenum; intnum; char*name; charsex; intwage; }; #endif //以下是employee.cpp #include"employee.h" #include<iostream.h> #include<string.h> #include<assert.h> intemployee::basenum=10; employee::employee(intnu,char*na,chars,intw) { name=newchar[strlen(na)+1]; assert(name!=0); strcpy(name,na); num=nu+basenum; sex=s; wage=w; } employee::~employee() { delete[]name; } voidemployee::setemployee(intnu,char*na,chars,intw) { delete[]name; name=newchar[strlen(na)+1]; assert(name!=0); strcpy(name,na); num=nu+basenum; sex=s; wage=w; } voidemployee::print() { cout<<"num:"<<num<<'\t'<<"name:"<<name<<endl<<"sex:"<<sex<<'\t'<<"wage:"<<wage<<endl; } //以下是main.cpp #include<iostream.h> #include"employee.h"; voidmain() { employeenum1(2,"oo",'q',5000),num2(3,"rr",'q',10000); cout<<"num1:"<<endl; num1.pr