《CPlusPlusPrimer》第二章编程源码——Array模板简单实现
2011-09-07 19:02
351 查看
array.h
array.cpp
test.cpp
运行截图:
#ifndef _CPlusPlusPrimer_Array #define _CPlusPlusPrimer_Array // 定义域名空间 namespace Icanth_CPlusPlus_Primer { template<class elemType> class Array { public: // 把元素类型参数化 explicit Array(int size = DefaultArraySize); Array(elemType* array, int array_size); Array(const Array& rhs); virtual ~Array() { delete [] ia; } // 第二个只能调用其它const成员函数,及不能对数据成员进行修改; // 获得对常量对象(此时为const Array &对象)的const成员函数. bool operator==(const Array &) const; bool operator!=(const Array &) const; Array& operator=(const Array &); int size() const { return _size; } virtual elemType& operator[](int index) { return ia[index]; } protected: static const int DefaultArraySize = 12; // 静态常量整型 int _size; // 函数名不能与变量名相同 elemType* ia; }; } #endif
array.cpp
#include "array.h" using namespace Icanth_CPlusPlus_Primer; template<class elemType> Array<elemType>::Array(int size = DefaultArraySize) { ia = new elemType[size]; for(int i = 0; i < size; i++) { *(ia + i) = 0; } _size = size; } template <class elemType> Array<elemType>::Array(elemType* array, int array_size) { ia = new elemType[array_size]; for(int i = 0; i < array_size; i++) { *(ia + i) = array[i]; } _size = array_size; } template <class elemType> bool Array<elemType>::operator==(const Array &arr) const { int i = 0; bool rslt = false; if((size() == 0) || (size() != arr.size())) { rslt = false; } else { while((i < size()) && (ia[i] && arr.ia[i])) { i++; } rslt = (i == size()) ? true : false; } return rslt; } template <class elemType> bool Array<elemType>::operator!=(const Array &arr) const { return !(this==arr); } template <class elemType> Array<elemType>& Array<elemType>::operator=(const Array &arr) { Array<elemType> new_arr(arr.ia, arr.size()); delete this; this = new_arr; return this; }
test.cpp
// 只有包含array.cpp文件才能使用模板类, // 而只包含#include "array.h"则不行。详解见STL原理。 #include "array.cpp" #include <iostream> using namespace std; // 使用标准库域名空间 namespace ICP = Icanth_CPlusPlus_Primer; // 将域名空间Icanth_CPlusPlus_Primer缩写成ICP int main() { using ICP::Array; // 使用ICP下的Array类 const int array_size = 3; int array[array_size] = {1, 2, 3}; Array<int> intArray(3); Array<int> intArray2(array, array_size); cout << "the value of intArray2[2]:" << intArray2[2] << endl; intArray2[2] = 3; cout << "the value of intArray2[2] after excuting intArray[2] = 3 is:" << intArray2[2] << endl; cout << "before the envalution to intArray1, the result of comparing intArray1 and intArray2 is:" << (intArray == intArray2) << endl; intArray[0] = intArray2[0]; intArray[1] = intArray2[1]; intArray[2] = intArray2[2]; cout << "afther the envalution to intArray1, the result of comparing intArray1 and intArray2 is:" << (intArray == intArray2) << endl; return 0; }
运行截图:
相关文章推荐
- 《CPlusPlusPrimer》第三章编程源码——String类的简单实现
- 《CPlusPlusPrimer》第四章编程源码——iStack类的简单实现
- 《CPlusPlusPrimer》第五章编程源码——list类的简单实现
- 《CPlusPlusPrimer》第十二章二节编程源码——vocab功能的源码
- 郭克华手机编程教学视频----我的练习源码(6)实战:实现一个简单的监听程序
- [Java] 【源码】新手初探java网络编程——socket接口实现简单的客户机/服务器程序
- java IO,伪异步IO以及NIO网络编程 简单实现源码以及区别
- C++ Primer Plus第六版 第二章 编程练习答案
- C++ Primer Plus第五版 第二章 编程练习答案
- C Primer Plus 6 第二章编程练习
- c++ primer plus 第六版第二章编程练习答案
- C primer plus(第五版)编程练习第二章
- 【第二章】C++ Primer plus 的编程练习题(选取部分)
- 《c primer plus》编程练习回顾-第二章
- c++ primer plus(第6版)中文版 第二章编程练习答案
- C Primer Plus (第五版) 第二章 编程练习
- Java GUI编程之贪吃蛇游戏简单实现方法【附demo源码下载】
- C Primer Plus 第二章 编程练习 1-7题
- C++ Primer Plus的string类的简单实现
- C++Primer Plus 第二章编程练习个人答案