泛型程式設計(模板函式,模板類的套用) Myvector 具體案例 實現可存放int 陣列 char陣列 類物件陣列 以及一組指標

梁朝偉啊發表於2018-03-11

主要通過泛型程式設計,利用Myvector該模板類,實現可存放int 陣列 char陣列  類物件陣列 以及一組指標

Myvector.h

#include<iostream>

using namespace std;
//MyVector 相當於一個容器
template <typename T>
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 << <T>(ostream &out, const  MyVector<T> & obj);
	int getlen()   //獲取長度
	{
		return m_len;
	}

protected:
	T *m_space;
	int m_len;

};

Myvector.cpp

#include<iostream>
#include"Myvector.h"
using namespace std;


template <typename T>


MyVector<T>::MyVector<T>(int size = 0)  //建構函式
{
	m_space = new T[size];  //獲取地址大小
	m_len = size;


}


template <typename T>
MyVector<T>::MyVector<T>(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 <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];
}




//a3 = a2 = a1  過載等號運算子


template <typename T>


MyVector<T> & MyVector<T>::operator=(const MyVector<T> &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 <typename T>


ostream& operator << (ostream &out, const  MyVector<T> & 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<iostream>

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<Teacher> 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<Teacher *> 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<int> myv1(10);

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


	//在MyVector陣列中存放char型別的陣列
	MyVector<char> myv3(10);

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

	cout << myv3 << endl;

	system("pause");
}

相關文章