第九周项目二我的数组类
来源:互联网 发布:winsteel软件下载 编辑:程序博客网 时间:2024/06/06 09:52
/* *Copyright(c)2016,烟台大学计算机与控制工程学院 *All right reserved. *文件名称:text.cpp *作 者:闫舒 *完成日期:2016年4月28日 *版 本 号:v1.0 * *问题描述:完成成员函数的定义,因为存在指针型的数据成员,注意需要深复制的构造函数。 *程序输入: *程序输出: 最大值以及所有元素和 */ #include <iostream> using namespace std; class MyArray { private: int *arrayAddr;//保存一个有len个整形元素的数组的首地址 int len;//记录动态数组的长度 int max;//记录数组中的最大值(并非动态数组中必须要的数据成员) public: MyArray(int *a,int n); ~MyArray(); int getValue(int i);//获得数组中下标为的元素的值 int getLen(); //返回数组长度 int getMax(); //返回数组中的最大值 }; MyArray::MyArray(int *a,int n) { len=n; arrayAddr=new int[n]; max=a[0]; for(int i=0;i<n;i++) { arrayAddr[i]=a[i]; if(max<a[i]) max=a[i]; } } MyArray::~MyArray() { delete []arrayAddr; } int MyArray:: getValue(int 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 我的数组类
- 第九周项目 2.1 我的数组类
- 第九周项目2-我的数组类
- 第九周项目3:我的数组类
- 第九周项目2—我的数组类
- 第九周项目三 我的数组类
- 第九周项目2 我的数组类
- 第九周项目2-我的数组类
- 第九周项目2——我的数组类
- 第九周【项目2-我的数组类】
- 第九周实践项目2-我的数组类
- 第九周项目3我的数组类
- 制作自定义背景Button按钮、自定义形状Button的全攻略
- ubuntu14.04下vnc的安装
- i2c协议
- 构建定制的camera
- asp.net课程设计——物流信息供求网
- 第九周项目二我的数组类
- JavaScript入门
- glclear glclearcolor小记
- Android-仿微信菜单
- 匈牙利算法和hopcroft_karp算法模板
- HttpURLConnection
- Android初级教程之内容提供者获取联系人信息
- 重识char与vchar
- 将uniy3D集成到android的子窗口中