Class template case

demand

To implement a general array class, the requirements are as follows:

  • Data of built-in data type and custom data type can be stored.
  • Store the data in the array to the heap
  • The capacity of arrays that can be passed in the constructor
  • Provide the corresponding copy constructor and operator = to prevent shallow copy problems
  • Provide tail insertion and tail deletion to add and delete data in the array
  • Elements in an array can be accessed by subscript
  • You can get the number of current elements in the array and the capacity of the array

Test code

Code in myArray.hpp

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

template<class T>
class MyArray
{
public:
    
	//Constructor
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		pAddress = new T[this->m_Capacity];
	}

	//copy construction
	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++)
		{
			//If T is an object and contains pointers, you must overload the = operator, because this equals sign is not a construction but an assignment.
			// Normal type can be direct = but pointer type needs deep copy
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	//Overload = operators prevent shallow copy problems
	MyArray& operator=(const MyArray& myarray) {

		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->m_Capacity = 0;
			this->m_Size = 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[i];
		}
		return *this;
	}

	//Overload [] operator arr[0]
	T& operator [](int index)
	{
		return this->pAddress[index]; //Do not consider cross-border, users will handle it by themselves
	}

	//Tail insertion method
	void Push_back(const T & val)
	{
		if (this->m_Capacity == this->m_Size)
		{
			return;
		}
		this->pAddress[this->m_Size] = val;
		this->m_Size++;
	}

	//Tail deletion method
	void Pop_back()
	{
		if (this->m_Size == 0)
		{
			return;
		}
		this->m_Size--;
	}

	//Get array capacity
	int getCapacity()
	{
		return this->m_Capacity;
	}

	//Get array size
	int	getSize()
	{
		return this->m_Size;
	}


	//Deconstruction
	~MyArray()
	{
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}
	}

private:
	T * pAddress;  //Point to a heap space that stores real data
	int m_Capacity; //capacity
	int m_Size;   // Size
};

Class template case - in array class encapsulation.cpp

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

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

//Test built-in data types
void test01()
{
	MyArray<int> array1(10);
	for (int i = 0; i < 10; i++)
	{
		array1.Push_back(i);
	}
	cout << "array1 Printout:" << endl;
	printIntArray(array1);
	cout << "array1 Size:" << array1.getSize() << endl;
	cout << "array1 Capacity:" << array1.getCapacity() << endl;

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

	MyArray<int> array2(array1);
	array2.Pop_back();
	cout << "array2 Printout:" << endl;
	printIntArray(array2);
	cout << "array2 Size:" << array2.getSize() << endl;
	cout << "array2 Capacity:" << array2.getCapacity() << endl;
}

//Test custom data types
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 << "Full name:" << personArr[i].m_Name << " Age: " << personArr[i].m_Age << endl;
	}

}

void test02()
{
	//Create array
	MyArray<Person> pArray(10);
	Person p1("Sun WuKong", 30);
	Person p2("Hanxin", 20);
	Person p3("Da Ji", 18);
	Person p4("Wang Zhaojun", 15);
	Person p5("Zhao Yun", 24);

	//insert data
	pArray.Push_back(p1);
	pArray.Push_back(p2);
	pArray.Push_back(p3);
	pArray.Push_back(p4);
	pArray.Push_back(p5);

	printPersonArray(pArray);

	cout << "pArray Size:" << pArray.getSize() << endl;
	cout << "pArray Capacity:" << pArray.getCapacity() << endl;

}

int main() {

	//test01();

	test02();

	system("pause");

	return 0;
}

summary

  • Using previous knowledge to implement general array

Tags: P4

Posted on Sat, 02 Nov 2019 00:42:10 -0700 by Azazeal