第十天之类模板作业

封装你自己的数组类;设计被存储的元素为类对象;
思考:类对象的类,应该实现的功能。
//1 优化 Teacher 类, 属性变成 char panme, 构造函数里面 分配内存
//2 优化 Teacher 类,析构函数 释放 panme 指向的内存空间
//3 优化 Teacher 类,避免浅拷贝 重载= 重写拷贝构造函数
//4 优化 Teacher 类,在 Teacher 增加 <<
//5 在模板数组类中,存 int char Teacher Teacher
(指针类型)
//=====>stl 容器的概念

MyVector.h

#pragma once

#include <iostream>
using namespace std;

template <typename T>
class MyVector
{
public:
	friend ostream& operator<< <T>(ostream& out, const MyVector& obj);

	MyVector(int size = 0);			//构造函数
	MyVector(const MyVector &obj);	//拷贝构造函数
	~MyVector();					//析构函数
	int GetLen()
	{
		return m_len;
	}

public:
	T& operator[](int index);
	MyVector& operator=(const MyVector& obj);
	
private:
	T	*m_space;
	int m_len;

};


MyVector.cpp

#include "MyVector.h"
#include <iostream>

using namespace std;

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;
}

template <typename T>
MyVector<T>::MyVector(int size = 0)			//构造函数
{
	m_space = new T[size];
	m_len = size;
}
//MyVector<int> myvl2 = myvl;
template <typename T>
MyVector<T>::MyVector(const MyVector &obj)	//拷贝构造函数
{
	m_len = obj.m_len;
	m_space = new T[m_len];

	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_len = 0;
		m_space = NULL;
	}
}
template <typename T>
T& MyVector<T>::operator[](int index)
{
	return m_space[index];
}

//a1 = a2
template <typename T>
MyVector<T>& MyVector<T>::operator=(const MyVector& obj)
{
	//先把旧内存a1释放掉
	if (m_space != NULL)
	{
		delete[] m_space;
		m_space = NULL;
		m_len = 0;
	}
	m_len = obj.m_len;
	//再根据a2分配新内存
	m_space = new T[m_len];

	return *this;
}

MyVector_test.cpp

#define  _CRT_SECURE_NO_WARNINGS
#include "MyVector.cpp"
#include <iostream>
using namespace std;

//1、优化Teacher类, 属性变成char *pname; 构造函数里分配内存
//2、析构函数中释放内存
//3、避免浅拷贝		重载=运算符 重写拷贝构造函数
//4、在Teacher中增加<<运算符重载
//5、在模板数组类中存 int char Teacher Teacher*等类型


class Teacher
{
public:
	Teacher()
	{
		age = 33;
		m_p = new char[1];
		strcpy(m_p, "");
	}
	Teacher(char *name, int age)
	{
		this->age = age;
		this->m_p = new char[strlen(name)];
		strcpy(this->m_p, name);
	}
	Teacher(const Teacher& obj)
	{	
		m_p = new char[strlen(obj.m_p) + 1];
		age = obj.age;
		strcpy(this->m_p, obj.m_p);
	}

	~Teacher()
	{
		if (m_p = NULL)
		{
			delete[] m_p;
			m_p = NULL;
			age = 33;
		}
	}
	void ptintT()
	{
		cout << m_p << ", " << age << endl;
	}

public:	//重载操作符
	friend ostream& operator<<(ostream &out, Teacher &obj);
	Teacher& operator=(const Teacher &obj)
	{
		//1、先释放旧内存
		if (m_p != NULL)
		{
			delete[] m_p;
			m_p = NULL;
			age = 33;
		}
		//2、根据参数obj分配内存
		m_p = new char[strlen(obj.m_p) + 1];

		//3、赋值拷贝
		age = obj.age;
		strcpy(m_p, obj.m_p);

		return *this;
	}
private:
	int age;
	//char name[32];
	char *m_p;	//问题抛出:如果是一个指针则程序崩溃 深拷贝和浅拷贝(优化)
};
ostream& operator<<(ostream &out, Teacher &obj)
{
	out << obj.m_p << ", " << obj.age << endl;
	return out;
}

void main()
{
	//MyVector中存Teacher *指针类型
	Teacher t1("t1", 30), t2("t2", 31), t3("t3", 32), t4("t4", 33);
	MyVector<Teacher *> tArray(4);
	tArray[0] = &t1;
	tArray[1] = &t2;
	tArray[2] = &t3;
	tArray[3] = &t4;
	for (int i = 0; i < 4; i++)
	{
		Teacher *temp = tArray[i];
		temp->ptintT();
	}


	system("pause");
}

void main003_存Teacher类型()
{
	Teacher t1("t1", 30), t2("t2", 31), t3("t3", 32), t4("t4", 33);
	MyVector<Teacher> tArray(4);
	tArray[0] = t1;
	tArray[1] = t2;
	tArray[2] = t3;
	tArray[3] = t4;
	for (int i = 0; i < 4; i++)
	{
		Teacher temp = tArray[i];
		temp.ptintT();
	}


	system("pause");
}
void main002_存char类型()
{
	MyVector<char> myvl(10);
	myvl[0] = 'a';
	myvl[1] = 'b';
	myvl[2] = 'c';
	myvl[3] = 'd';
	myvl[4] = '\0';
	cout << myvl;

	system("pause");
}


void main001_存int类型()
{
	MyVector<int> myvl(10);
	for (int i = 0; i < myvl.GetLen(); i++)
	{
		myvl[i] = i + 1;
		cout << myvl[i] << " ";
	}
	cout << endl;

	MyVector<int> myvl2 = myvl;
	for (int i = 0; i < myvl2.GetLen(); i++)
	{
		cout << myvl2[i] << " ";
	}
	cout << endl;
	cout << myvl2 << endl;
	system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值