第9周项目2—我的数组类
来源:互联网 发布:vb.net 用户控件 编辑:程序博客网 时间:2024/05/01 20:01
/*02. *Copyright (c) 2016,烟台大学计算机学院03. *All rights reserved. 04. *文件名称:cpp 05. *作 者:孙亚茹06. *完成日期:2016年5月9日07. *版 本 号:v1.0 09. *问题描述:阅读程序完成成员函数的定义,因为存在指针型的数据成员,注意需要深复制的构造函数。*/#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];}int MyArray::getLen(){ return len;}int MyArray::getMax( ){ int max=0; int i; for(i=0;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;}
知识点总结:
深复制:当被复制的对象数据成员是指针类型时,要为其专门分配空间。 0 0
- 第9周项目2—我的数组类
- 第9周-项目2-我的数组类
- 第9周项目2:我的数组类
- 第9周项目2 我的数组类
- 第9周项目2-我的数组类
- 第9周项目2-我的数组类
- 第9周项目2- 我的数组类
- 第9周项目2-我的数组类
- C++第9周项目 数组和指针 【项目2-我的数组类】(复习新版)
- 第9周项目2——我的数组类
- 第9周项目二-我的数组类
- 第6周项目2我的数组类
- 第6周项目2-我的数组类
- 第5周项目2-我的数组类
- 第6周项目2-我的数组类
- 第6周项目2-我的数组类
- 2015-第6周项目2-我的数组类
- 第6周项目2我的数组类
- 协同过滤浅谈
- Android通用流行框架大全
- MFC选择文件目录查找带有指定后缀的所有文件
- SurfaceView-基本使用
- Java 实现图片水印之文字水印(SpringMVC + Jsp)
- 第9周项目2—我的数组类
- Spring定时器的两种实现方式
- 涉及模式总结
- 安装NTFS For Mac之后不能使用怎么办
- AndroidStudio配置指南总结-包括护眼模式
- YY3-带人的技术(前言、第一章)笔记-__XwD__-V1.xmind
- CountDownTimer ---实现计数器
- setjmp.h
- PAT-JAVA-5-28 猴子选大王 (20分)