C++类模版封装训练_黑马

  • 目标

内置数据类型和自定义数据进行存储
1.数组存入堆区
2.构造函数可 传入数组容量
3.提供对应拷贝构造函数 operator= 深拷贝,防止浅拷贝
4.提供尾插法 尾删法 对数组中数据进行 增加删除
5.通过index下标 访问数组元素
6.获取数组元素个数 数组容量

实现:

C++类模版封装训练_黑马_第1张图片

Code:

mArray.hpp

//通用数组类
#pragma once
#include
#include
using namespace std;

template<class T>
class mArray
{
private:
    T * pAddress;   //指针指向堆区 开辟真实数组
    int m_Capacity; //数组容量
    int m_Size;     //数组大小

public:
    mArray(int capacity);                   //有参构造,在堆区创建T数据类型
    mArray(const mArray& arr);              //拷贝构造      防止浅拷贝
    mArray& operator=(const mArray& arr);   //操作符重载    防止浅拷贝
    void Push_Back(const T& val);           //尾插法    const 常量修饰
    void Pop_Back();                        //尾删法
    T& operator[](int index);               //通过下标访问数组元素  重载[]运算符,且作为左值赋值操作,需要为 引用数据类型
    int getCapacity();                      //返回数组容量
    int getSize();                          //返回数组大小
    ~mArray();                              //析构函数      释放堆区数据
};

template<class T>
mArray<T>::mArray(int capacity){
    //cout<<"Test :mArray Func(T)"<
    this->m_Capacity = capacity;
    this->m_Size = 0;
    this->pAddress = new T[this->m_Capacity];
}

template<class T>
mArray<T>::mArray(const mArray& arr){
    //cout<<"Test :mArray Copy Func(T)"<
    this->m_Capacity = arr.m_Capacity;
    this->m_Size = arr.m_Size;
    //this->pAddress = arr.pAddress;    //指针 重写拷贝构造函数,进行深拷贝
    this->pAddress = new T[arr.m_Capacity]; //实现深拷贝(22.cpp)
    for (int i = 0; i < this->m_Size; i++)
    {
        this->pAddress[i] = arr.pAddress[i];
    }
}

template<class T>
mArray<T>& mArray<T>::operator=(const mArray& arr){    //operator= 防止浅拷贝
    //先判断堆区是否有数据,如果有 先释放
    //cout<<"Test :mArray Operator="<
    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[arr.m_Capacity];
    for (int i = 0; i < this->m_Size; i++)
        this->pAddress[i] = arr.pAddress[i];
    return *this;   //解引用
}

template<class T>
void mArray<T>::Push_Back(const T& val){    //尾插法
    //判断容量是否满
    if (this->m_Capacity == this->m_Size){
        cout<<"Array is FULL!!!"<<endl;
        return;
    }
    this->pAddress[this->m_Size] = val;     //末尾插入数据
    this->m_Size++;                         //更新数组大小
}

template<class T>
void mArray<T>::Pop_Back(){    //尾删法
    //让用户无法访问最后元素,尾删成功。    属于逻辑删除
    if (0 == this->m_Size)   //判断数据是否为空,空 无法删除
        return;
    this->m_Size--;
}

template<class T>
T& mArray<T>::operator[](int index){
    return this->pAddress[index];
}

template<class T>
int mArray<T>::getCapacity(){
    return m_Capacity;
}

template<class T>
int mArray<T>::getSize(){
    return m_Size;
}

template<class T>
mArray<T>::~mArray(){
    if (this->pAddress != NULL){
        //cout<<"Test :mArray DelFunc(T)"<
        delete[] this->pAddress;
        this->pAddress = NULL;
    }
}


ClassTemplate_Train

#include
#include
#include"mArray.hpp"
using namespace std;
/* 
    内置数据类型和自定义数据进行存储
        1.数组存入堆区
        2.构造函数可 传入数组容量
        3.提供对应拷贝构造函数 operator= 深拷贝,防止浅拷贝
        4.提供尾插法 尾删法 对数组中数据进行 增加删除
        5.通过index下标 访问数组元素
        6.获取数组元素个数 数组容量

    Goal:
    定义 Class mArray{
        private
            T * pAddress;   //数组(开辟在堆区)    T* = new T[len];
            m_Capacity;     //容量
            m_Size;         //大小
        public:
            构造函数(容量);
            拷贝构造;
            operator=;
            index访问;
            尾插法;
            尾删法;
            获取数组容量;
            获取数组大小;
            ~析构;
        }
 */

void printIntArray(mArray<int>& arr){           //打印 T == int 类型数据
    for (int i = 0; i < arr.getSize(); i++)
    {
        cout<<arr[i]<<" ; ";
    }
    cout<<endl;
}

void test01(){
    mArray <int>arr1(5);    //有参构造
    mArray <int>arr2(arr1); //实现 深拷贝
    mArray <int>arr3(100);  //有参
    arr3 = arr1;            //operator +调用
}

void test02(){
    int len = 5;
    mArray <int>arr4(len);
    for (int i = 0; i < len; i++)
    {
        arr4.Push_Back(i);          //利用尾插法 向数组插入数据
    }
    cout<<"Array arr4's Data is :"<<endl;
    printIntArray(arr4);

    cout<<"Array arr4's Capacity : "<<arr4.getCapacity()<<endl
        <<"Array arr4's Size : "<<arr4.getSize()<<endl;

    mArray <int>arr5(arr4);
    cout<<"Array arr5's Data is :"<<endl;
    printIntArray(arr5);

    arr5.Pop_Back();                //尾删测试

    cout<<"PopBack Done!!!"<<endl
        <<"Array arr5's Data is :"<<endl;
    printIntArray(arr5);

    cout<<"Array arr5's Capacity : "<<arr5.getCapacity()<<endl
        <<"Array arr5's Size : "<<arr5.getSize()<<endl;
    
}


//测试 自定义数据类型
class Person
{
private:
    string m_Name;
    int m_Age;
public:
    Person();
    Person(string name,int age);
    string getName();
    int getAge();
};
Person::Person(){}

Person::Person(string name,int age){
    this->m_Name = name;
    this->m_Age = age;
}

string Person::getName(){
    return m_Name;
}

int Person::getAge(){
    return m_Age;
}

void printPersonArray(mArray<Person>& arr){     //打印 T = Person 类型数据
    for (int i = 0; i < arr.getSize(); i++)
        cout<<"Name : "<<arr[i].getName()<<"\tAge : "<<arr[i].getAge()<<endl;
    cout<<endl;
}

void test03(){          //测试自定义 Person数据类型
    mArray<Person> arr6(10);
    Person p1("GodOuO",23);
    Person p2("Wendy",23);
    Person p3("Sean",23);
    Person p4("Tom",999);
    Person p5("Jerry",998);
    //数据插入数组中
    arr6.Push_Back(p1);
    arr6.Push_Back(p2);
    arr6.Push_Back(p3);
    arr6.Push_Back(p4);
    arr6.Push_Back(p5);

    printPersonArray(arr6);

    cout<<"Array arr6's Capacity : "<<arr6.getCapacity()<<endl
        <<"Array arr6's Size : "<<arr6.getSize()<<endl;
}

int main(){
    test01();
    test02();
    test03();
}

你可能感兴趣的:(C/C++实践,c++,算法,数据结构)