low >> high;if (!array.initialize(low, high)) {// 为array申请存储。C++的逐层抽象:从结构体到类、模板( 二 )。" />

C++的逐层抽象:从结构体到类、模板( 二 )

using namespace std;#include "array.h"int main(){DoubleArray array;double value;int low, high, i;//输入数组的下标范围cout <<"请输入数组的下标范围:";cin >> low >> high;if (!array.initialize(low, high)) {// 为array申请存储数组元素的空间cout <<"空间分配失败" ; return 1;}//数组array的初始化for (i = low; i <= high; ++i) {//数组元素的输入cout <<"请输入第"<< i <<"个元素:";cin >> value;array.insert(i, value);}while (true) {//数组元素的访问cout <<"请输入要访问的元素序号(0表示结束):";cin >> i;if (i == 0)break;if (array.fatch(i, value))cout << value << endl;elsecout <<"下标越界n";}array.cleanup();//归还存储数组元素的空间return 0;}3 用定义较为完整的类来实现
类还实现了资源获取即初始化(RAII, Resource Acquisition Is Initialization)以及自动实现资源析构的功能,以及对运算符的重载等功能 。
// DoubleArray.h// DoubleArray类的定义#ifndef _array_h#define _array_h#include <iostream.h>class DoubleArray{friend ostream &operator<<(ostream &os, const DoubleArray &obj);friend istream &operator>>(istream &is, DoubleArray &obj);friend bool operator==(const DoubleArray &obj1, const DoubleArray &obj2);private:int low;int high;double *storage;public:// 构造函数根据low和high为数组分配空间DoubleArray(int lh = 0, int rh = 0):low(lh), high(rh){storage = new double [high - low + 1];}// 复制构造函数DoubleArray(const DoubleArray &arr);// 赋值运算符重载函数DoubleArray &operator=(const DoubleArray &right);// 下标运算符重载函数double & operator[](int index);// 作为左值const double & operator[](int index) const;// 作为右值// 取数组的一部分形成一个新的数组DoubleArray operator()(int start, int end, int lh);// 析构函数~DoubleArray() {if (storage)delete [] storage;}};#endif// DoubleArray.cpp// DoubleArray类的实现#include <cassert>#include "DoubleArray.h"DoubleArray::DoubleArray(const DoubleArray &arr){low = arr.low;high = arr.high;storage = new double [high - low + 1];for (int i = 0; i < high -low + 1; ++i)storage[i] = arr.storage[i];}DoubleArray &DoubleArray::operator=(const DoubleArray & a){if (this == &a)// 防止自己复制自己return *this;delete [] storage;// 归还空间low = a.low;high = a.high;storage = new double[high - low + 1];// 根据新的数组大小重新申请空间for (int i=0; i <= high - low; ++i)storage[i] = a.storage[i];// 复制数组元素return *this;}double & DoubleArray::operator[](int index){assert(index >= low && index <= high);return storage[index - low];}const double & DoubleArray::operator[](int index) const{assert(index >= low && index <= high);return storage[index - low];}ostream &operator<<(ostream &os, const DoubleArray &obj){os <<"数组内容为:n";for (int i=obj.low; i<=obj.high; ++i)os << obj[i] << 't';os << endl;return os;}istream &operator>>(istream &is, DoubleArray &obj){cout <<"请输入数组元素["<< obj.low <<", "<< obj.high <<"]:n";for (int i=obj.low; i<=obj.high ; ++i)is >> obj[i] ;return is;}bool operator==(const DoubleArray &obj1, const DoubleArray &obj2){if (obj1.low != obj2.low || obj1.high != obj2.high)return false;for (int i = obj1.low; i<=obj1.high; ++i)if (obj1[i] != obj2[i])return false;return true;}DoubleArray DoubleArray::operator()(int start, int end, int lh){assert (start <= end && start >= low && end <= high );// 判断范围是否正确DoubleArray tmp(lh, lh + end - start);// 为取出的数组准备空间for (int i = 0; i < end - start + 1; ++i)tmp.storage[i] = storage[start + i - low];return tmp;}// DoubleArrayApp.cpp// DoubleArray类的使用#include "DoubleArray.h"int main(){DoubleArray array1(20,30), array2;cin >> array1;// 利用流提取运算符重载输入array1cout <<"array1 "; cout << array1;// 利用流插入运算符重载输出array1array2 = array1;// 利用赋值运算符重载将array1赋给array2cout <<"执行 array2 = array1, array2 ";cout << array2;// 利用==重载比较array1和array2cout <<"array1 == array2 是 "<< ((array1 == array2)? "true" : "false") << endl;array2[25] = 0;// 利用下标运算符重载为array2的元素赋值cout <<"执行array[25] = 0后, array1 == array2 是 "<< ((array1 == array2)? "true" : "false") << endl;array2 = array1(22, 25, 2);cout <<"执行array2 = array1(22, 25, 2)后, array2 的值为: "<< array2;while(1);return 0;}4 用模板实现泛型
强类型一定程度上实现了类型在编译期的检查功能,而模板却可以让类并不囿于过于具体的类型:


推荐阅读