第七周作业项目---体验常成员函数
2014-04-15 19:23
483 查看
/* * 程序的版权和版本声明部分: * Copyright (c) 2013.烟台大学计算机学院。 * All rights reserved. * 文件名称: 体验常成员函数 * 作 者:何新 * 完成日期:2014 年 4月 8日 * 版 本 号:v1.0 * 对任务及求解方法的描述部分: * 输入描述: * 问题描述: * 程序输出: */ #include<iostream> using namespace std; class A { private: int *arrayAddr;//保存一个有len个整型元素的数组的首地址 int len; //记录动态数组的长度 int max; //动态数组中的最大值(并非动态数组中必须要的数据成员) public: A(int *a, int n); ~A(); int getValue(int i); //获得a指向的数组中下标为i的元素的值 int getLen(); //返回数组长度 int getMax( ); //返回数组中的最大值 }; A::A(int *a,int n) { arrayAddr=new int[n+1]; arrayAddr=a; len=n; max=arrayAddr[0]; for(int i=1; i<len; i++) { if(max<arrayAddr[i]) max=arrayAddr[i]; } } A::~A() { delete[]arrayAddr; } int A::getValue(int i) //获得a指向的数组中下标为i的元素的值 { return arrayAddr[i]; } int A::getLen() //返回数组长度 { return len; } int A::getMax( ) //返回数组中的最大值 { return max; } int main() { int b[10]= {75, 99, 90, 93, 38, 15, 5, 7, 52, 4}; A 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}; A r2(c,15); int i,s=0; for(i=0; i<r2.getLen(); i++) s+=r2.getValue(i); cout<<"所有元素的和为:"<<s<<endl; return 0; }
相关文章推荐
- 内存泄露检测—VLD工具
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
- arm汇编--变量
- 初识springMVC
- hdu题目分类
- Struts2提交表单中文乱码问题解决
- C# WinForm多线程开发(一) Thread类库
- C# WinForm多线程开发(一) Thread类库
- Visitor模式加深理解
- alibaba FastJson 解析
- N皇后问题
- Centos 6.5 安装CloudStack4.3(完善更新)
- Uva11324 The Largest Clique tarjan+dp
- ubuntu如何安装tar.gz文件
- 代码面试最常用的10大算法
- Xcode cannot run using the selected device
- java继承
- ios开发常用技巧
- androd—Intent操作ContentProvider获取电话号码
- C#迁移之callXBFLibrary - ADO.net