第六周项目2-my array class
来源:互联网 发布:淘宝店铺怎么修改域名 编辑:程序博客网 时间:2024/05/17 03:55
/* *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 [n]; 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;}
在析构函数中释放之前分配的空间。
0 0
- 第六周项目2-my array class
- 第六周项目2
- 第六周项目2
- vo(class)2Array
- 第六周项目2-2
- 第六周项目2-1
- 第六周项目2填空
- 第六周项目一(2)
- 第六周项目2-友元类
- 第六周项目一(2)
- 第六周项目一(2)
- 第六周上机任务项目 2
- 第六周项目二(2)
- 第六周项目阅读程序2
- 第六周项目一(2)
- 第六周 项目2 数组类
- 第二学期第六周项目2
- 第六周项目2猴子选大王
- Java开发之反射机制(详)
- CMOS Sensor的调试经验分享
- CSS:盒子模型与定位
- 《Kinect应用开发实战》读书笔记---干货集合
- 纯Java实现的多路归并快速排序算法
- 第六周项目2-my array class
- WEB-INF 有关的目录路径问题总结
- 用 scanf 系列函数实现简单的 email 地址合法性检查功能
- org.springframework.context.ApplicationContextAware使用理解
- Chief Ray Angle
- hdoj2072字符串单词数
- 发现了一个大神推荐Android开发的资源,不管你是一个是从事或是准备从事Android 开发还是纯粹的兴趣爱好,都值得参考
- Mac系统下Tomcat安装及配置
- 解决Eclipse进行Android开发时logcat不显示问题-使用DDMS