类模板示例——数组类封装

实现一个通用的数组类,要求如下:

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

废话不多说,直接提供实现代码:

代码结构:

  • 测试案例:Person类实体
  • 数组类模板
  • 对<<运算符的重载
  • 为了输出自定义数据类型而写的输出函数
  • 测试案例1
  • 测试案例2
  • 主函数
#include<iostream>
using namespace std;

class Person
{
private:
	int m_age = 0;
	string m_name;
public:
	Person() {};
	Person(int age, string name)
	{
		this->m_age = age;
		this->m_name = name;
	}
	int getage() {
		return this->m_age;
	}
	string getname() {
		return this->m_name;
	}
};

template<class T>
class Array
{
private:
	int size;
	int len = 0;
	T* point = NULL;
public:
	//friend ostream& operator<<<T>(ostream& out, Array<T>& array);//这里一定要加<T>
	
	Array(int size) {//构造函数
		this->size = size;
		this->len = 0;
		point = new T[this->size];
	}
	
	Array(const Array& array){//拷贝构造函数
		this->size = array.size;
		this->len = array.len;
		this->point = new T[size];
		for (int i = 0; i < array.len; i++) {
			point[i] = array.point[i];
		}
	}
	Array<T>& operator=(const Array<T>& array) {//等号运算符重载
		/*if (point != NULL) {
			delete[]this->point;
			this->point = NULL;
			this->len = 0;
			this->size = 0;
		}*/
		delete[]point;
		this->len = array.len;
		this->size = array.size;
		point = new T[array.size];
		for (int i = 0; i < len; i++) {
			point[i] = array.point[i];
		}
		return *this;
	}

	void add(const T& elem){//尾插法
		if (this->len == this->size) {
			cout << "容量已满" << endl;
			return;
		}
		point[this->len++] = elem;
	}

	void remove(){//尾删法
		if (this->len > 0)this->len--;
		else cout << "容器为空" << endl;
	}

	T& operator[](int index){//通过下标索取元素
		return point[index];
	}

	int get_len(){//获取长度和容量
		return this->len;
	}
	int get_size(){
		return this->size;
	}

	~Array()
	{
		if (this->point != NULL) {
			delete[]this->point;
			point = NULL;
		}
	}
};

template<class T>
ostream& operator<<(ostream &out,Array<T> &array)
{
	for (int i = 0; i < array.get_len(); i++) {
		out << array[i] << " ";
	}
	out << endl;
	return out;
}

template<class T>
void printPerson(Array<T> &array)
{
	for (int i = 0; i < array.get_len(); i++) {
		cout << array[i].getage() << " " << array[i].getname() << endl;
	}
}

void test1()//内置数据类型
{
	class Array<int> a1(10);
	a1.add(5);
	a1.add(6);
	a1.add(7);
	a1.add(8);
	a1.add(9);
	a1.add(100);
	a1.add(500);
	//a1.add(8);
	//a1.add(9);
	//a1.add(10);
	//a1.add(11);
	class Array<int> b(a1);
	class Array<int> c(5);
	c = b;
	cout << a1;
	cout << a1.get_len() << " " << a1.get_size() << endl;
	
	c.remove();
	cout << "c[5]=" << c.operator[](5) << endl;
}
void test2()//自定义数据类型
{
	Array <Person>arr(12);
	
	Person p1(18,"zhangsan");
	Person p2(50, "lisi");
	Person p3(12, "wangwu");
	Person p4(35, "liuliu");
	Person p5(19, "zahoqi");

	arr.add(p1);
	arr.add(p2);
	arr.add(p3);
	arr.add(p4);
	arr.add(p5);

	printPerson(arr);

	Array<Person>test(arr);
	test = arr;
	//printPerson(test);
}

int main()
{
	test1();
	test2();
}

简要说一下每个要求是怎么处理的:

  1. 内置数据类型就是int、char之类的基本数据类型,自定义数据类型就是自己建立的实体类;对数据进行存储就在类中添加属性即可;
  2. 利用new把数据存放到堆区中;
  3. 模板类的构造函数中传入一个int类型的参数代表容量;
  4. 拷贝构造函数和“=”运算符的重载都是为了防止浅拷贝的问题,自己写即可(详解请参考个人博客——c++深拷贝与浅拷贝,这是链接https://blog.csdn.net/qq_63211065/article/details/124253695?spm=1001.2014.3001.5502
  5. 尾插法尾删法都是对类属性长度(len)的加减操作,注意判断长度与容量的大小;
  6. 重载[  ]运算符即可;
  7. 类内函数 get_len()和 get_size()。

附一句文案:浪漫和悲观并不冲突,我时常消极又觉得生活很美好。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值