二、类模板

2.2.7类模板成员函数分文件编写

 类模板成员函数创建是在调试阶段,导致分文件编写时链接不到。

解决方案:

  1. 直接包含.cpp源文件(很少这样做)
  2. 将声明和实现写在同一个文件中,并更改后缀名为.hpp,.hpp时约定的名称,不是强制

 person.hpp

#pragma once
#include<iostream>

using namespace std;
template<class T1, class T2>
class Person
{
public:
	Person(T1 name, T2 age);

	void showPerson();

	T1  m_Name;
	T2   m_Age;
};

template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
	this->m_Age = age;
	this->m_Name = name;
}

template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
	cout << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl;
}

.cpp  主函数

#include<iostream>
#include<string>
using namespace std;

//第一种解决方式,直接包含.cpp源文件
//#include "person.cpp"

//第二种解决方式,将.h和.cpp中的内容写到一起,将后缀名为.hpp文件
#include "person.hpp"

//template<class T1, class T2>
//class Person
//{
//public:
//	Person(T1 name, T2 age);
//	
//	void showPerson(); 
//	
//	T1  m_Name;
//	T2   m_Age;
//};
//构造函数类外实现
//template<class T1, class T2>
//Person<T1, T2>::Person(T1 name, T2 age)
//{
//	this->m_Age = age;
//	this->m_Name = name;
//}
//
//template<class T1, class T2>
//void Person<T1, T2>::showPerson()
//{
//		cout << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl;
//}

void test01()

{
	Person<string, int> p("Tom", 100);
	p.showPerson();
}

int main()
{
	test01();   
	return 0;
}

2.2.8类模板与友元

全局函数类内实现:直接在内类声明友元即可

全局函数类外实现:需要提前让编译器知道全局函数的存在

#include<iostream>
#include<string>
using namespace std;

//提前让编译器知道Person类的存在
template<class T1, class T2>
class Person;

//类外实现
template<class T1, class T2>
void printPeson2(Person<T1, T2> p)
{
	cout << "类外实现---姓名:" << p.m_Name << "年龄:" << p.m_Age << endl;
}

//通过全局函数 打印person信息
template<class T1, class T2>
class Person
{
	//全局函数类内实现
	friend void printPeson(Person<T1,T2> p)
	{
		cout << "姓名:" << p.m_Name << "年龄:" <<p.m_Age << endl;
	}
	//全局函数在类外实现
	//加一个空模板函数列表
	//全局函数 类外实现 需要提前让编译器知道全局函数的存在
	friend void printPeson2<>(Person<T1, T2> p);
	

public:
	Person(T1 name, T2 age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
	 
private:	
	T1  m_Name;
	T2  m_Age;
};


//1.全局函数在类内实现
void test01()
{
	Person<string, int> p("Tom", 100);
	printPeson(p);
}

//1.全局函数在类外实现
void test02()
{
	Person<string, int> p("Terry", 200);
	printPeson2(p);
}

int main()
{
	//test01();
	test02();
	return 0;
}

2.2.9类模板案例

1.可以对内置数据类型以及自定义数据类型的数据进行存储

2.将数组中的数据存储到堆区

3.构造函数中可以传入数组的容量

4.提供对应的拷贝构造函数以及operator=防止浅拷贝问题

5.提供尾插法和尾删法对数组中的数据进行增加和删除

6.可以通过下标的方式访问数组中函数

7.可以获取数组中当前元素的个数和数组的容量

MyArray.hpp

//自己实现的通用数组
#pragma once
#include<iostream>
using namespace std;

template<class T>
class MyArray
{
public:
	//有参构造  参数 容量
	MyArray(int capacity)
	{
		//cout << "MyArray的有参构造调用" << endl;
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
    }

