第六周【项目2-我的数组类】1
来源:互联网 发布:手机添加阿里云邮箱 编辑:程序博客网 时间:2024/05/09 04:45
问题及代码:
/*【项目2-我的数组类】 *Copyright (c) 2014,烟台大学计算机学院 *ALL right reserved *文件名:项目2-我的数组类(1)*作者;童宇 *完成日期:2015年 4月 12日 *版本号v1.0 *问题描述:为各成员函数赋值,按照深复制原则,其中arrayAddr应该是为保存数据新分配的连续空间的首地址 *输入描述: *程序输出: */#include<iostream>#include<cstring>using namespace std;class MyArray{private: int *arrayAddr; //保存一个有len个整型元素的数组的首地址 int len; //记录动态数组的长度 int max; //动态数组中的最大值(并非动态数组中必须要的数据成员)public: MyArray(int *a, int n) { max=0; len=n; arrayAddr=new int[n]; arrayAddr=a; for(int i=0; i<n; i++) { if(arrayAddr[i]>max) max=arrayAddr[i]; } } ~MyArray() { delete []arrayAddr; } int getValue(int i); //获得数组中下标为i的元素的值 int getLen(); //返回数组长度 int getMax( ); //返回数组中的最大值};int MyArray::getValue(int i) //获得数组中下标为i的元素的值{ return arrayAddr[i];}int MyArray::getLen() //返回数组长度{ return len;}int MyArray::getMax( ) //返回数组中的最大值{ return max;}int main(){ int b[10]= {75, 99, 90, 93, 38, 15, 5, 7, 52, 4}; MyArray r1(b,10); cout<<"最大值:"<<r1.getMax()<<endl; int c[15] = {18,68,10,52,3,19,12,100,56,96,95,97,1,4,93}; MyArray r2(c,15); int i,s=0; for(i=0; i<r2.getLen(); i++) s+=r2.getValue(i); cout<<"所有元素的和为:"<<s<<endl; return 0;}
运行结果:
0 0
- 第六周【项目2-我的数组类】1
- 第六周项目:我的数组类
- 第六周【项目2-我的数组类】2
- 第六周项目2 我的数组类
- 第六周 项目2-我的数组类
- 第六周 【项目2-我的数组类】
- 第六周项目2 我的数组类
- 第六周上机实践项目2-我的数组类
- 第六周 【项目2-我的数组类】
- 第六周项目2——我的数组类
- 第六周上机项目2我的数组类
- 第六周项目2 我的数组类
- 第六周项目2:我的数组类
- 第六周项目2-我的数组类
- 第六周项目2-我的数组类
- 第六周项目2我的数组类
- 第六周上机项目2 我的数组类
- 第六周项目1-我的数组类
- Android之请求码和返回码
- 修改UIWebView中字体的大小
- 高性能高并发 面试
- hdfs随机读写文件
- PAT 1065. A+B and C (64bit) (20)
- 第六周【项目2-我的数组类】1
- Android 图片上传 工具提交类(三)
- 小米2s 白色电信版 线刷教程亲身经历
- OS X 10.10 配置nginx+php+mysql
- 正则表达式
- mysql隔离级别与悲观锁、乐观锁
- 触摸屏性能优化
- 进制转换 HDU 2031 注意-2147483648变为正数后用int存会溢出
- 机器学习--概述