C++:类模板应用-数组类封装

C++:类模板应用-数组类封装

1、数组封装代码

/*
开发环境:vs2013
代码文件名:类模板应用-数组类封装.hpp
*/

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

template<class ELEMENTTYPE>
class userArray
{
private:
	ELEMENTTYPE* parr;
	int capacity;
	int size;
public:
	// 有参构造
	explicit userArray(const int& capacity);
	// 拷贝构造
	userArray(const userArray<ELEMENTTYPE>& arr);
	// 赋值运算符重载
	userArray<ELEMENTTYPE>& operator=(const userArray<ELEMENTTYPE>& arr) const;
	// 析构函数
	~userArray();
	// []运算符重载
	ELEMENTTYPE& operator[](const int& index) const;
	// 获取数组大小
	int getSize() const;
	// 向数组追加元素
	void append(const ELEMENTTYPE& elem);
};

// 有参构造
template<class ELEMENTTYPE>
userArray<ELEMENTTYPE>::userArray(const int& capacity)
{
	this->capacity = capacity;
	this->parr = new ELEMENTTYPE[this->capacity];
	this->size = 0;
}

// 拷贝构造
template<class ELEMENTTYPE>
userArray<ELEMENTTYPE>::userArray(const userArray<ELEMENTTYPE>& arr)
{
	this->capacity = arr.capacity;
	this->parr = new ELEMENTTYPE[this->capacity];
	this->size = arr.size;
	memcpy(this->parr, arr.parr, sizeof(ELEMENTTYPE)*this->capacity);
}

// 赋值运算符重载
template<class ELEMENTTYPE>
userArray<ELEMENTTYPE>& userArray<ELEMENTTYPE>::operator=(const userArray<ELEMENTTYPE>& arr)const
{
	if (NULL != this->parr)
	{
		delete[] this->parr;
		this->parr = NULL;
	}
	this->capacity = arr.parr;
	this->parr = new ELEMENTTYPE[this->capacity];
	this->size = arr.size;
	memcpy(this->parr, arr.parr, sizeof(ELEMENTTYPE)*this->capacity);
	return *this;
}

// 析构函数
template<class ELEMENTTYPE>
userArray<ELEMENTTYPE>::~userArray()
{
	if (NULL != this->parr)
	{
		delete[] this->parr;
		this->parr = NULL;
	}
	this->capacity = 0;
	this->size = 0;
}

// []运算符重载
template<class ELEMENTTYPE>
ELEMENTTYPE& userArray<ELEMENTTYPE>::operator[](const int& index) const
{
	return this->parr[index];
}

// 获取数组大小
template<class ELEMENTTYPE>
int userArray<ELEMENTTYPE>::getSize() const
{
	return this->size;
}

// 向数组追加元素
template<class ELEMENTTYPE>
void userArray<ELEMENTTYPE>::append(const ELEMENTTYPE& elem)
{
	if (this->size == this->capacity)
	{
		return;
	}
	this->parr[this->size] = elem;
	++this->size;
}

2、测试代码

/*
开发环境:vs2013
代码文件名:test_array.cpp
*/

#define _CRT_SECURE_NO_WARNINGS
#include "类模板应用-数组类封装.hpp"

template<class ELEMENTTYPE>
void userPrint(const userArray<ELEMENTTYPE>& arr)
{
	for (int i = 0; i<arr.getSize(); i++)
	{
		cout << arr[i] << endl;
	}
}

void test01()
{
	userArray<int> arr(20);
	for (int i = 0; i<10; i++)
	{
		arr.append(i);
	}
	userArray<int> arr1 = arr;
	userPrint(arr1);
}

class Person
{
	friend ostream& operator<<(ostream& cout, const Person& p);
public:
    // 堆区开辟空间,一定调用对象的默认构造函数,因此须保证其存在
	Person(){}
	explicit Person(string name, int age) : m_name(name), m_age(age){}
private:
	string m_name;
	int m_age;
};

ostream& operator<<(ostream& cout, const Person& p)
{
	cout << "姓名:" << p.m_name << " 年龄:" << p.m_age;
	return cout;
}

void test02()
{
	userArray<Person> arr(20);

	Person p1("Lisi", 20);
	Person p2("wangwu", 21);
	Person p3("chenliu", 22);
	Person p4("qiqi", 23);
	Person p5("shiba", 24);

	arr.append(p1);
	arr.append(p2);
	arr.append(p3);
	arr.append(p4);
	arr.append(p5);
	userPrint(arr);
}

int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值