MyVector.h
//模板类,不要分开多文件去写。要类的声明和类函数的实现体放在一个文件中去写。这里没有分开写
//如果要岔开写的的,需要将实现体的文件 #include进来(即实现函数的.cpp文件)。
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>
using namespace std;
//1 Myvector 能够存放 int,char,
//2 MyVector 能够存放 Teacher对象
//函数的声明和类的
template<class T>
class MyVector
{
friend ostream& operator<<<T>(ostream &out,MyVector &myvector);//友元函数声明
public:
MyVector();
MyVector(int len);
MyVector(const MyVector &another);//拷贝构造函数
~MyVector();
MyVector& operator=(const MyVector &another);
T& operator[](int index);
private:
int len;
T *space;
};
//函数的实现
template<typename T>
MyVector<T>::MyVector()//构造函数
{
space=NULL;
this->len=0;
}
template<typename T>
MyVector<T>::MyVector(int len)//构造函数
{
space=new T[len];
this->len=len;
}
template<typename T>
MyVector<T>::MyVector(const MyVector &another)//拷贝构造函数
{
len=another.len;
space=new T[len];
for(int i=0;i<len;i++)
{
space[i]=another.space[i];
}
}
template<typename T>
MyVector<T>::~MyVector()//析构函数
{
if(space!=NULL)
{
delete[] space;
space=NULL;
len=0;
}
}
template<typename T>
T& MyVector<T>::operator[](int index)
{
return space[index];
}
template<typename T>
MyVector<T>& MyVector<T>::operator=(const MyVector<T> &another)
{
//先把释放旧的内存
if(space!=NULL)
{
delete[] space;
space=NULL;
len=0;
}
//跟据情况分配内存
this->len=another.len;
this->space=new T[len];
//拷贝数据
for(int i=0;i<len;i++)
{
this->space[i]=another.space[i];
}
return *this;//返回本身
}
template<typename T>
ostream& operator<<(ostream &out,MyVector<T> &myvector)
{
for(int i=0;i<myvector.len;i++)
{
out<<myvector.space[i]<<" ";
}
return out;
}
test_MyVector.cpp测试数组模板
#include <iostream>
using namespace std;
#include <string>
#include "MyVector.h"
class Teacher
{
friend ostream& operator<<(ostream &out,Teacher &t)//友元函数声明
{
out<<"姓名:"<<t.name<<" 年龄:"<<t.age<<endl;
return out;
}
public:
Teacher()
{
//this->name="";
///////
this->name=NULL;
////////
this->age=0;
}
//Teacher(string name,int age)
Teacher(char *name,int age)
{
//this->name=name;
//////////////
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
////////////////
this->age=age;
}
Teacher(const Teacher &another)
{
//this->name=another.name;
/////////////////////
if(this->name!=NULL)
{
delete[] this->name;
this->age=0;
}
this->name=new char[strlen(another.name)+1];
strcpy(this->name,another.name);
////////////////////
this->age=another.age;
}
~Teacher()
{
//this->name="";
//////////
if(this->name!=NULL)
{
delete[] this->name;
this->name=NULL;
}
this->age=0;
////////////////
}
Teacher& operator=(const Teacher &another)
{
//this->name=another.name;
/////////////////////////
if(this->name!=NULL)
{
delete[] this->name;
this->age=0;
}
this->name=new char[strlen(another.name)+1];
strcpy(this->name,another.name);
//////////////////////
this->age=another.age;
return *this;
}
private:
//string name;//使用string类比较简单在C++中尽可能使用string表示字符串
char *name;//char *name就会涉及到深拷贝和浅拷贝问题
int age;
};
int main()
{
MyVector<int> vector_int(10);
for(int i=0;i<10;i++)
{
vector_int[i]=i+1;
}
cout<<vector_int<<endl;
MyVector<int> vector_intcopy;
vector_intcopy=vector_int;
cout<<vector_intcopy<<endl;
MyVector<char> vector_char(5);
for(int i=0;i<5;i++)
{
vector_char[i]=i+'a';
}
cout<<vector_char<<endl;
Teacher t1("jueshi",23);
Teacher t2("quanyecha",25);
Teacher t3("honglve",28);
MyVector<Teacher> vector_teacher(3);
vector_teacher[0]=t1;
vector_teacher[1]=t2;
vector_teacher[2]=t3;
cout<<vector_teacher<<endl;
}
浙公网安备 33010602011771号