第九周项目2——我的数组类
来源:互联网 发布:面相大师知秋 编辑:程序博客网 时间:2024/05/18 17:54
问题及代码:
/* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名称:main.cpp * 作 者:赵志君 * 完成日期:2016年4月28日 * 版 本 号: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]; arrayAddr=a; len=n;}MyArray::~MyArray(){ delete []arrayAddr;}int MyArray::getValue(int i){return *(arrayAddr+i);} //返回下标为i的元素的值int MyArray::getLen(){return len;} //返回数组长度int MyArray::getMax() //返回函数的最大值{ int max=*arrayAddr; for(int i=1;i<len;i++) { if(arrayAddr[i]>max) max=arrayAddr[i]; } 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;}
运行结果:
学习心得:整形数组不需要最后的结束符,在刚开始做的时候模仿了项目1中的加一和使用strlen函数,后来发现整形数组并不需要那么的麻烦。
0 0
- 第九周项目2——我的数组类
- 第九周项目2—我的数组类
- 第九周项目2—我的数组类
- 第九周项目(2)—我的数组类
- 第九周项目2—我的数组类
- 第九周上机时间项目——项目2—我的数组类
- 第九周上机实践项目——项目2-我的数组类
- 第九周实践项目2————我的数组类
- 第九周项目2 我的数组类
- 第九周项目2-我的数组类
- 第九周项目2 我的数组类
- 第九周项目2-我的数组类
- 第九周【项目2-我的数组类】
- 第九周实践项目2-我的数组类
- 第九周项目2-我的数组类
- 第九周上机实践项目2:我的数组类
- 第九周项目2-我的数组类
- 第九周项目2-我的数组类
- 第九周阅读程序-(1)
- 奋斗吧,程序员——第十九章 无可奈何花落去,似曾相识燕归来
- 经典排序算法之冒泡排序
- 做一个带X的弹出DIV
- 移动互联风口频现,百度高德谁已手握地图关键钥匙
- 第九周项目2——我的数组类
- 例会新名词
- 编程小练习
- <input>checkbox
- 为什么日本德国没有一流互联网企业?
- 关于图片转化为NSDATA
- 第八周项目1-数组作数据成员(1)
- RSA签名和验签过程
- ios goto语法。