C++类模板实例:数组类封装

实现功能:

对于用户提供的数据类型能够实现①整个数组赋值(拷贝)②插入与删除 ③获得数组的大小和容量

头文件 “MyArray.hpp" :

#pragma once
#include<iostream>
using namespace std;
template <class T>
class MyArray
{
public:
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[capacity];
	}
	MyArray(const MyArray& arr)
	{
		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];
		}
	}
	~MyArray()
	{
		if (this->pAddress)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
	MyArray& operator=(const MyArray& arr)
	{
		if (this->pAddress)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
		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];
		}
	}
	void Push_Back(const T& val)
	{
		if (this->m_Capacity == this->m_Size)
		{
			cout << "The Array is full" << endl;
			return;
		}
		this->pAddress[this->m_Size] = val;
		this->m_Size++;
	}
	void Pop_Back()
	{
		if (this->m_Size)this->m_Size--;
		else return;
	}
	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;
};

 主函数测试:

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

class Person
{
public:
	Person(){}
	Person(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}
	string m_Name;
	int m_Age;
};
void Print( MyArray<Person>& arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr[i].m_Name << " " << arr[i].m_Age << endl;
	}
}
void test()
{
	MyArray<Person> arr(10);
	Person p1("Vi", 19);
	Person p2("jinx", 16);
	Person p3("Caitlyn", 19);
	Person p4("Worick", 45);
	Person p5("Victor", 25);
	arr.Push_Back(p1);
	arr.Push_Back(p2);
	arr.Push_Back(p3);
	arr.Push_Back(p4);
	arr.Push_Back(p5);
	Print(arr);
	cout << "Capacity: " << arr.getCapacity() << endl;
	cout << "Size: " << arr.getSize() << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

相关推荐

  1. C++模板实例数组封装

    2024-02-07 14:28:01       47 阅读
  2. C++实现Vector模板

    2024-02-07 14:28:01       32 阅读
  3. C++ 字符串 简易封装

    2024-02-07 14:28:01       42 阅读
  4. C/C++ - 模板

    2024-02-07 14:28:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-07 14:28:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-07 14:28:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-07 14:28:01       82 阅读
  4. Python语言-面向对象

    2024-02-07 14:28:01       91 阅读

热门阅读

  1. Synchronized 和 ReentrantLock 的区别

    2024-02-07 14:28:01       56 阅读
  2. Dockerfile和.gitlab-ci.yml文件模板

    2024-02-07 14:28:01       57 阅读
  3. 什么是可解释AI

    2024-02-07 14:28:01       47 阅读
  4. 详解Non-ASCII character ‘\xe6‘

    2024-02-07 14:28:01       51 阅读
  5. Spring Cloud Netflix Eureka的参数调优

    2024-02-07 14:28:01       44 阅读
  6. 融转通与转融通

    2024-02-07 14:28:01       53 阅读
  7. 数据分析之数据预处理、分析建模、可视化

    2024-02-07 14:28:01       46 阅读
  8. PostgreSQL开启wal日志归档模式

    2024-02-07 14:28:01       41 阅读
  9. 【vue+vex-table】翻页保留复选框选中状态

    2024-02-07 14:28:01       51 阅读
  10. Web安全

    2024-02-07 14:28:01       50 阅读
  11. C++学习:string的了解

    2024-02-07 14:28:01       42 阅读