类还实现了资源获取即初始化(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 用模板实现泛型
强类型一定程度上实现了类型在编译期的检查功能,而模板却可以让类并不囿于过于具体的类型:
推荐阅读
- 莳萝功效,莳萝精油的功效与作用
- 喝人参花茶的好处,喝人参茶的好处
- 在Java中使用Optional的开销很大 - pkolaczk
- 洋甘菊对皮肤的功效,洋甘菊的功效
- 五宝茶哪个牌子好,五宝茶什么牌子的好
- 双柏散的功效与作用,四妙散的功效与作用
- linux之grep使用技巧
- 藤合欢的别名,合欢皮别名是什么呢
- H3C常见视图及命令
- 微波炉怎么加热饭菜,不能用微波炉加热的食物