第九周-我的数组类
来源:互联网 发布:证券软件哪个好 编辑:程序博客网 时间:2024/05/16 08:31
问题及代码:
/*copyright 计算机与控制工程学院。完成日期:2016年5月4日作者:马艳艳问题描述:无;输入描述:无;输出描述:结果信息*/#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;}
运行结果 :
知识点总结:
深复制时,利用指针要先分配空间。
0 0
- 第九周2-我的数组类
- 第九周-我的数组类
- 第九周项目2 我的数组类
- 第九周项目 2.1 我的数组类
- 第九周项目2-我的数组类
- 第九周项目3:我的数组类
- 第九周项目2—我的数组类
- 第九周项目三 我的数组类
- 第九周项目二-我的数组类
- 第九周项目2 我的数组类
- 第九周项目2-我的数组类
- 第九周项目2——我的数组类
- 第九周【项目2-我的数组类】
- 第九周实践项目2-我的数组类
- 第九周项目二我的数组类
- 第九周项目3我的数组类
- 第九周 项目二-我的数组类
- 第九周项目2—我的数组类
- Android定位和地图开发实例
- leetcode 328 --Odd Even Linked List 链表 双指针 移动节点
- 关于java.awt.list与java.util.list的一些区别
- 安装Numpy,Scipy,Matplotlib的步骤及下载地址(亲测有效)
- Java对数据库进行事务处理(批量删除操作)
- 第九周-我的数组类
- 剑指offer:把二叉树打印成多行
- hdoj-5670-Machine
- 封装的EditText的输入限制(输入的数字前+¥,精确到小数点后两位)
- GDB概述
- hdoj--4289--Control(最小割拆点)
- CSDN-markdown编辑器语法——字体、字号与颜色
- 1.介绍 - IntelliJ IDEA 使用教程
- TortoiseSVN基本操作和冲突演示3