	//拷贝构造 (自己写)  防止浅拷贝的问题
	MyArray(const MyArray& arr)
	{
		//cout << "MyArray的拷贝构造调用" << endl;
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//this->pAddress = arr.pAddress; 不可以直接赋值,防止浅拷贝,重复释放内存空间
		
		//深拷贝
		this->pAddress = new T[this->m_Capacity];

		//将arr中的数据都拷贝过来
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	//operator=防止浅拷贝的问题     a=b=c  等号做返回操作,要用引用的方式返回自身
	MyArray& operator=(const MyArray&arr)
	{
		//cout << "MyArray的operator=调用" << endl;
		//先判断原来堆区是否有数据,如果有先释放
		if (this->pAddress != NULL)
		{
			delete[]this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		//深拷贝
		this->m_Size = arr.m_Size;
		this->m_Capacity = arr.m_Capacity;
		this->pAddress=new T[this->m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}


	//尾插法
	void Push_Back(const T & val)
	{
		//判断容量是否更与大小
		if (this->m_Capacity == this->m_Size)
		{
			return;
		}
		this->pAddress[this->m_Size] = val;//在数组的末尾插入数据
		this->m_Size++;//更新数组的大小
	}

	//尾删法
	void Pop_Back()
	{
		//让用户访问不到最后一个元素 ,即为尾删
		if (this->m_Size ==0)
		{
			return;
		}
		this->m_Size--;
	}

	//通过下标的方式访问数组的的元素   arr[0]=100 左值存在,引用的方式返回
	T& operator[](int index)
	{
		return this->pAddress[index];
	}

	//返回数组容量 
	int getCapacity()
	{
		return this->m_Capacity;
	}

	// 返回数组大小
	int getSize()
	{
		return this->m_Size;
	}

	//析构函数
	~MyArray()
	{
		if (this->pAddress != NULL)
		{
			//cout << "MyArray的析构函数调用" << endl;
			delete[]this->pAddress;
			this->pAddress = NULL;

		}

	}
private:
	T * pAddress;//指针指向堆区开辟的真实数组

	int m_Capacity;//数组的容量

	int m_Size;//数组的大小
};

.cpp

#include<iostream>
#include<string>
#include "MyArray.hpp"
using namespace std;

void printIntArray(MyArray<int>& arr)
{
	for(int i=0;i<arr.getSize();i++)
	{
		cout << arr[i] << endl;
	}
	
}
void test01()
{
	MyArray<int> arr1(5);

	for (int i = 0; i < 5; i++) 
	{
		arr1.Push_Back(i);
	}
	cout << "arr1的打印输出为:" << endl;

	printIntArray(arr1);

	cout << "arr1的容量为:" << arr1.getCapacity()<<endl;
	cout << "arr1的大小为:" <<arr1.getSize()<< endl;


	MyArray<int> arr2(arr1);
	cout << "arr2的打印输出为:" << endl;
	printIntArray(arr2);

	//尾删
	arr2.Pop_Back();
	cout << "arr2尾删后" << endl;
	cout << "arr2的容量为:" << arr2.getCapacity() << endl;
	cout << "arr2的大小为:" << arr2.getSize() << endl;

	/*MyArray<int> arr3(100);
	arr3 = arr1;*/
}

//测试自定义数据类型
class Person
{
public:
	Person() {};
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};




//打印数组函数
void printPersonArray(MyArray<Person>&arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout <<"姓名:"<< arr[i].m_Name << "年龄:"<< arr[i].m_Age<<endl;
	}
}
void test02()
{
	MyArray<Person> arr(10);
	Person P1("孙悟空", 999);
	Person P2("韩信", 30);
	Person P3("赵云",20);
	Person P4("妲己", 25);
	Person P5("安其拉", 27);

	//将数据插入到数组中
	arr.Push_Back(P1);
	arr.Push_Back(P2);
	arr.Push_Back(P3);
	arr.Push_Back(P4);
	arr.Push_Back(P5);

	//打印数组
	printPersonArray(arr);

	//输出容量
	cout << "arr容量:" << arr.getCapacity() << endl;

	//输出大小
	cout << "arr大小:" << arr.getSize() << endl;

}
int main()
{
	test01();
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值