15第六周项目二——我的数组类
来源:互联网 发布:程序员不善于沟通 编辑:程序博客网 时间:2024/06/03 20:53
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:李晓凯
* 完成日期:2015年 4 月 11 日
* 版 本 号:v1.0
*
* 问题描述:编辑构造函数和析构函数完成下列任务
(1)为各成员函数赋值,按照深复制原则,其中arrayAddr应该是为保存数据新分配的连续空间的首地址;
(2)MyArray(int *a, int n)中,要将a指向的数组中的数值,逐个地复制到新分配的arrayAddr指向的空间中;
(3)getMax( )函数采取的策略是直接返回max(所以,计算max的工作,由构造函数完成)
* 输入描述:
* 程序输出:一个数组的最大值和另一个数组的所有项之和
*/
#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){ int i; max=a[0]; len=n; arrayAddr=new int[n]; for(i=0;i<n;i++) { arrayAddr[i]=a[i]; if(max<a[i]) max=a[i]; }}MyArray::~MyArray(){}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
- 第六周——项目二我的数组类
- 第六周项目二——我的数组类
- 第六周项目二——我的数组类
- 第六周项目二—我的数组类
- 15第六周项目二——我的数组类
- 第六周 课后实践:项目二——我的数组类
- 第六周项目二:我的数组类
- 第六周 项目二:我的数组类
- 第六周项目二:-我的数组类
- 第六周项目二我的数组类
- 第六周项目二--我的数组类
- 第六周 项目二-我的数组类
- 第六周 项目二--我的数组类
- 第六周项目二我的数组类
- 2015年第六周项目二:我的数组类
- 第六周项目二 我的数组类
- 第六周项目二 我的数组类
- 第六周项目二 我的数组类
- ubuntu下安装程序的三种方法
- 写在学习经济学原理之前
- C++实现 反射 机制(根据 类名 创建 类的对象) C++ Create Object Dynamically
- metasploit - killav - netsh
- burpsuite1.6安装使用方法详解
- 15第六周项目二——我的数组类
- jQuery高性能跨浏览器全屏幻灯片特效插件
- Android出现Multiple dex files define...
- Ubuntu下deb包的安装方法
- 第5周-项目0-阅读程序
- GerritCodeReview安装过程
- The typical features of the film faced plywood
- 通信系统中进行调制和解调的原因及调制的分类方法
- Foxit pdf阅读器不及时释放文件的bug解决