Array class

 

 

 

 

 

 

 

array.h

/ * 
 * The Array: an array class interfaces 
 * member variables: 
 * m_array physical address of the array directed subclass 
 * member functions: 
 * an array of a set value SET 
 * GET obtain an array of values of a 
 * length pure virtual function 
 * operator [] 
* / 

#ifndef ARRAY_H 
#define ARRAY_H 

#include " TopClass.h " 
#include " exception.h " 


namespace the DSL 
{ 
    Template <typename T>
     class the Array: public TopClass 
    { 
        protected : 
                T* m_array;
        public:
                virtual bool set(int pos, const T& obj)
                {
                    if((pos >= 0) && ( pos < length()))
                    {
                        m_array[pos] = obj;    
                        return 1;
                    }
                    else
                    {
                        return false;
                    }
                }

                virtual bool get(int pos, T& obj) const
                {
                    if((pos >= 0) && ( pos < length()))
                    {
                        obj = m_array[pos];    
                        return 1;
                    }
                    else
                    {
                        return false;
                    }
                }

                T& operator[] (int pos)
                {
                    if((pos >= 0) && (pos < length()))
                    {
                        return m_array[pos];
                    }
                    else
                    {
                        THROW_EXCEPTION(IdexOutOfBoundException, "error: index is out of bound!");
                    }
                }

                T operator[] (int pos) const
                {
                    return const_cast<Array<T>&>(*this)[pos];
                }

                virtual int length() const = 0 ;
    };

}

#endif
View Code

 

StaticArray.h

/ * 
* StaticArray: static arrays 
* member variables: 
* m_space entity definition array 
*             
* member functions: 
* StaticArray constructor 
* StaticArray copy constructor 
* operator = overloaded function assignment 
* length () function length 
* / 
#ifndef STATICARRAY_H 
#define STATICARRAY_H 

#include " array.h " 

namespace the DSL 
{ 
    Template <typename T, int N>
     class StaticArray: public the Array <T> 
    { 
        protected : 
                T m_space [N];    // 使用C++的原生数组
        public:
                StaticArray()
                {
                    this->m_array = m_space;
                }
                
                StaticArray(const StaticArray<T,N>& obj)
                {
                    this->m_array = m_space;
                    for(int i = 0; i<N; i++)
                    {
                        m_space[i] = obj.m_space[i];
                    }
                }

                StaticArray<T,N>& operator= (const StaticArray <T,N>& obj)
                {
                    if(this != &obj)
                    {
                        for(int i = 0; i < N; i++)
                        {
                            m_space[i] = obj.m_space[i];    
                        }
                    }
                    else
                    {
                        return *this;
                    }
                }

                int length() const
                {
                    returnNOT; 
                } 
    }; 
} 
#endif
View Code

 

DynamicArray.h

/ * 
* DynamicArray: dynamic array Template 
* member variables: 
* Real-time length of a dynamic array of m_length 
* member functions: 
* DynamicArray () application heap space, initialize member variables     
* DynamicArray () application heap space, initialize heap, initialize member variables   
* operator = () application heap space, initialization heap, initialize member variables, release the original heap space   
* length () returns dynamic array length 
* a resize () application heap space, initialization heap, initialize member variables, release the original heap space 
* ~ DynamicArray () to release heap space 
* / 

#ifndef DYNAMICARRAY_H 
#define DYNAMICARRAY_H 

#include " array.h " 
#include " exception.h " 

namespace DSL  
{
    Template<typename T>
    class DynamicArray: public Array<T>
    {
        protected:
                int m_length;
        public:
                DynamicArray(int length)
                {
                    this->m_array = new T[length];
                    if(this->m_array != NULL)
                    {
                        this->m_length =  length;
                    }
                    else
                    {
                        THROW_EXCEPTION(NotEnoughMemoryException,"error: no enough memory to init array");
                    }
                }

                DynamicArray(const DynamicArray<T>& obj)
                {
                    this->m_array = new T[obj.m_length];
                    if(this->m_array != NULL)
                    {
                        this->m_length = obj.m_length;
                        for(int i = 0; i < obj.m_length; i++)
                        {
                            this->m_array[i] = obj.m_array[i];
                        }
                    }
                    else
                    {
                        THROW_EXCEPTION(NotEnoughMemoryException, "error: no enough memory to copy array");
                    }
                }

