第六周项目2——我的数组类
来源:互联网 发布:微信支付demo java 编辑:程序博客网 时间:2024/05/20 06:57
(1)为各成员函数赋值,按照深复制原则,其中arrayAddr应该是为保存数据新分配的连续空间的首地址;
(2)MyArray(int *a, int n)中,要将a指向的数组中的数值,逐个地复制到新分配的arrayAddr指向的空间中;
(3)getMax( )函数采取的策略是直接返回max(所以,计算max的工作,由构造函数完成)
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( ); //返回数组中的最大值
};
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;
}
#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]; }}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;}
总结:还是分不清楚深复制和复制有何区别;感觉用起来都是一样的;
- 第六周项目2——我的数组类
- 第六周——项目二我的数组类
- 第六周项目二——我的数组类
- 第六周项目二——我的数组类
- 第六周项目二—我的数组类
- 第六周项目:我的数组类
- 第六周上机实践项目2——我的数组类
- 第六周【项目2-我的数组类】2
- 第六周项目2 我的数组类
- 第六周 项目2-我的数组类
- 第六周 【项目2-我的数组类】
- 第六周项目2 我的数组类
- 第六周上机实践项目2-我的数组类
- 第六周【项目2-我的数组类】1
- 第六周 【项目2-我的数组类】
- 第六周上机项目2我的数组类
- 第六周项目2 我的数组类
- 第六周项目2:我的数组类
- 第六周项目一之深复制体验
- 第六周项目二 我的数组类
- 国内不翻墙更新SDK方法
- 我的数组类
- 第五周项目一——深复制体验1
- 第六周项目2——我的数组类
- 第六周 项目一 深复制体验2
- 第6周项目1——深复制体验(2)
- 省选模版复习——后缀数组
- 第六周项目一深复制体验
- 项目1-深复制的体验(2)
- C++单例模式
- BP神经网络Python实现
- 省选模版复习——LCT