zoukankan      html  css  js  c++  java
  • 类模板在项目开发中的应用

    需求:设计一个数据模板类(MyVector),完成对int、char、Teacher类型元素的管理。

    思路:类模板 构造函数 拷贝构造函数 <<运算符重载  []重载 =运算符重载

    实现:

    MyVector.h:

    #include<iostream>
    using namespace std;
    
    template<typename T>
    class MyVector
    {
        friend ostream &operator<< <T> (ostream& out, const MyVector& obj);
    public:
        MyVector (int size = 0);//构造函数
        MyVector(const MyVector& obj); //拷贝构造函数
    
        ~MyVector();//析构函数
    
    public:
        T& operator[] (int index);
        MyVector& operator=(const MyVector& obj);
    public:
        int getLen()
        {
            return m_len;
        }
    protected:
        T* m_space;
        int m_len;
    };

    MyVector.cpp

    #include<iostream>
    using namespace std;
    #include"MyVector.h"
    
    
    template<typename T>
    ostream& operator<<(ostream& out, const MyVector<T>& obj)
    {
        for (int i = 0; i < obj.m_len; i++)
        {
            out << obj.m_space[i] << " ";
        }
        out << endl;
        return out;
    }
    
    template<typename T>
    
    //MyVector<T> myv1(10);
    MyVector<T>::MyVector(int size)//构造函数
    {
        m_space = new T(size);
        m_len = size;
    }
    
    
    
    template<typename T>
    MyVector<T>::MyVector(const MyVector& obj)//拷贝构造函数
    {
        //根据myv1的大小分配内存
        m_len = obj.m_len;
        m_space = new T[m_len];
    
        //copy数据
        for (int i = 0; i < m_len; i++)
        {
            m_space[i] = obj.m_space[i];
        }
    
    }
    template<typename T>
    MyVector<T>::~MyVector()//析构函数
    {
        if (m_space != NULL)
        {
            delete[] m_space;
            m_space = NULL;
            m_len = 0;
        }
    }
    template<typename T>
    T& MyVector<T>::operator[] (int index)
    {
        return m_space[index];
    }
    
    //a3 = a2 = a1;
    template<typename T>
    MyVector<T>& MyVector<T>::operator=(const MyVector<T>& obj)//拷贝构造函数
    {
        //先把a2的旧内存释放掉
        if (m_space != NULL)
        {
            delete[] m_space;
            m_space = NULL;
            m_len = 0;
        }
    
        //根据a1分配内存
        m_len = obj.m_len;
        m_space = new T[m_len];
        
        //copy数据
        for (int i = 0; i < m_len; i++)
        {
            m_space[i] = obj[i];
        }
    
        return *this;//a2 = a1,返回的是a2的自身
    }

    test.cpp:

    #include<iostream>
    using namespace std;
    #include"MyVector.cpp"
    
    int main()
    {
    
        MyVector<int> myv1(10);
        for (int i = 0; i < myv1.getLen(); i++)
        {
            myv1[i] = i + 1;
            cout << myv1[i] << " ";
        }
        cout << endl;
    
        MyVector<int>myv2 = myv1;
        for (int i = 0; i < myv2.getLen(); i++)
        {
            myv2[i] = i + 1;
            cout << myv2[i] << " ";
        }
        cout << myv2 << endl;
        
    
    
        system("pause");
        return 0;
    }
  • 相关阅读:
    ExtJS Form布局
    UML系列 (六)如何提取用例技术?
    牛腩新闻发布系统 (4)验证码的生成
    DIV +CSS 系列详细教程 (一)初识
    Java、JavaScript、asp.net 、jquery 详细分析
    设计模式详细系列教程 (三)以网上购物通用的订单流程 详解状态模式
    设计模式详细系列教程 (四) 无处不在的单例模式
    SCM软件配置管理 (二) SVN管理平台搭建以及详细设置
    JAVA JDK环境变量的配置
    牛腩新闻发布系统 (5) 总结
  • 原文地址:https://www.cnblogs.com/ymj11/p/13747006.html
Copyright © 2011-2022 走看看