第六周项目2-my array class
2015-04-13 18:02
183 查看
/* *Copyright (c) 2014, 烟台大学计算机学院 *All rights reserved. *文件名称:week6-2.cpp *作者:高赞 *完成日期:2015年 4 月 13 日 *版本号: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); //获得数组中下标为i的元素的值 int getLen(); //返回数组长度 int getMax(); //返回数组中的最大值 }; MyArray::MyArray(int *a, int n) { arrayAddr=new int ; max=a[0]; for(int i=0; i<n; ++i) { arrayAddr[i]=a[i]; if(max<a[i]) max=a[i]; } len=n; } MyArray::~MyArray() { delete []arrayAddr; } 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; }
在析构函数中释放之前分配的空间。
相关文章推荐
- MyEclipse Workspace 项目文件 .project .classpath .mymetadata解析
- 使用weblogic部署项目时提示ClassNotFoundException: org.hibernate.hql.ast.HqlToken【转】
- idea 中Java web项目只中创建Java main方法运行出现ClassNotFoundException
- 关于IDEA开发工具,WEB 项目编译后的class文件存放的地方!
- mac版本idea使用(四) - 检出的项目运行时候报错:Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
- 项目启动报错 content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,...)"
- 第六周 项目2 对象作为数据成员
- class获取项目路径
- web项目启动报java.lang.ClassNotFoundException
- My Graduation’s project:StudentClassMaintain module
- web项目部署到tomcat 遇到ClassNotFound问题
- eclipse用maven运行项目报错java.lang.ArrayIndexOutOfBoundsException: 28054
- Tomcat启动web项目报Bad version number in .class file (unable to load class ...)错
- 第六周项目2-带武器的游戏角色
- 第六周项目4-静态成员应用
- There is no getter for property named 'value' in 'class com.hl.myabtis.first.beas.User'
- eclipse创建第一个spring boot工程项目时ClassNotFoundException
- 解决DLL包组织的项目运行报 a class named Txxx exists
- 第六周项目(1):建立顺序栈算法库
- 第六周上机项目4-静态成员应用