第9周项目2-我的数组类
来源:互联网 发布:查电话号码软件 编辑:程序博客网 时间:2024/04/30 01:40
/* *Copyright(C) 2016,计算机与控制工程学院 *All rights reserved. *文件名:test.cpp *作者:张志新 *完成日期:2016年4月29日 *版本号:v1.0 * *问题描述:完成此程序的成员函数。*/ #include<iostream>#include<string>using namespace std;class MyArray{private: int *arrayAddr; //保存一个有len个整型元素的数组的首地址 int len; //记录动态数组的长度 int max; //动态数组中的最大值(并非动态数组中必须要的数据成员)public: MyArray(int *a, int n); ~MyArray(); int getValue(int i); //获得数组中下标为i的元素的值 int getLen(); //返回数组长度 int getMax(); //返回数组中的最大值};//写出各成员函数的定义MyArray::MyArray(int *a, int n){ int i; arrayAddr=new int[n]; max=a[0]; len=n; for(i=0;i<n;i++) { arrayAddr[i]=a[i]; if(max<arrayAddr[i]) max=arrayAddr[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;}
学习心得:。
这个程序练习的是深复制,这个程序求最大值和数组长度都在构造函数中求会更简单,new建立了动态内存在构造函数中,释放动态内存在析构函数中。
0 0
- 第9周-项目2-我的数组类
- 第9周项目2:我的数组类
- 第9周项目2 我的数组类
- 第9周项目2-我的数组类
- 第9周项目2—我的数组类
- 第9周项目2-我的数组类
- 第9周项目2- 我的数组类
- 第9周项目2-我的数组类
- C++第9周项目 数组和指针 【项目2-我的数组类】(复习新版)
- 第9周项目二-我的数组类
- 第6周项目2我的数组类
- 第6周项目2-我的数组类
- 第5周项目2-我的数组类
- 第6周项目2-我的数组类
- 第6周项目2-我的数组类
- 2015-第6周项目2-我的数组类
- 第6周项目2我的数组类
- 第9周项目2——我的数组类
- 图片选择器
- Reset
- 数据结构--绪论自测题
- SQLserver 存储过程执行错误记录到表
- python发邮件
- 第9周项目2-我的数组类
- HDU 1429 胜利大逃亡(续)
- 各种消息下wParam及lParam值的含义
- app内存优化
- 数据结构与算法:C++实现归并排序
- Shell中字符串的截取方法
- python 本地数据获取
- centos 6.4 安装emacs 24.5
- 减少spark job jar包的大小