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;
}