模板数组类案例

      如果把Teacher放入到MyVector数组中, 并且Teacher类的属性含有指针, 就出现深拷贝和浅拷贝的问题。
需要
Teacher封装的函数有:
      1) 重写拷贝构造函数
      2) 重载等号操作符
      3) 重载左移操作符。

//.h文件

#pragma once
#include<iostream>
using namespace std;
template<typename T>
class MyVector
{
public:
	MyVector(int size);
	//MyVector b = a;//拷贝构造函数
	MyVector(const MyVector &obj);
	MyVector();
	~MyVector();
	int getlen()
	{
		return length;
	}
	
public:
	//a[i] = i;//[]重载
	T &operator[](int i);
	//c = b;//=操作符重载
	MyVector&  operator=(MyVector&t);
	//cout << c;//<<重载
	friend ostream& operator<<<T>(ostream&out, MyVector&t);
private:
	T *str;
	int length;
};
#pragma once
#include"iostream"
using namespace std;
class Teacher
{
public:
	Teacher();
	Teacher(char *p ,int age);
	Teacher(const Teacher &t);

	~Teacher();
public:
	Teacher & operator=(Teacher &t);
	friend ostream& operator<<(ostream&out, Teacher&t);
private:
	char *name;
	int age;
};

//.cpp文件

#include "MyVector.h"
#include<iostream>
using namespace std;
template<typename T>
MyVector<T>::MyVector(int size)
{
	length = size;
	str = new T[size];
}
template<typename T>
MyVector<T>::MyVector()
{
}
template<typename T>
MyVector<T>::MyVector(const MyVector<T> &obj)
{
	this->length = obj.length;
	this->str = obj.str;
}
template<typename T>
MyVector<T>::~MyVector()
{
	if (str != NULL)
	{
		delete str;
		str = NULL;
		length = 0;
	}
}
template<typename T>
T &MyVector<T>::operator[](int i)
{
	return str[i];
}
template<typename T>
MyVector<T>&  MyVector<T>::operator=(MyVector<T>&t)
{   //先释放旧内存
	if (str != NULL)
	{
		delete str;
		str = NULL;
		length = 0;
	}
	//开辟空间
	length = t.length;
	str = new T[length+1];
	//copy数据
	for (int i = 0; i < t.length; i++)
	{
		str[i] = t.str[i];
	}
	return *this;
}
template<typename T>
ostream& operator<<(ostream&out, MyVector<T>&t)
{
	for (int i = 0; i < t.getlen(); i++)
	{
		out << t.str[i] << " ";
	}
	cout<< endl;
	return out;
}
#define  _CRT_SECURE_NO_WARNINGS

#include "Teacher.h"
#include"iostream"
using namespace std;
Teacher::Teacher()
{
	age = 0;
	name = new char[1];
	strcpy(name, "");
}
Teacher::Teacher(char *p, int age)
{	
	    this->age = age;
		name = new char[strlen(p)+1];
		strcpy(name, p);
}
Teacher::Teacher(const Teacher &t)
{
	name = new char[strlen(t.name)+1];
	strcpy(name, t.name);
	age = t.age;
}
Teacher::~Teacher()
{
	if (name != NULL)
	{
		delete [] name;
		name = NULL;
		age = 0;
	}
}

ostream& operator<<(ostream&out, Teacher &t)
{
    out << t.name << "," << t.age;
	return out;
}
Teacher& Teacher::operator=(Teacher &t)
{
	if (name != NULL)
	{
		delete[] name;
		name = NULL;
		age = 0;
	}
    
	name = new char[strlen(t.name)+1];
	strcpy(name, t.name);
	age = t.age;
	return *this;
}

//main.cpp
#include"MyVector.cpp"
#include "Teacher.h"
#include<iostream>
using namespace std;
int main()
{
	Teacher  a1("js", 30), a2("zx", 31), a3("rm", 33);
	MyVector<Teacher> a(3);
	a[0] = a1;
	a[1] = a2;
	a[2] = a3;
	MyVector<Teacher> b = a;
	MyVector<Teacher> c;
	c = b;
	cout << c;
	
	
	system("pause");
	return 0;

}
int main01()
{
	MyVector<int> a(10);
	for (int i = 0; i < a.getlen(); i++)
	{ 
		a[i] = i;//[]重载
	}
	MyVector<int> b = a;//拷贝构造函数
	MyVector<int> c;
	c = b;//=操作符重载
	cout << c;//<<重载
	cout << a;
	cout << b;
	system("pause");
	return 0;

}


猜你喜欢

转载自blog.csdn.net/ukston_c/article/details/80536938
今日推荐