类模板

模板类的声明,一般将声明和定义放在一个文件内,后缀为.hpp。

或者,在main函数中,将所包含的头文件后缀.h改为.cpp。

#pragma once
#include<iostream>

using namespace std;

template<class T>
class MyArray
{
public:
    MyArray(int capacity)
    {
        cout << "MyArray的有参构造调用" << endl;
        this->m_Capacity = capacity;
        this->m_Size = 0;
        this->pAddress = new T[capacity];
    }
    ~MyArray()
    {
        if (this->pAddress != NULL)
        {
            cout << "MyArray的析构构造调用" << endl;
            delete[] this->pAddress;
            this->pAddress = NULL;
        }
    }
    MyArray(const MyArray& arr)
    {
        cout << "MyArray的拷贝构造调用" << endl;
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;

        //深拷贝
        this->pAddress = new T[arr.m_Capacity];
        for (int i = 0; i < this->m_Size; i++)
        {
            this->pAddress[i] = arr.pAddress[i];
        }
    }
    //operator = 防止浅拷贝
    MyArray& operator=(const MyArray& arr)
    {
        cout << "MyArray的operator构造调用" << endl;
        if (this->pAddress != NULL)
        {
            delete[] this->pAddress;
            this->pAddress = NULL;
            this->m_Capacity = 0;
            this->m_Size = 0;
        }
        //深拷贝
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[this->m_Capacity];
        for (int i = 0; i < this->m_Size; i++)
        {
            this->pAddress[i] = arr.pAddress[i];
        }
        return *this;
    }
    //尾插法
    void push_back(const T& val)
    {
        //判断满不满
        if (this->m_Capacity == this->m_Size)
        {
            return;
        }
        this->pAddress[this->m_Size] = val;
        this->m_Size++;

    }
    //尾删法
    void pop_back()
    {
        if (this->m_Size == 0)
        {
            return;
        }
        this->m_Size--;  //用户访问不到
    }
    //通过下标访问数组中的元素
    T& operator[](int index)
    {
        return this->pAddress[index];
    }
    //返回数组的容量
    int getCapacity()
    {
        return this->m_Capacity;
    }
    //返回数组的大小
    int getSize()
    {
        return this->m_Size;
    }

private:
    T* pAddress;  //指向堆区开辟的真是数组

    int m_Capacity;   //数组容量
     
    int m_Size; //数组大小
};

main函数:

#include<iostream>
#include "MyArray.hpp"
using namespace std;


void printIntArray(MyArray<int>& arr)
{
    for (int i = 0; i < arr.getSize(); i++)
    {
        cout << arr[i] << endl;
    }
}
void test01()
{
    MyArray<int>arr1(5);
}
void test02()
{
    MyArray<int>arr1(5);

    for (int i = 0; i < 5; i++)
    {
        arr1.push_back(i);
    }
    printIntArray(arr1);
}
int main()
{
    test02();

    system("pause");
    return 0;
}

 还可以新建一个类当作T。

#include<iostream>
#include "MyArray.hpp"
using namespace std;
#include<string>

class Person
{
public:
    Person(string name, int age)
    {
        m_name = name;
        m_age = age;
    }
    Person() {};
    void printMsg()
    {
        cout << "/t姓名:" << m_name << "/t年龄:" << m_age << endl;
    }
private:
    string m_name;
    int m_age;
};

void printIntArray(MyArray<int>& arr)
{
    for (int i = 0; i < arr.getSize(); i++)
    {
        cout << arr[i] << endl;
    }
}
void test01()
{
    MyArray<int>arr1(5);
}
void test02()
{
    MyArray<int>arr1(5);

    for (int i = 0; i < 5; i++)
    {
        arr1.push_back(i);
    }
    printIntArray(arr1);
}
void test03()
{
    MyArray<Person>arr1(5);
    Person p1("恐龙", 100);
    Person p2("宝可梦", 20);
    arr1.push_back(p1);
    arr1.push_back(p2);
    for (int i = 0; i < arr1.getSize(); i++)
    {
        arr1[i].printMsg();
    }
}
int main()
{
    test03();

    system("pause");
    return 0;
}

 

posted @ 2021-10-18 16:06  为红颜  阅读(45)  评论(0编辑  收藏  举报