基于 C++类模板 实现自定义数组

本文档展示了如何使用C++实现一个动态数组类`MyArray`,包括构造函数、拷贝构造函数、赋值运算符重载、下标访问、尾插法、尾删法等功能。并通过实例测试了内置类型和自定义类型数据的存储和操作。代码中着重处理了深拷贝问题,避免了浅拷贝导致的问题。
摘要由CSDN通过智能技术生成

第一次学习要为第二次学习做准备

需求分析

  • 可以对内置数据类型以及自定义数据类型的数据进行存储
  • 将数组中的数据存储到堆区
  • 构造函数中可以传入数组的容量
  • 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
  • 提供尾插法和尾删法对数组中的数据进行增加和删除
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组中当前元素个数和数组的容量

在这里插入图片描述

代码

myArray.cpp

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

template <class T>
class MyArray {
public :

	// 构造函数
	MyArray(int capacity) {
		this->m_Capacity = capacity;
		this->m_Size = 0;
		pAddress = new T[this->m_Capacity];
	}

	// 拷贝构造
	//  const  的作用在于 防止误操作
	MyArray(const MyArray &arr) {
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		// this->pAddress = arr.pAddress;   这里不能直接这样操作(这是指针,普通则可以)   浅拷贝
		this->pAddress = new T[this->m_Capacity];

		for (int i = 0; i < this->m_Size; i++) {
			this->pAddress[i] = arr.pAddress[i];
		}
	}


	// 重载  =     防止浅拷贝的问题
	MyArray& operator=(const MyArray& myarray) {
		//  判断原来堆区是否有数据,有则释放数据
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->m_Size = 0;
			this->m_Capacity = 0;
		}

		this->m_Capacity = myarray.m_Capacity;
		this->m_Size = myarray.m_Size;
		this->pAddress = new T[this->m_Capacity];

		for (int i = 0; i < this->m_Size; i++) {
			this->pAddress[i] = myarray.pAddress[i];
		}
		return *this;
	}

// 重载 []  
	T& operator[](int index) {
		return this->pAddress[index];
}

// 尾插法
	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--;
	}

	int getCapacity() {
		return this->m_Capacity;
	}
	int getSize() {
		return this->m_Size;
	}


	~MyArray() {
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}
	}
private :
	T* pAddress;  // 指向一个堆空间 ,存储真正的数据
	int m_Capacity ; 
	int m_Size;
};

数组类封装主代码

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


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


//  测试自定义数据类型
class Person {
public:
	Person() {};    // 无参数构造
	Person(string name, int age) {
		this->m_Age = age;
		this->m_Name = name;
	};
public:
	string m_Name;
	int m_Age;
};
//  输出
void printPersonArray(MyArray<Person> PA ) {
	for (int i = 0; i < PA.getSize(); i++) {
		cout << "年龄 :" << PA[i].m_Age << "姓名" << PA[i].m_Name << endl;
 	}
}


// 测试内置数据类型
void test10() {
	MyArray<int> array1(10);
	for (int i = 0; i < 10; i++) {
		array1.Push_back(i);
	}
	printArray(array1);
	cout << "array1 的大小" << array1.getSize() << endl;
	cout << "array1的容量" << array1.getCapacity() << endl;
		 
	cout << "-------------" << endl;
	MyArray<int> array2(array1);    // 拷贝数组  array1 
	array2.Pop_back();
	printArray(array2);
	cout << "array2 的大小" << array2.getSize() << endl;
	cout << "array2的容量" << array2.getCapacity() << endl;
}

// 测试自定义数据类型    不要好高鹜云   -->   不动手敲代码
void test11() {
	MyArray<Person> pArray(10);
	Person p1("周杰伦",12);
	Person p2("张杰", 11);
	Person p3("周杰", 13);
	Person p4("钟杰", 21);

	pArray.Push_back(p1);
	pArray.Push_back(p2);
	pArray.Push_back(p3);
	pArray.Push_back(p4);

	printPersonArray(pArray);
	cout << "pArray 的大小" << pArray.getSize() << endl;
	cout << "pArray的容量" << pArray.getCapacity() << endl;
}

int main() {

	test10();
	test11();

	system("pause");
	return 0;
}

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值