【1.3 类模板】

1.3 类模板

1.3.1 类模板语法

1、模板作用:

  • 建立一个通用的类,类中的成员 数据类型可以不具体制定,用一个虚拟的类型来代表

2、语法:

template<typename T>
  • 解释:
  • template — 声明创建模板
  • typename — 表明其后面的符号是一种数据类型,可以用class代替
  • T — 通用的数据类型,名称可以替换,通常为大写字母

3、代码

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

template<class NameType, class AgeType>
class Person
{
public:
	Person(NameType name, AgeType age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	
	void showPerson()
	{
		cout << "name:" << this->m_Name << "  age:" << this->m_Age << endl;
	}

	AgeType m_Age;
	NameType m_Name;
};

void test01()
{
	Person<string, int>p1("孙悟空",999);
	p1.showPerson();
}

int main()
{
	test01();
	system("pause");
	return 0;
}

4、总结:

  • 类模板和函数模板语法相似,在声明模板template后面加类,以此类为类模板

5、运行结果
在这里插入图片描述

1.3.2 类模板和函数模板的区别

1、类模板和函数模板的区别主要有两点:

  • 类模板没有自动类型推导的使用方式
  • 类模板在模板参数列表中可以有默认参数

2、代码

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

template<class NameType, class AgeType=int>
class Person
{
public:
	Person(NameType name, AgeType age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	void showPerson()
	{
		cout << "name:" << this->m_Name << "  age:" << this->m_Age << endl;
	}

	AgeType m_Age;
	NameType m_Name;
};

//1、类模板没有自动类型推导使用方式
void test01()
{
	//Person p1("张三", 11);
	Person<string, int>p1("孙悟空", 1000);//正确,只能用显示指定类型
	p1.showPerson();
}

//2、类模板在模板参数列表中可以有默认参数
void test02()
{
	Person<string>p1("李四", 11);
	p1.showPerson();
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

3、 总结:

  • 类模板使用只能用显示指定类型方式
  • 类模板中的模板参数可以有默认参数

4、运行结果
在这里插入图片描述

1.3.3 类模板中的成员函数创建时机

1、类模板中成员函数和普通成员函数创建时机是有区别的:

  • 普通类中的成员函数一开始就可以创建
  • 类模板中成员函数在调用时才去创建

2、代码

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

class Person1
{
public:
	void showPerson1()
	{
		cout << "Person1 show" << endl;
	}

};

class Person2
{
public:
	void showPerson2()
	{
		cout << "Person2 show" << endl;
	}

};

template<class T>
class MyClass
{
public:
	T obj;

	//类模板中的成员函数
	void func1()
	{
		obj.showPerson1();
	}

	void func2()
	{
		obj.showPerson2();
	}
};

void test01()
{
	MyClass<Person1>m;
	m.func1();
	//m.func2
}

int main()
{
	test01();
	system("pause");
	return 0;
}

3、总结:

  • 类模板中成员函数在调用时才去创建

4、运行结果
在这里插入图片描述

1.3.4 类模板对象做函数参数

1、学习目标:

  • 类模板实例化出的对象,向函数传参的方式

2、一共有三种传入方式:

  • 指定传入的类型 —直接显示对象的数据类型
  • 参数模板化 —将对象中的参数变为模板进行传递
  • 整个类模板化 —将这个对象类型 模板化进行传递

3、代码

#include<iostream>
#include<string>
using namespace std;
template<class T1,class T2>
class Person
{
public:
	Person(T1 name, T2 age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	void showPerson()
	{
		cout << "姓名:"<<this->m_Name << " 年龄:" << this->m_Age << endl;
	}
	T1 m_Name;
	T2 m_Age;
};
//1、指定传入类型
void printPerson(Person<string, int>& p)
{
	p.showPerson();
}
void test01()
{
	Person<string, int> p("张三",10);
	printPerson(p);
	//m.func2
}

//2、参数化模板化
template<class T1=string,class T2=int>
void printPerson2(Person<T1, T2>& p)
{
	p.showPerson();
	//cout << "T1的类型为:" << typeid(T1).name() << endl;
	//cout << "T2的类型为:" << typeid(T2).name() << endl;
}
void test02()
{
	Person<string, int>p("李四", 10);
	printPerson2(p);
}

//3、整个模板化
template<class T>
void printPerson3(T&p)
{
	p.showPerson();
	cout << "T的类型为:" << typeid(T).name() << endl;
}
void test03()
{
	Person<string, int>p("王五", 12);
	printPerson3(p);
}


int main()
{
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}

4、总结:

  • 通过类模板创建的对象,可以有三种方式向函数中进行传参
  • 使用比较广发的是第一种,指定传入的类型

5、运行结果

在这里插入图片描述

1.3.5 类模板与继承

1、当类模板碰到继承时,需要注意以下几点:

  • 当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型
  • 如果不指定,编译器无法给子类分配内存
  • 如果想灵活指定出父类中T的类型,子类也需要变为类模板

2、代码

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

template<class T>
class Base
{
public:
	T m;
};

//class Son :public Base //错误,c++编译器需要给子类分配内存,必须知道父类中T的类型才可以继承
class Son :public Base<int>//必须指定一个类型
{

};

void test01()
{
	Son s1;
}

//如果想灵活指定父类中T类型,子类也需要变类模板
template<class T1,class T2>
class Son2 :public Base<T2>
{
public:
	Son2()
	{
		cout << "T1的类型为:" << typeid(T1).name() << endl;
		cout << "T2的类型为:" << typeid(T2).name() << endl;
	}
	T1 obj;
};

void test02()
{
	Son2<int, char>s2;
}


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

3、总结:

  • 如果父类是模板,子类需要指定出父类中T的数据类型

4、运行结果
在这里插入图片描述

1.3.6 类模板成员函数类外实现

1、学习目标:

  • 能够掌握类模板中的成员函数类外实现

2、代码

#include<iostream>
#include<string>
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_Name = name;
	this->m_Age = age;
}

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

void test01()
{
	Person<string, int> p("张三", 18);//要指定参数类型
	p.showPerson();
}

int main()
{
	test01();

	system("pause");
	return 0;
}

3、总结:

  • 类模板中成员函数类外实现时,需要加上模板参数列表

4、运行结果:
在这里插入图片描述

1.3.7 类模板分文件编写

1、学习目标:

  • 掌握类模板分文件编写产生的问题以及解决方案

2、问题:

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

3、解决:

  • 解决方式1:直接包含.cpp源文件
  • 解决方案2:将声明和实现内容写到一起,将后缀名改为.hpp文件,hpp是约定的名称并不是强制

4、代码:
解决方案1代码
person.h

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

//类模板成员函数类外实现
template<class T1, class T2>
class Person
{
public:
	Person(T1 name, T2 age);
	void showPerson();

	T1 m_Name;
	T2 m_Age;
};

person.cpp

#include"person.h"
using namespace std;

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

}

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

源文件

#include<iostream>
#include"person.cpp"
using namespace std;

void test01()
{
	Person<string, int> p("张三", 18);//要指定参数类型
	p.showPerson();
}

int main()
{
	test01();

	system("pause");
	return 0;
}

解决方案2代码
person.hpp

#pragma once
#include<iostream>
#include<string>
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_Name = name;
	this->m_Age = age;

}

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

源文件

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

void test01()
{
	Person<string, int> p("张三", 18);//要指定参数类型
	p.showPerson();
}

int main()
{
	test01();

	system("pause");
	return 0;
}

5、 总结:

