Array class encapsulation case C++ template

Array class packaging case: To
implement a general array class, the requirements are as follows:
·Can store built-in data types and custom types of data
· Store the data in the array in the heap area
·The capacity
of the array can be passed into the constructor · Provide the corresponding copy constructor and operater= to prevent shallow copy problems
. Provide tail interpolation and tail deletion methods to add and delete data in the
array.
You can access the elements in the array by subscript . You can get the current element in the array. Number and capacity of array

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

template<class T>
class MyArray
{
    
    
	public:
		
		//有参构造	参数	容量 
		MyArray(int capacity){
    
    
			//cout << "MyArray 的构造函数调用" << endl; 
			this -> m_Capacity = capacity;
			this -> m_Size = 0;
			this -> pAddress = new T[this -> m_Capacity];
		}
		
		//拷贝构造函数
		MyArray(const MyArray& arr){
    
    
			//cout << "MyArray 的拷贝构造函数调用" << endl;
			this -> m_Capacity = arr.m_Capacity;
			this -> m_Size = arr.m_Size;
			//this -> pAddress = arr.pAddress;
			//深拷贝 
			this -> pAddress = new T[arr.m_Capacity];
			
			//将arr中的数据都拷贝过来
			for (int i = 0; i < this -> m_Size; i ++){
    
    
				this -> pAddress[i] = arr.pAddress[i];
			} 	
		 } 
		
		//operator= 防止浅拷贝问题 a = b =c
		MyArray & operator= (const MyArray & arr){
    
    
			//cout << "MyArray 的operator=函数调用" << endl;
			//先判断原来堆区是否有数据,如果有先释放
			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;
		}
		
		void Push_Back(const T & val){
    
    
			//判断容量是否等于大小
			if (this -> m_Capacity == this -> m_Size){
    
    
				cout << "容量已满" << endl; 
				return;
			} 
			this -> pAddress[this -> m_Size] = val;	//在数组末尾插入数据 
			this -> m_Size ++;	//更新数组大小 
		}
		
		void Pop_Back(){
    
    
			//让用户访问不到最后一个元素,即为尾删,逻辑删除
			if(this -> m_Size == 0){
    
    
				cout << "容器已空" << endl;
				return; 
			} 
			this -> m_Size --;
		 } 
		 
		//通过下标方式访问数组中的元素	arr[0] = 10;&返回引用可以作为左值存在 
		T & operator[](int index){
    
    
			return this -> pAddress[index]; 
		} 
		
		//返回数组容量
		int getCapacity(){
    
    
			return this -> m_Capacity;
		} 
		
		//返回数组大小
		int getSize(){
    
    
			return this -> m_Size;
		} 
		
		//析构函数
		~MyArray(){
    
    
			if (this -> pAddress != NULL){
    
    
				//cout << "MyArray 的析构函数调用" << endl;
				delete[] this -> pAddress;
				this -> pAddress = NULL;
			}
		 } 
	private:
		T * pAddress;		//指针指向堆区开辟的真实数组
		int m_Capacity;		//数组容量
		int m_Size;			// 
};
//################以下为测试代码#############
class Person
{
    
    
	public:
		Person(){
    
    
		};
		Person(string name, int age){
    
    
			this -> m_Name = name;
			this -> m_Age = age; 
		}
		
		string m_Name;
		int m_Age;
	
};

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

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

void test2()
{
    
    
	MyArray<Person> arr(10);
	Person p1("孙悟空", 999);
	Person p2("猪八戒", 888);
	Person p3("沙悟净", 777);
	Person p4("白龙马", 666);
	Person p5("唐三藏", 25);
	arr.Push_Back(p1);
	arr.Push_Back(p2);
	arr.Push_Back(p1);
	arr.Push_Back(p3);
	arr.Push_Back(p4);
	arr.Push_Back(p5);
	printPersonArray(arr);
	cout << arr.getCapacity() << endl;
	cout << arr.getSize() << endl;
	
	arr.Pop_Back();
	printPersonArray(arr);
	
	cout << arr.getCapacity() << endl;
	cout << arr.getSize() << endl;
}

void test()
{
    
    	
	MyArray<int> arr1(5);
	for (int i = 0; i < 5; i ++){
    
    
		arr1.Push_Back(i);
	}
	cout << "arr1的打印输出" << endl;
	printIntArray(arr1);
	cout << "arr1的rongliang  : " << arr1.getCapacity() << endl;
	cout << "arr1的rongliangde daxiao  : " << arr1.getSize() << endl;
	
	MyArray<int> arr2(arr1);
	arr2.Pop_Back();
	cout << "arr2尾删后的打印输出" << endl;
	cout << "arr2的rongliang  : " << arr2.getCapacity() << endl;
	cout << "arr2的rongliangde daxiao  : " << arr2.getSize() << endl;
	//MyArray<int> arr3(100);
	//arr3 = arr1;
}

int main(){
    
    
	test2();
	test();
	system("pause");
}

Dark horse programmer C++ course case exercises

Guess you like

Origin blog.csdn.net/Fighting_gua_biu/article/details/113583087