第九周项目2—我的数组类
来源:互联网 发布:pkpm软件 编辑:程序博客网 时间:2024/06/09 20:06
<img src="http://img.blog.csdn.net/20160611152600622?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />/* 02.*烟台大学计算机学院 03.*文件名称:xiangmu2.cpp 04.*作 者:李亚辉 05.*完成日期:2016年6月11日 06.*版 本 号:vc++6.0 07.* 08.*问题描述: 完成个成员函数的定义 09.*输入描述: 无 10.*程序输出: 最大值与元素之和11..*/ #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[0]; for(int i=0; i<n; i++) { arrayAddr[i]=a[i]; if (max<a[i]) max=a[i]; }}//析构函数的类外定义,释放指针型数据a所指向的空间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;}//学习心得:感觉与出语言里的数组没什么不同,只是在形式上,在运用中发生了一些细微的变化,但有时候还是会混淆数组的下标,自己C语言里有时也经常如此,还是因为自己的条例有时候不清晰造成的,数组加上指针真是令人头疼啊,好在这个项目并不是很复杂。以后还是要多回顾指针、数组的运用。
0 0
- 第九周项目2——我的数组类
- 第九周项目2—我的数组类
- 第九周项目2—我的数组类
- 第九周项目(2)—我的数组类
- 第九周项目2—我的数组类
- 第九周项目2 我的数组类
- 第九周项目2-我的数组类
- 第九周项目2 我的数组类
- 第九周项目2-我的数组类
- 第九周【项目2-我的数组类】
- 第九周实践项目2-我的数组类
- 第九周项目2-我的数组类
- 第九周上机实践项目2:我的数组类
- 第九周项目2-我的数组类
- 第九周项目2-我的数组类
- 第九周项目2-我的数组类
- 第九周上机时间项目——项目2—我的数组类
- 第九周上机实践项目——项目2-我的数组类
- Java---俄罗斯方块小游戏
- 第14周项目1-排序函数模板
- 解决wampserver server offline
- 排序算法基础篇之宏观理论
- Hibernate4关系映射总结
- 第九周项目2—我的数组类
- Class.forName()用法详解
- 1358 Period
- $("button").click(function () { 与 $(":button").click(function () { window.onblur window.onfocus
- ACdream 1216 Beautiful People 【双序列LIS输出下标】
- 数据挖掘 贝叶斯算法
- 有了梭子和毛线就能织出各式各样的衣服
- Hibernate学习——第一章
- C++操作符重载