泛型编程(模板函数,模板类的套用) Myvector 具体案例 实现可存放int 数组 char数组 类对象数组 以及一组指针

主要通过泛型编程,利用Myvector该模板类,实现可存放int 数组 char数组  类对象数组 以及一组指针

Myvector.h

#include

using namespace std;
//MyVector 相当于一个容器
template 
class MyVector
{
public:
	MyVector(int size = 0);  //构造函数
	MyVector(const MyVector &obj);   //拷贝构造函数
	~MyVector();  //析构函数

public:

	//重载[]运算符
	T &operator[](int index);

	//a3 = a2 = a1  重载等号运算符
	MyVector &operator=(const MyVector &obj);

	friend ostream& operator << (ostream &out, const  MyVector & obj);
	int getlen()   //获取长度
	{
		return m_len;
	}

protected:
	T *m_space;
	int m_len;

};

Myvector.cpp

 
  
#include
#include"Myvector.h"
using namespace std;


template 


MyVector::MyVector(int size = 0)  //构造函数
{
	m_space = new T[size];  //获取地址大小
	m_len = size;


}


template 
MyVector::MyVector(const MyVector &obj)  //拷贝构造函数
{
	//根据MV1的大小分配内存
	m_len = obj.m_len;
	m_space = new T[m_len];


	//copy数据


	for (int i = 0; i < m_len; i++)
	{
		m_space[i] = obj.m_space[i];
	}
	
}




template 
MyVector::~MyVector() //析构函数
{
	if (m_space != NULL)
	{
		delete[] m_space;
		m_space = NULL;  //避免野指针
		m_len = 0;
	
	}
}


template 


T& MyVector::operator[](int index)	   //重载[]运算符
{
	return m_space[index];
}




//a3 = a2 = a1  重载等号运算符


template 


MyVector & MyVector::operator=(const MyVector &obj)
{
	//先释放掉a2的旧内存
		 
	if (m_space != NULL)
	{
		delete[] m_space;
		m_space = NULL;  //避免野指针
		m_len = 0;


	}


	//根据a1分配内存
	m_len = obj.m_len;
	m_space new T[m_len];




	//copy数据


	for (int i = 0; i < m_len; i++)
	{
		m_space[i] = obj.m_space[i];
	}	


	return *this;  //a2 = a1 返回给a2的自身
} 




//友元函数重载<< 的函数写在类外
template 


ostream& operator << (ostream &out, const  MyVector & obj)
{
	for (int i = 0; i < obj.m_len; i++)
	{
		out << obj.m_space[i] << " ";
	}


	out << endl;
	return out;
}



Myvector_test.cpp

#include"Myvector.cpp"
#include

using namespace std;


class Teacher   //用于测试是否正确存放一组对象的 Teacher 类
{
public:

	Teacher()
	{
		age = 33;
		pName2 = new char[1];
		strcpy(pName2, "");
	}

	Teacher(char *name, int age)     //一般构造函数
	{
		this->age = age;
		pName2 = new char[strlen(name) + 1];  //获取开辟空间大小

		if (pName2 != NULL)
		{
			//如果m_pName不是空指针,则把形参指针pN所指的字符串复制给它
			strcpy(pName2, name);
		}


	}


	Teacher(const Teacher & obj)     //拷贝构造函数,避免浅拷贝
	{

		age = obj.age; 
		// 用运算符new为新对象的指针数据成员分配空间
		pName2 = new char[strlen(obj.pName2) + 1];
		if (pName2!=NULL)
		     {
	      // 复制内容
			strcpy(pName2, obj.pName2);
			 }

	}

	~Teacher()  //析构函数,避免二次delete
	{
		
		if (NULL!=pName2)
		{
			delete [] pName2;  //释放指针所指向的内容   
			pName2 = NULL;      //将指针置为空  
			age = 0;
			cout << "析构函数" << endl;
		} 
	}

	void printT()
	{
		cout << pName2 << "," << age << endl;
	}


	/*友元函数重载  */
	friend ostream &operator<<(ostream &out, const Teacher &obj)
	{
		out << obj.pName2 << "," << obj.pName2 << endl;
		return out;
	}


	/* 成员函数 =  实现 a1 = a2 = a3  实现C++当中的深拷贝 返回Teacher的一个引用*/
	Teacher & operator = (const Teacher &obj)
	{
		//先释放掉a2的旧内存
		if (pName2 != NULL)
		{
			delete[] pName2;
			pName2 = NULL;
			age = 33;
		}

		//根据a1分配内存,并且copy数据

		pName2 = new char[strlen(obj.pName2) + 1];
		if (pName2 != NULL)
		{
			//pName2 = obj.pName2;
			strcpy(pName2, obj.pName2);
			age = obj.age;
		}

		//返回this
		return *this;
	}

private:
	int age;
	//char name[32];  
	char *pName2;
};


void main()
{

	//在MyVector数组中存放一组类对象
	Teacher t1("t1", 31), t2("t2", 32), t3("t3", 33), t4("t4", 34);

	MyVector tArray(4);

	tArray[0] = t1;
	tArray[1] = t2;
	tArray[2] = t3;
	tArray[3] = t4;

	for (int i = 0; i<4; i++)
	{
		Teacher tmp = tArray[i];
	}
	cout << tArray;


	//在MyVector数组中存放一组指针

	Teacher t5("t5", 31), t6("t6", 32), t7("t7", 33), t8("t8", 34);

	MyVector tArray_1(4);

	tArray_1[0] = &t5;
	tArray_1[1] = &t6;
	tArray_1[2] = &t7;
	tArray_1[3] = &t8;

	for (int i = 0; i<4; i++)
	{
		Teacher *tmp = tArray_1[i];
		tmp->printT();
		//tmp.printT();
	}
	//cout << tArray;

	system("pause");
}

void main_01()
{

	//在MyVector数组中存放int类型的数组
	MyVector myv1(10);

	for (int i = 0; i < myv1.getlen(); i++)
	{
		myv1[i] = i + 1;
		cout << myv1[i] << "" ;
	}
	cout << endl;
	cout << myv1 << endl;
	MyVector myv2(10);


	//在MyVector数组中存放char类型的数组
	MyVector myv3(10);

	for (int i = 97; i < myv3.getlen(); i++)
	{
		myv3[i] = 97+i;
		cout << myv3[i] << "";
	}

	cout << myv3 << endl;

	system("pause");
}

你可能感兴趣的:(C++)