⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enhancedsafearray.h

📁 数据结构 ssd5 exam1
💻 H
字号:
/*
 * author:weizheng
 * ID: 063420
 * EXAM1
 */


#ifndef ENHANCEDSAFEARRAY_H
#define ENHANCEDSAFEARRAY_H

#include<iostream>
#include"safeArray.h"
using namespace std;

//class EnhancedSafeArray
template <typename T>   
class EnhancedSafeArray : public SafeArray<T>   
{   
	public:   

		EnhancedSafeArray(void);   
        EnhancedSafeArray(int);    
        EnhancedSafeArray(const EnhancedSafeArray<T>& enh_T); 

        int size(void) const;   
        const EnhancedSafeArray<T> &operator =(const EnhancedSafeArray<T>& enh_T);   
        bool operator ==( EnhancedSafeArray<T>& enh_T) const;   
};   

/*
 * default constructor. It invoke the default constructor of the SafeArray class. 
 */
template <typename T>   
EnhancedSafeArray<T>::EnhancedSafeArray(void):SafeArray<T>(){ }

/*
 * single-parameter constructor.
 * It invoke the single-parameter constructor of the SafeArray class.
 * It build a array that its space is "count" 
 */
template <typename T>   
EnhancedSafeArray<T>::EnhancedSafeArray(int count):SafeArray<T>(count){ }

/*
 * copy constructor 
 * copy the private members of class SafeArray from one object to another
 */
template <typename T>   
EnhancedSafeArray<T>::EnhancedSafeArray(const EnhancedSafeArray<T>& enh_T):SafeArray<T>()
{ 
	count = enh_T.count;   
    storage = new T[enh_T.count];   
    for(int i = 0; i < (this->count); i++)
	{ 
        storage[i] = enh_T.storage[i];
	}
}

/*
 * return the size of this array 
 */
template <typename T>   
int  EnhancedSafeArray<T>::size(void) const   
{   
    return count;   
}
 
/*
 * change value of array to a current array  
 */
template <typename T>   
const EnhancedSafeArray<T>& EnhancedSafeArray<T>:: operator=( const EnhancedSafeArray<T>& enh_T)   
{   
	//if the two array are same, it have no use for change the value
    if(this != &enh_T)   
    {   
		//if the size are different, changes in the size of the current array
        if(this->count != enh_T.count)   
		{   
			this->count = enh_T.count; 
            delete[] this->storage;				//delete the old data space  
            this->storage = new T[this->count]; //build a new data space
		}
		
		for(int i = 0; i < (this->count); i++)   
		{   
			this->storage[i] = enh_T.storage[i]; //copy the data one by one
		}
	}
    return *this;   
       
}   
   
/*
 * Two EnhancedSafeArray objects are equal if and only if their sizes 
 * and their stored elements are equal. 
 * If two EnhancedSafeArray objects are equal return true
 * else return false
 *
 */
template <typename T>   
bool EnhancedSafeArray<T>::operator==( EnhancedSafeArray<T>& enh_T) const   
{   
    if(this->count != enh_T.count)   
        return false;   
	
    for( int i = 0; i < (this->count); i++)
	{  
		if(this->storage[i] != enh_T.storage[i])
			return false;   
	}

	return true;                              
}   

#endif //ENHANCEDSAFEARRAY_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -