第六周项目:我的数组类
来源:互联网 发布:sql数据库紧急模式 编辑:程序博客网 时间:2024/05/20 06:09
代码:
#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); //获得数组中下标为i的元素的值 int getLen(); //返回数组长度 int getMax(); //返回数组中的最大值};MyArray::MyArray(int *a,int n){ len=n; arrayAddr=new int [n]; max=*a; for(int i=0;i<n;i++) { *(arrayAddr+i)=*(a+i); if(max<*(a+i)) max=*(a+i); }}int MyArray::getValue(int i) //获得数组中下标为i的元素的值{ return arrayAddr[i];}int MyArray::getLen() //返回数组长度{ return len;}int MyArray::getMax() //返回数组中的最大值{ return max;}MyArray::~MyArray(){ delete [] arrayAddr;}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-我的数组类
- 第六周 【项目2-我的数组类】
- 第六周 项目二--我的数组类
- 第六周项目2 我的数组类
- 第六周项目二——我的数组类
- 第六周项目二我的数组类
- 2015年第六周项目二:我的数组类
- 第六周上机实践项目2-我的数组类
- Dedecms 数据库结构分析
- Container with most water
- 关于mysql的update语句中可能出现的com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException的一点发现
- ubuntu安装sogou失败后恢复ibus
- Facebook新开源框架Fresco(转载)
- 第六周项目:我的数组类
- 将泛型为String类型的集合或String类型的数组转化为逗号间隔字符串形式
- RPowerLabs: Electric power system virtual laboratories online
- 2015年大一下第5周项目6-复数模板类(2)复数模板类友元函数
- [workStudy]Mail Delivery System发邮件
- 指针和字符串
- What does operator “dot” (.) mean?
- 三种方法进行分水岭分割
- 2015-第六周-复数类