C++的类模板,重写运算符

写这些只是单纯的为了打个笔记,方便自己后面回来看,所以写的粗糙至极,你可以指出,相互学习,今天的是可莉

这是一个myArray.hpp文件

#pragma once
#include <iostream>
using namespace std;

template<class T>
class myArray
{
private:
    int m_Capccity; // 总容量大小
    int m_Size; // 当前大小
    T * pAddress;
public:
    //构造函数
    myArray(int capacity)
    {
        this->m_Capccity = capacity;
        this->m_Size = 0;
        this->pAddress = new T[this->m_Capccity];
    }
    //析构函数
    ~myArray()
    {
        if(this->pAddress != NULL)
        {
            delete [] this->pAddress;
            this->pAddress = NULL;
            this->m_Size = 0;
            this->m_Capccity = 0;
        }
    }
    //拷贝构造
    myArray(const myArray &arr)
    {
        this->m_Capccity = arr.m_Capccity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[this->m_Capccity];
        for (int i = 0; i < this->m_Size; i++)
        {
            this->pAddress[i] = arr.pAddress[i];
        }
    }
    //重载= 操作符  防止浅拷贝问题
    myArray& operator=(const myArray& arr)
    {
        if(this->pAddress != NULL)
        {
            delete [] this->pAddress;
            this->pAddress = NULL;
            this->m_Size = 0;
            this->m_Capccity = 0;
        }
        this->m_Capccity = arr.m_Capccity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[this->m_Capccity];
        for (int i = 0; i < this->m_Size; i++)
        {
            this->pAddress[i] = arr.pAddress[i];
        }
        return *this;
    }
    //重载[] 操作符  arr[0]
    T& operator[](int index)
    {
        return this->pAddress[index];
    }
    //尾插法
    void Push_back(const T& val)
    {
        if(this->m_Size == this->m_Capccity)
        {
            return;
        }
        this->pAddress[this->m_Size] = val;
        this->m_Size++;
    }
    //尾删法
    void Pop_back()
    {
        if(this->m_Size == 0)
        {
            return;
        }
        this->m_Size--;
    }
    //获取数组容量
	int getCapacity()
	{
		return this->m_Capccity;
	}

	//获取数组大小
	int	getSize()
	{
		return this->m_Size;
	}
};

这个用来测试 test_myArray.cpp

// run 
// g++ .\test_myArray.cpp -o .\test; chcp 65001 ; .\test

#include "myArray.hpp"
#include <string>


void printArray(myArray<int>& arr) {
	for (int i = 0; i < arr.getSize(); i++) {
		cout << arr[i] << " ";
	}
	cout << endl;
}

//测试内置数据类型
void test01()
{
	myArray<int> array1(10);
	for (int i = 0; i < 10; i++)
	{
		array1.Push_back(i);
	}
	cout << "array1打印输出 " << endl;
	printArray(array1);
	cout << "array1大小 " << array1.getSize() << endl;
	cout << "array1总大小 " << array1.getCapacity() << endl;

	cout << "--------------------------" << endl;

	myArray<int> array2(array1);
	array2.Pop_back();
	cout << "array2打印输出 " << endl;
	printArray(array2);
	cout << "array2大小 " << array2.getSize() << endl;
	cout << "array2总大小 " << array2.getCapacity() << endl;
}

//测试自定义数据类型
class Person {
public:
	Person() {} 
		Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
public:
	string m_Name;
	int m_Age;
};

void printPersonArray(myArray<Person>& personArr)
{
	for (int i = 0; i < personArr.getSize(); i++) {
		cout << "姓名:" << personArr[i].m_Name << " 年龄: " << personArr[i].m_Age << endl;
	}

}

void test02()
{
	//创建数组
	myArray<Person> pArray(10);
	Person p1("烦烦烦", 2);
	Person p2("反反复复", 3);
	Person p3("呃呃呃呃", 5);
	Person p4("涛涛涛涛", 7);
	Person p5("改观古港", 9);
	Person p6("会让他", 11);
	Person p7("发生太热", 13);

	//插入数据
	pArray.Push_back(p1);
	pArray.Push_back(p2);
	pArray.Push_back(p3);
	pArray.Push_back(p4);
	pArray.Push_back(p5);
	pArray.Push_back(p6);
	pArray.Push_back(p7);

	printPersonArray(pArray);

	cout << "pArray大小 " << pArray.getSize() << endl;
	cout << "pArray总大小 " << pArray.getCapacity() << endl;
    
    cout << "--------------------------------" << endl;

	myArray<Person> pArray2(pArray);
	printPersonArray(pArray2);
	pArray2.Pop_back();
    cout << "----------------------111----------" << endl;
	printPersonArray(pArray2);
    cout << "----------------------2222----------" << endl;
    for (int i = 0; i < pArray2.getSize() + 1; i++) {
		cout << "姓名:" << pArray2[i].m_Name << " 年龄: " << pArray2[i].m_Age << endl;
	}
    cout << "pArray2大小 " << pArray2.getSize() << endl;
	cout << "pArray2总大小 " << pArray2.getCapacity() << endl;
}


int main()
{
    test01();
    cout << "111111111111111111" << endl;
    test02();

    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值