类模板在项目开发中的应用

训练题:
  1. 请设计一个数组模板类(MyVector),完成对int,char,Teacher类型元素的管理.
    需求: 类模板, 构造函数, 拷贝构造函数 重载 << >> =
MyVector.h
#pragma once

#include 
using namespace std;


template 
class MyVector
{
	// 需要注意  一定要加上   但是在.cpp文件里面加上了   还会报错 MyVector 可以不加  编译也能通过
	friend ostream & operator<< (ostream &out, const MyVector &obj);
public:
	MyVector(int size = 0);	// 构造函数
	MyVector(const MyVector &obj);	// 拷贝构造函数
	~MyVector();	// 析构函数

public:
	T & operator[] (int index);
	MyVector & operator= (const MyVector &obj);
	
public:
	int getLen()
	{
		return m_len;
	}


protected:
	T *m_space;
	int m_len;


};

MyVector.cpp
#include 
using namespace std;

#include "MyVector.h"


template 
MyVector::MyVector(int size)  // 不允许使用默认参数
{
	// m_space = new int[size];
	m_space = new T[size];
	m_len = size;
}
// 拷贝构造函数
template 
MyVector::MyVector(const MyVector &obj)
{
	// 根据obj的大小分配内存 
	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];
}

template 
MyVector & MyVector::operator= (const MyVector &obj)
{
	// 先把自己的内存给释放掉
	if (m_space != NULL)
	{
		delete[] m_space;
		m_space = NULL;
		m_len = 0;
	}
	// 根据 obj 分配内存
	m_len = obj.m_len;
	m_space = new T[m_len];
	// copy 数据
	for (int i = 0; i < m_len; i++)
	{
		m_space[i] = obj[i];
	}
	return *this;
}

// 重载 << 操作符
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
#define _CRT_SECURE_NO_WARNINGS
#include 
using namespace std;

#include "MyVector.cpp"


class Teacher 
{
public:
	Teacher()
	{
		this->age = 33;
		strcpy(name, " ");
	}
	Teacher(char *name, int age)
	{
		this->age = age;
		strcpy(this->name, name);
	}
	void printT()
	{
		cout << "name : " << name << " - age : " << age << endl;
	}
private:
	int age;
	char name[32];

};

int main()
{
	Teacher t1("t1", 31);
	Teacher t2("t2", 32);
	Teacher t3("t3", 33);
	Teacher t4("t4", 34);

	MyVector tArray(4);
	tArray[0] = t1;
	tArray[1] = t2;
	tArray[2] = t3;
	tArray[3] = t4;

	// cout << tArray;
	// cout << tArray << endl;

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


	system("pause");
	return 0;
}




int main02()
{
	MyVector myv1(10);
	myv1[0] = 'a';
	myv1[1] = 'b';
	myv1[2] = 'c';
	myv1[3] = 'd';
	cout << myv1 << endl;

	system("pause");
	return 0;
}


int main01()
{
	MyVector myv1(10);
	for (int i = 0; i < myv1.getLen(); i++)
	{
		myv1[i] = i + 1;
		// cout << myv1[i] << " ";
	}
	// cout << endl;
	cout << "-----myv1-----" << endl;
	cout << myv1;

	MyVector myv2 = myv1;
	/*for (int i = 0; i < myv1.getLen(); i++)
	{
		cout << myv2[i] << " ";
	}*/
	cout << "-----myv2-----" << endl;
	cout << myv2 << endl;

	


	cout << "Hello World" << endl;
	system("pause");
	return 1;
}

注意:

如果把Teacher放入MyVector数组中,并且Teacher类里面含有指针,就会出现深拷贝和浅拷贝的问题.

理论提高:

所有的容器提供的都是值(value)语意,而非引用(reference)语意.容器执行插入元素操作时,内部实施拷贝工作.所以STL容器内存储的元素必须能够被拷贝(必须提供拷贝构造函数)

类模板在项目开发中的应用_第1张图片

由于是 = 操作 c++编译器选择的是默认的拷贝构造函数.

在MyVector重载 = 操作符中: 进行的也是浅拷贝
类模板在项目开发中的应用_第2张图片

结论2:需要Teacher封装的函数有:
1) 重写拷贝构造函数
2) 重载等号操作符
3) 重载左移操作符。

你可能感兴趣的:(正则)