第九周项目(2)—我的数组类
来源:互联网 发布:npm node sass 淘宝 编辑:程序博客网 时间:2024/06/06 01:25
问题描述及代码:
/**copyright (c) 2016,烟台大学计算机学院*All rights reserved.*文件名称:hellow.cpp*作者:田甜*完成日期:2016年5月12日*版本号:v1.0**问题描述:阅读程序,请完成成员函数的定义,因为存在指针型的数据成员,注意需要深复制的构造函数。*输入描述:///*程序输出:///*/#include <iostream>using namespace std;class MyArray{private: int *arrayAddr; int len; int max;public: MyArray(int array[],int len); ~MyArray(); int get_value(int i); int get_len(); int get_max();};MyArray::MyArray(int array[],int len){ this->len=len; arrayAddr=new int[len]; for(int i=0;i<len;i++) { arrayAddr[i]=array[i]; }}int MyArray::get_len(){ return len;}int MyArray::get_value(int i){ return arrayAddr[i-1];}int MyArray::get_max(){ int max=arrayAddr[0]; for(int i=0;i<len;i++) { if(arrayAddr[i]>max) max=arrayAddr[i]; } return max;}MyArray::~MyArray(){ delete []arrayAddr;}int main(){ int sum=0; int arr[6]={1,2,3,4,5,6}; MyArray array(arr,6); cout<<array.get_len(); cout<<array.get_max(); cout<<array.get_value(2); cout<<endl; for(int i=1;i<array.get_len()+1;i++) sum+=array.get_value(i); cout<<"所有元素的和为:"<<sum<<endl; return 0;}运行结果:
心得体会:
在给类中的指针所指向的空间赋值时一定要注意动态分配内存,否则指针为野指针,程序会崩溃。
0 0
- 第九周项目(2)—我的数组类
- 第九周项目2——我的数组类
- 第九周项目2—我的数组类
- 第九周项目2—我的数组类
- 第九周项目2—我的数组类
- 第九周项目2 我的数组类
- 第九周项目2-我的数组类
- 第九周项目2 我的数组类
- 第九周项目2-我的数组类
- 第九周【项目2-我的数组类】
- 第九周实践项目2-我的数组类
- 第九周项目2-我的数组类
- 第九周上机实践项目2:我的数组类
- 第九周项目2-我的数组类
- 第九周项目2-我的数组类
- 第九周项目2-我的数组类
- 第九周上机时间项目——项目2—我的数组类
- 第九周上机实践项目——项目2-我的数组类
- 佛祖保佑 永无bug
- Java 值传递和对象传递详解
- POJ 2184 Cow Exhibition
- GEEK编程练习— —最长连续序列
- 如何让UITextField在跳转后直接进入编辑状态
- 第九周项目(2)—我的数组类
- android 利用android:scheme 打不开app或者app打开失败的处理
- Android自定义控件1:自定义topBar
- 【Node.js】使用iconv-lite解决中文乱码 (GB2312)
- 余数和商 两位数乘以一位数
- 中国移动关于WEB应用渗透|Writeup
- 最全面的 Android 编码规范指南
- 【python】python中的字符串及其格式化
- CocoaPods:missing from working copy 解决方法