• 数组类的创建——StaticArray.h


    创建好的基于顺序存储结构的线性表存在两个方面的问题:
    1)功能上的问题:数组操作符的重载带来的问题,有可能线性表被无用为数组了,线性表被当做数组来使用了。
    2)效率方面的问题

    本篇博客就要解决功能上的问题。

    数组类的开发
    需要在DTLib中提供安全可靠的原生数组的代替品,原生数组C++是直接支持的,但是原生数组有其自己的劣势,不能提供数组的长度信息,并且无法检测当前的操作是否合法。

    ——完成Array类的具体实现
    ——完成StaticArray类的具体实现

    需求分析:
    ——创建数组类代替原生数组的使用
      数组类包含长度信息
      数组类能够主动发现越界访问

    Array设计要点
    ——抽象类模板,存储空间的位置和大小由子类完成
    ——重载数组操作符,判断访问下标是否合法
    ——提供数组长度的抽象访问函数
    ——提供数组对象间的复制操作

    Array.h

    #ifndef ARRAY_H
    #define ARRAY_H
    
    #include "Object.h"
    #include "Exception.h"
    
    namespace DTLib
    {
    template <typname T>
    class Array : public Object
    {
    protected:
        T* m_array;
    public:
        virtual bool set(int i, const T& e)
        {
            bool ret = ((0<=i) && (i< length()));
    
            if(ret)
            {
                m_array[i] = e;
            }
    
            return ret;
        }
        virtual boot get(int i, T& e) const
        {
            bool ret = ((0<=i) && (i< length()));
    
            if(ret)
            {
                e = m_array[i];
            }
    
            return ret;
        }
    
        //数组访问操作符
        T& operator[] (int i)
        {
            if((0<=i) && (i< length()))
            {
                return m_array[i];
            }
            else
            {
                THROW_EXCEPTION(IndexOutOfBoundsException,"Paramete i is invalid...");
            }
        }
        T operator[] (int i) const
        {
            return (const_cast< Array<T&> >(*this)[i]);
        }
        virtual int length() const = 0;
    };
    
    }
    
    #endif // ARRAY

    StaticArray设计要点:
    ——类模板
      封装原生数组
      使用模板参数决定数组大小
      实现函数返回数组长度
      拷贝构造和赋值操作

    #ifndef STATICARRAY_H
    #define STATICARRAY_H
    
    #include "Array.h"
    
    namespace DTLib
    {
    template <typename T, int N>
    class StaticArray : public Array<T>
    {
    protected:
        T m_space[N];
    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];
               }
            }
    
            return *this;
        }
    
        int length() const
        {
            return N;
        }
    };
    
    }
    
    #endif // STATICARRAY_H

    测试

    #include <iostream>
    #include "StaticArray.h"
    
    using namespace std;
    using namespace DTLib;
    
    int main()
    {
        StaticArray<int ,5> sl;
    
        for(int i=0; i<sl.length(); i++)
        {
            sl[i] = i * i;
        }
    
        for(int i=0; i<sl.length(); i++)
        {
            cout << sl[i] << endl;
        }
    
        return 0;
    
    }
  • 相关阅读:
    10个用jQuery实现图片幻灯片/画廊效果和源码
    老赵面试题参考答案(二)
    C#的显式接口和隐式接口
    老赵面试题参考答案(三)
    C#中的参数传递:值类型(value type)和引用类型(reference type)
    word转换成html的方法
    老赵面试题参考答案(四)
    五个Metro UI 风格的网页设计
    老赵面试题参考答案(六)
    概要设计怎么写?
  • 原文地址:https://www.cnblogs.com/-glb/p/12057430.html
Copyright © 2020-2023  润新知