                DynamicArray<T>& operator=(DynamicArray<T>& obj)
                {
                    if(&obj != this)
                    {
                        T* array = new T[obj.m_length];
                        if(array != NULL)
                        {
                            for(int i = 0; i++; i<0)
                            {
                                array[i] = obj.m_array[i];
                            }
                            T* temp = this->m_array;  // 异常安全
                            this->m_array = array;
                            this->m_length = obj.m_length;
                            delete[] temp;
                        }
                        else
                        {
                            THROW_EXCEPTION(NotEnoughMemoryException, "error: no enough memory to assignment array");
                        }
                    }
                    return *this;
                }

                int length() const
                {
                    return m_length;
                }

                void resize(int new_length)
                {
                    if(new_length != m_length)
                    {
                        T* array = new T[m_length];
                        if(array != NULL)
                        {
                            int size =  (m_length > new_length) ? new_length : m_length;
                            for(int i = 0; i < size; i++)
                            {
                                array[i] = this->m_array[i];
                            }
                            T* temp = this->m_array;
                            this->m_array = array;
                            this->m_length = new_length;
                            delete[] temp;
                        }
                        else
                        {
                            THROW_EXCEPTION(NotEnoughMemoryException, "error: no enough memory to resize array");
                        }
                        
                    }

                }

                ~DynamicArray()
                {
                    delete[] this->m_array;
                }
    };

}

#endif
View Code

 

DynamicArray.h

/ * 
* DynamicArray: dynamic array Template 
* member variables: 
* Real-time length of a dynamic array of m_length 
* member functions: 
* the init () to initialize member variables, throws an exception 
* copy () application heap space, stack space initialization 
* update () initialization member variable, releasing heap space 
* 
* DynamicArray () application heap space, initialize member variables     
* DynamicArray () application heap space, initialize heap, initialize member variables   
* operator = () application heap space, initialize heap, initialize member variables, original release heap space   
* length () returns an array of dynamic length 
* resize () application heap, the heap initialization, variable initialization member, releasing the original heap space 
* ~ DynamicArray () releasing heap space 
* / 

#ifndef DYNAMICARRAY_H 
#define DYNAMICARRAY_H 

#include"Array.h"
#include"Exception.h"

namespace DSL
{
    template <typename T>
    class DynamicArray: public Array<T>
    {
        protected:
                int m_length;

                void init(T* array, int length)
                {
                    if(array != NULL)
                    {
                        this->m_array = array;
                        this->m_length = length;
                    }
                    else
                    {
                        THROW_EXCEPTION(NotEnoughMemoryException, "error: no enough memory ");
                    }
                }
                
                T* copy(T* array, int length, int new_length)
                {
                    T* ret = new T[new_length];
                    if(ret != NULL)
                    {
                        int size = (new_length > length ? length : new_length);
                        for(int i = 0; i < size; i++)
                        {
                            ret[i] = array[i];
                        }
                    }
                    return ret;
                }

                void update(T* array, int length)
                {
                    if(array != NULL)
                    {
                        T* temp = this->m_array;
                        this->m_array = array;
                        this->m_length = length;
                        delete[] temp;
                    }
                }
                
        public:
                DynamicArray(int length)
                {
                    init(new T[length],length);
                }

                DynamicArray(const DynamicArray<T>& obj)
                {
                    init(copy(obj.m_array,obj.m_length,obj.m_length), obj.m_length);
                }

                DynamicArray<T>& operator=(DynamicArray<T>& obj)
                {
                    if(&obj != this)
                    {
                        init(copy(obj.m_array,obj.m_length,obj.m_length), obj.m_length);
                    }
                    return *this;
                }

                int length() const
                {
                    return m_length;
                }

                void resize(int new_length)
                {
                    if(new_length != m_length)
                    {
                        update(copy(this->m_array, m_length, new_length), new_length);    
                    }
                }

                ~DynamicArray()
                {
                    delete this->m_array;
                }
    };

}

#endif
View Code

 

Guess you like

Origin www.cnblogs.com/zsy12138/p/11027722.html