c++模板学习12之通用数组类模板案例封装

2021-03-02 16:56:38 浏览数 (1)

通用数组类模板案例封装

arr.hpp

代码语言:javascript复制
#pragma once
#include<iostream>
using namespace std;
//通用的数组模板类
template<class T>
class myarray
{
private:
  //数组指针,指向自定义T类型的数组首地址
	T* arr;
  //数组容量
	int capicity;
  //数组元素个数
	int size;
public:
	//构造函数初始化
	myarray(int cap) 
	{
		//cout << "1";
		this->capicity = cap;
		arr = new T[capicity];
		size = 0;
	}
	//深拷贝构造,防止浅拷贝
	myarray(const myarray& a)
	{
		//cout << "2";
		//浅拷贝会带来堆区内存重复释放
		//arr = a.arr;
		this->capicity = a.capicity;
		this->size = a.size;
		this->arr = new T[a.capicity];
		for (int i =0 ;i <a.size; i  )
		{
			this->arr[i] = a.arr[i];
		}
	}
	//赋值运算符重载,防止浅拷贝  a=b=c链式编程
	myarray& operator=(const myarray& a)
	{
		//cout << "3";
		if (arr != NULL)
		{
			delete[] arr;
			arr = NULL;
			capicity = 0;
			size = 0;
		}
		//非自赋值
		if (this != &a)
		{
			capicity = a.capicity;
			size = a.size;
			arr = new T[a.capicity];
			for (int i = 0; i < a.size; i  )
			{
				arr[i] = a.arr[i];
			}
		}
		return *this;
	}
	//尾插法
	void push_back(const T& val)
	{
		//判断容量是否等于大小
		if (capicity == size)
		{
			cout << "数组容量已满" << endl;
			return;
		}
		this->arr[size] = val;
		size  ;
	}

	//尾删法
	void end_del()
	{
		//让用户无法访问最后一个元素即为尾删
		if (size == 0)
			return;
		size--;
	 }
	//通过下标访问数组中元素---[]运算符重载
	T& operator[](int index)
	{
		return  this->arr[index];
	}
	//返回数组的容量
	int getCap()
	{
		return   capicity;
	}
	//返回数组大小
	int getSize()
	{
		return size;
	}
	~myarray()
	{
		if (arr != NULL)
		{
			//cout << "4";
			delete[] arr; //释放动态堆区开辟的数组
			arr = NULL;
		}

	}
};

main.cpp

代码语言:javascript复制
#include<iostream>
#include"arr.hpp"
using namespace std;
//打印输出
void print(myarray<int>& a)
{
	for(int i=0;i<a.getSize();i  )
	cout << a[i]<<" ";   //[]运算符重载
	cout << endl;
}
int main()
{
   //尾插法测试
	myarray<int> a(10);
	for (int i = 0; i < 5; i  )
	{
		a.push_back(i);
	}
	print(a);
	cout << "数组容量:" << a.getCap() << endl;
	cout << "数组大小:" << a.getSize() << endl;
	//尾删法测试
	a.end_del();
	print(a);
	cout << "数组容量:" << a.getCap() << endl;
	cout << "数组大小:" << a.getSize() << endl;
	system("pause");
	return 0;
}

自定义数据类型测试:

代码语言:javascript复制
#include<iostream>
#include<string>
#include"arr.hpp"
using namespace std;
class person 
{
private:
	string name;
	int age;
public:
	//arr.hpp中new在堆区开辟person类型数据时,会自动调用person的无参构造函数
	//如果写了perosn的有参构造,那么有参构造会覆盖默认的无参构造,需要自己再写一遍无参构造,否则会报错
	person(){}
	person(string name, int age):name(name),age(age){}
	string getName()
	{
		return name;
	}
	int getAge()
	{
		return age;
	}
};
//打印输出(类模板对象做函数参数)
void printPerson(myarray<person>& p)
{
	for (int i = 0; i < p.getSize(); i  )
	{
		cout << "年龄为: " << p[i].getAge() << " " << "姓名: " << p[i].getName() << endl;
	}
}
int main()
{
	myarray<person> p(4);
	//自定义数据类型测试
	person p1("大忽悠", 19);
	person p2("小忽悠", 18);
	person p3("大朋友", 18);
	person p4("小朋友", 18);
	//尾插法
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);
	//打印
	printPerson(p);
	//数组容量
	cout << "数组容量:" << p.getCap() << endl;
	//数组大小
	cout << "数组大小:" << p.getSize() << endl;
	system("pause");
	return 0;
}

0 人点赞