c++自定义的容器实现

通常我们使用的int、char等数据类型可以创建数组去容纳数据,其实我们自定义的类也能够像基础类型一样去创建容器容纳各种数据。包括基础数据类型、自定义的类对象、以及对象指针等等数据

  1. 创建类对象的头文件
#pragma once
#include "iostream"
using namespace std;
template <typename T>//泛型编程,可以使类存储任意形式的数据
class myVector
{
	friend ostream& operator<< <T>(ostream& cout, myVector& obj);
public:
	myVector(int length);//构造函数
	myVector(const myVector& obj);// 拷贝构造函数
	~myVector();//析构函数
public:
	T& operator[](int index);//重载[]
	myVector& operator=(myVector &obj);//重载=
	int getLen();//提供一个外部获取数组长度入口
private: 
	int m_len;//数组长度
	T *m_space;//数组指向的内存地址
};
  1. 函数体的实现(在类模板的实现时,一般将实现文件命名为.hpp文件)
#include "iostream"
using namespace std;
#include "容器.h"
template <typename T>
myVector<T>::myVector(int length)//构造函数
{
	m_space = new T[length];//在数组类的构造函数中,应该对新的对象分配内存,否则会出现没有内存地址接收数据的错误
	//这才是数组类的关键所在,类名只是一个类型,跟int,char是一样的,但是用于开辟数组内存的时候就是数组类了
	this->m_len = length;
}
template <typename T>
myVector<T>::myVector(const myVector<T>& obj)// 拷贝构造函数
{
	m_len = obj.m_len;
	m_space = new T[m_len];
	for (int i = 0; i < obj.m_len; i++)
	{
		m_space[i] = obj.m_space[i]+1;
	}
}
template <typename T>
myVector<T>::~myVector()//析构函数
{
	if (m_space != NULL)
	{
		delete[] m_space;
		m_space = NULL;
		m_len = 0;
	}
}
template <typename T>
T& myVector<T>::operator[](int index)
{
	return m_space[index];
}
template <typename T>
myVector<T>& myVector<T>::operator=(myVector<T> &obj)
{
	//1.释放原有内存
	if (m_space != NULL)
	{
		delete[] m_space;
		m_space = NULL;
		m_len = 0;
	}
	//2.分配新的内存
	m_len = obj.m_len;
	m_space = new int[m_len];
	//3.复制
	for (int i = 0; i < obj.m_len; i++)
	{
		m_space[i] = obj.m_space[i];
	}
	return *this;
}
template <typename T>
int myVector<T>::getLen()
{
	return m_len;
}
template <typename T>

ostream& operator<<(ostream& cout, myVector<T>& obj)
{
	for (int i = 0; i < obj.m_len; i++)
	{
		cout << obj.m_space[i] << " ";
	}
	cout << endl;
	return cout;
}
  1. 主函数体(为了测试能否存储对象,另外创建了teacher类)
    3.1 teacher.h
#pragma once
class teacher
{
	friend ostream& operator<<(ostream& cout, teacher& obj);
public:
	teacher();
	teacher(int age, char *name);
	teacher(const teacher& obj);//拷贝函数
	~teacher();//析构函数
	teacher& operator=(teacher  &obj);
	void print()
	{
		cout << "年纪: " << t_age << "    名字:" << t_name << endl;
	}
private:
	int t_age;
	char *t_name;
};

3.2 teacher.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
using namespace std;
#include "teacher类.h"
teacher::teacher()
{

}
teacher::teacher(int age, char *name)
{
	this->t_age = age;
	this->t_name = new char[strlen(name)+1];
	strcpy(t_name, name);
}
teacher::teacher(const teacher& obj)//拷贝函数
{
	if (t_name != NULL)
	{
		free(t_name);
		t_name = NULL;
		t_age = 0;
	}
	this->t_age = obj.t_age;
	this->t_name = new char[strlen(obj.t_name) + 1];
	strcpy(t_name, obj.t_name);
}
teacher::~teacher()//析构函数
{
	cout << "我是析构函数" << endl;
}
ostream& operator<<(ostream& cout, teacher& obj)
{
	cout << "年纪: " << obj.t_age << "    名字:" << obj.t_name << endl;
	return cout;
}
teacher& teacher::operator=(teacher  &obj)
{
	//1.释放原有内存
	if (t_name != NULL)
	{
		free(t_name);
		t_name = NULL;
		t_age = 0;
	}
	//分配新的内存
	this->t_age = obj.t_age;
	this->t_name = new char[strlen(obj.t_name) + 1];
	//3.拷贝
	strcpy(t_name, obj.t_name);
	return *this;
}

3.3 容器的主函数体

#include "iostream"
using namespace std;
#include "容器.CPP"//当使用模板类时,需要包含的不再是.h文件,而是.cpp文件
#include "teacher类.h"
void main13()//用数组类myVector去创建int类型的数组对象
{
	myVector<int> my_vector1(10);//初始化数组容器对象,赋予10个数组长度
	for (int i = 0; i<my_vector1.getLen(); i++)
	{
		my_vector1[i] = i + 1;
		cout << my_vector1[i] << " ";
	}
	cout << endl;
	myVector<int> my_vector2 = my_vector1;
	cout << my_vector2 << endl;  
}
void main14()//用数组类myVector去创建char类型的数组对象
{
	myVector<char> my_vector1(10);//初始化数组容器对象,赋予10个数组长度
	for (int i = 0; i<my_vector1.getLen(); i++)
	{
		my_vector1[i] = i + 97;
		cout << my_vector1[i] << " ";
	}
	cout << endl;
	myVector<char> my_vector2 = my_vector1;
	cout << my_vector2 << endl;
}
void main15()//用数组类myVector去创建teacher类型的数组对象
{
	teacher Teacher1(25, "大娃25"), Teacher2(26, "二娃26"), Teacher3(27, "三娃27");
	myVector<teacher> Teacher(3);
	Teacher[0] = Teacher1;
	Teacher[1] = Teacher2;
	Teacher[2] = Teacher3;
	for (int i = 0; i < Teacher.getLen(); i++)
	{
		cout << Teacher[i] << endl;//先调用myVector类的重载[],然后返回一个引用,被赋予teacher类的属性
	}
	teacher t1 = Teacher1;
	cout << t1 << endl;
}
void main16()//用数组类myVector去创建 指针类型的数组对象。这个数组可以装指针
{
	teacher Teacher1(25, "大娃25"), Teacher2(26, "二娃26"), Teacher3(27, "三娃27");
	myVector<teacher *> Teacher(3);
	Teacher[0] = &Teacher1;
	Teacher[1] = &Teacher2;
	Teacher[2] = &Teacher3;
	for (int i = 0; i < Teacher.getLen(); i++)
	{
		teacher *tmp = Teacher[i];
		tmp->print();//先调用myVector类的重载[],然后返回一个引用,被赋予teacher类的属性
	}
	teacher t1 = Teacher1;
	cout << t1 << endl;
}

你可能感兴趣的:(c++提高第9天)