  • 主流的解决方式是第二种,将类模板成员函数写到一起,并将后缀改为.hpp

6、运行结果
在这里插入图片描述

1.3.8 类模板与友元

1、学习目标:

  • 掌握类模板配合友元函数的类内实现和类外实现

2、

  • 全局函数类内实现 - 直接在类内声明友元即可
  • 全局函数类外实现 - 需要提前让编译器知道全局函数的存在

3、代码

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

//提前让编译器知道Person类的存在
template<class T1, class T2> class Person;
//类外实现
template<class T1,class T2>
void printPerson2(Person<T1, T2> p)
{
	cout << "类外实现 --- 姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}

template<class T1,class T2>
class Person
{
	//全局函数 类内实现
	friend void printPerson(Person<T1, T2>p)
	{
		cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
	}

	//全局函数 类外实现
	friend void printPerson2<>(Person<T1, T2>p);

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

void test01()
{
	Person<string, int> p("张三", 18);//要指定参数类型
	printPerson(p);
}

void test02()
{
	Person<string, int> p("李四", 20);//要指定参数类型
	printPerson(p);
}

int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}

4、总结:

  • 建议用全局函数做类内实现,用法简单,而且编译器可以直接识别

5、运行结果
在这里插入图片描述

1.3.9 类模板案例

1、案列描述:

  • 实现一个通用的数组类

2、要求:

  • 可以对内置数据类型以及自定义数据类型的数据进行存储
  • 将数组中的数据存储到堆区
  • 构造数数中可以传入数组的容量
  • 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
  • 提供尾插法和尾删法对数组中的数据进行增加和删除
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组中当前元素个数和数组的容量

3、代码
myArry.hpp

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

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

	//拷贝构造
	MyArry(const MyArry& arr)
	{
		//cout << "MyArry的拷贝构造的调用" << endl;
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//this->pAddress = arr.pAddress;浅拷贝会导致堆区空间重复释放

		//深拷贝
		this->pAddress = new T[arr.m_Capacity];

		//将arr中的数据都拷贝过来
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}
	//重载= 防止浅拷贝问题
	MyArry& operator=(const MyArry& arr)
	{
		//cout << "MyArry的重载=的调用" << endl;
		//先判断原来堆区是否有数据,如果有先释放
		if (this->pAddress != NULL)
		{
			delete[]this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

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

	//利用下标访问数组元素
	T& operator[](int index)
	{
		return this->pAddress[index];
	}
	//输出数组
	//void showArry();

	//尾插法
	void Push_Back(const T& val)
	{
		//判断容量大小
		if (this->m_Capacity == this->m_Size)
		{

			return;
		}
		else
		{
			this->pAddress[this->m_Size] = val;
			this->m_Size++;
		}
	}
	//尾删法
	void Pop_Back()
	{
		if (this->m_Size == 0)
		{
			return;
		}
		else
		{
			this->m_Size--;
		}
	}

	//获取数据容量
	int getCapacity()
	{
		return this->m_Capacity;
	}

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

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

private:
	//数组
	T* pAddress;
	int m_Capacity;//容量
	int m_Size;//大小
};

源文件

#include"myArry.hpp"
using namespace std;

void printIntArry(MyArry<int>& arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr[i] << " ";
	}
}

void printIntArryS(MyArry<string>& arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr[i] << " ";
	}
}

class Person
{
public:
	Person() {};
	Person(string name,int age)
	{
			this->m_Age = age;
			this->m_Name = name;
	}

