设计一个数组模板类,完成对int、char、Teacher、Teacher*数据类型操作

模板类中应该包括:构造函数、析构函数、拷贝构造函数、重载[]、=、<<操作符

//MyArray.h
#pragma once
#pragma warning(disable : 4996)
#include 
using namespace std;

template 
class MyArray
{
	friend ostream & operator<<(ostream &out ,const MyArray&obj);
public:
	MyArray(int size);
	MyArray(const MyArray &obj);
	~MyArray();
	int GetLen();
public:
	T& operator[](int index);
	MyArray& operator=(const MyArray &obj);
private:
	T *array;
	int len;
};
//MyArray.cpp
#pragma warning(disable : 4996)
#include 
#include "MyArray.h"
using namespace std;

template 
MyArray::MyArray(int size)
{
	this->len = size;
	array = new T[len];
}

template
MyArray::MyArray(const MyArray & obj)
{
	this->len = obj.len;
	this->array = new T[len];

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

template
MyArray::~MyArray()
{
	if (this->array != NULL)
	{
		delete[] array;
		array = NULL;
		len = 0;
	}
}

template
int MyArray::GetLen()
{
	return this->len;
}

template
T & MyArray::operator[](int index)
{
	return array[index];
}

template
MyArray & MyArray::operator=(const MyArray & obj)
{
	if (this->array != NULL)
	{
		delete[] array;
		array = NULL;
		len = 0;
	}

	this->len = obj.len;
	this->array = new T[len];

	for (int i = 0;i
ostream & operator<<(ostream & out, const MyArray& obj)
{
	for (int i = 0; i < obj.len; i++)
	{
		out << obj.array[i] <<" ";
	}
	out << endl;
	return out;
}

在数组模板中要存储的元素为类对象或者类指针对象时:

//1  优化Teacher类, 属性变成 char *panme, 构造函数里面 分配内存

//2  优化Teacher类,析构函数 释放panme指向的内存空间

//3  优化Teacher类,避免浅拷贝 重载= 重写拷贝构造函数

//4  优化Teacher类,在Teacher增加 <<

//5  在模板数组类中,存int char Teacher Teacher*(指针类型)

//test.cpp
#pragma warning(disable : 4996)
#include 
#include "MyArray.cpp"
using namespace std;

class Teacher
{
	friend ostream & operator<<(ostream &out, const Teacher &obj)
	{
		out << "name: " << obj.m_name << "age: " << obj.m_age << endl;
		return out;
	}
public:
	Teacher(int age, const char *name)
	{
		this->m_age = age;
		int len = strlen(name);
		m_name = new char[len + 1];
		strcpy(this->m_name, name);
	}

	Teacher()
	{
		this->m_age = 0;
		m_name = new char[32];
		strcpy(this->m_name, "");
	}

	Teacher(const Teacher &obj)
	{
		this->m_age = obj.m_age;
		int len = strlen(obj.m_name);

		this->m_name = new char[len + 1];

		strcpy(this->m_name, obj.m_name);
	}

	Teacher& operator=(const Teacher &obj)
	{
		if (this->m_name != NULL)
		{
			delete [] m_name;
			m_name = NULL;
			m_age = 0;
		}

		m_age = obj.m_age;
		m_name = new char[strlen(obj.m_name) + 1];

		strcpy(this->m_name, obj.m_name);
		return *this;
	}


	~Teacher()
	{
		if (this->m_name != NULL)
		{
			delete [] m_name;
			m_name = NULL;
			m_age = 0;
		}
	}

	void PrintT()
	{
		cout << "name: " << m_name << "age: " << m_age << endl;
	}
private:
	int m_age;
	//char m_name[10];
	char *m_name;
};

int main()
{
	Teacher t1(18, "zhang1"), t2(19, "zhang2"), t3(20, "zhang3"), t4(21, "zhang4");

	MyArray array(4);

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

	for (int i = 0; i < array.GetLen(); i++)
	{
		Teacher *temp = array[i];
		temp->PrintT();
	}

	system("pause");
	return 0;
}

int main2()
{
	Teacher t1(18, "zhang1"), t2(19, "zhang2"), t3(20, "zhang3"), t4(21, "zhang4");

	MyArray array(4);

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

	for (int i = 0; i < array.GetLen(); i++)
	{
		Teacher temp = array[i];
		//temp.PrintT();

		cout << array[i] << endl;
	}

	system("pause");
	return 0;
}

//对int、char、类型数据操作
int main1()
{
	MyArray array(10);

	for (int i = 0; i < array.GetLen(); i++)
	{
		array[i] = i + 1;
		cout << array[i] << "  ";
	}

	cout << endl;

	MyArray strArray(5);
	for (int i = 0; i < strArray.GetLen(); i++)
	{
		strArray[i] = i + 97;
		cout << strArray[i] << "  ";
	}
	cout << endl;

	system("pause");
	return 0;
}
 

你可能感兴趣的:(C\C++基础,容器概念,类模板)