19_数组类的创建(上)

作者: 编程半岛 | 来源:发表于2018-01-23 20:48 被阅读9次

关键词:Array.h的实现、StaticArray.h的实现

1. 课程目标

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

2. 需求分析

创建数组类代替元素数组的使用
1)数组类包含长度信息
2)数组类能够主动发现越界访问

3. Array设计要点

  • 抽象类模板,存储空间的位置和大小由子类完成
  • 重载数组操作符,判断访问下标是否合法
  • 提供数组长度的抽象访问函数
  • 提供数组对象间的赋值操作

4. Array类的声明和实现

Array类的声明

Array.h

#ifndef ARRAY_H
#define ARRAY_H

#include "Object.h"
#include "Exception.h"

namespace DTLib
{

template <typename 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 bool 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, "Parameter i is invaild...");
        }
    }

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

    virtual int length() const = 0;
};

}

#endif // ARRAY_H

5. StaticArray设计要点

类模板

  • 封装原生数组
  • 使用模板参数决定数组大小
  • 实现函数返回数组长度
  • 拷贝构造赋值操作

6. StaticArray的声明和实现

StaticArray声明
StaticArray.h
#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

声明:此文章仅是本人在学习狄泰学院《数据结构实战开发教程》所做的笔记,文章中包含狄泰软件资料内容,一切版权归狄泰软件所有!
实验环境:ubuntu10 + Qt Creator2.4.1 + Qt SDK 4.7.4

相关文章

  • 19_数组类的创建(上)

    关键词:Array.h的实现、StaticArray.h的实现 1. 课程目标 完成Array类的具体实现 完成S...

  • Foundation基础类库容器

    基础类库:容器 1.NSArray //类方法数组创建 //实例方法创建数组 //数组个数 //访问元素 //追加...

  • 数组的基本操作

    遍历数组 例题 创建类Trap 创建类Tautog 填充替换数组元素 1.fill(int[] a,int val...

  • 第 13 章 数组类

    第 13 章 数组类 13.1 复习数组 数组是带索引的对象的集合。 13.2 数组的创建方法 使用 [] 创建数...

  • Java主要数据结构总结

    数组线性表类ArrayList 和链表类LinkedList ArrayList用数组存储元素,这个数组是动态创建...

  • Rxjava常用apiDemo

    创建类 ##转换类 ##过滤类##时间类##截取类。 createfrom fromArray(数组) from...

  • 类模版创建数组

    环境:ide:Mac+clion 视频链接:https://www.bilibili.com/video/BV1H...

  • JavaSE之数组

    六、数组 目录:数组概述、数组声明创建、数组使用、多维数组、Array类、稀疏数组 1.什么是数组 数组的定义:数...

  • Numpy-创建数据

    创建数据 创建ndarray NumPy的数组类被称作ndarray。通常被称作数组。 Numpy库中的矩阵模块为...

  • numpy_1

    ndaray 随机创建 ndarray的序列创建 ndarray 的arange 和 reshape 数组的数据类...

网友评论

    本文标题:19_数组类的创建(上)

    本文链接:https://www.haomeiwen.com/subject/xiosaxtx.html