		int m_Age;
		string m_Name;
};

void printIntArryP(MyArry<Person>& arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << "姓名:" << arr[i].m_Name << " 年龄:" << arr[i].m_Age<< endl;
	}
}

void test01()
{
	MyArry<int> arr1(5);
	for (int i = 0; i < 5; i++)
	{
		arr1.Push_Back(i);
	}
	cout << "arr1为:" << endl;
	printIntArry(arr1);
	cout << endl;

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

	MyArry<int>arr2(5);
	arr2 = arr1;
	cout << "未修改的arr2为:" << endl;
	printIntArry(arr2);
	cout << endl;
	cout << "arr2的容量为:" << arr2.getCapacity() << endl;
	cout << "arr2的大小为:" << arr2.getSize() << endl;
	arr2.Pop_Back();

	cout << "修改后的arr2为:" << endl;
	printIntArry(arr2);
	cout << endl;
	cout << "arr2的容量为:" << arr2.getCapacity() << endl;
	cout << "arr2的大小为:" << arr2.getSize() << endl;
}

void test02()
{
	MyArry<string>arr1(5);
	arr1.Push_Back("a");
	arr1.Push_Back("b");
	arr1.Push_Back("c");
	arr1.Push_Back("d");
	arr1.Push_Back("e");
	cout << "arr1为:" << endl;
	printIntArryS(arr1);
	cout << endl;
	cout << "arr1的容量为:" << arr1.getCapacity() << endl;
	cout << "arr1的大小为:" << arr1.getSize() << endl;
	MyArry<string>arr2(5);

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

	cout << "修改后的arr2为:" << endl;
	printIntArryS(arr2);
	cout << endl;
	cout << "arr2的容量为:" << arr2.getCapacity() << endl;
	cout << "arr2的大小为:" << arr2.getSize() << endl;
}


void test03()
{
	MyArry<Person>p(10);
	Person p1("李白", 999);
	Person p2("小鲁班", 10);
	Person p3("公孙离", 16);
	Person p4("孙尚香", 15);
	Person p5("艾琳", 18);
	p.Push_Back(p1);
	p.Push_Back(p2);
	p.Push_Back(p3);
	p.Push_Back(p4);
	p.Push_Back(p5);
	cout << "p为:"<<endl;
	printIntArryP(p);

}

int main()
{
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}

4、运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值