类模板作用:建立一个通用的类,类种的成员数据类型可以不用具体制定,用一个虚拟的类型来代表
23.1类模板语法
template<typename T>
类
解释:template---声明创建模板
typename---表面其后面的符号是一种数据类型,可以用class代替
T---通用的数据类型,名称可以替换,通常为大写字母
#include<iostream>
using namespace std;
//类模板
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;
}
NameType m_Name;
AgeType m_Age;
};
void test01()
{
Person<string, int>p1("大傻", 18);//<>尖括号是模板参数列表
p1.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
总结:类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板
23.2类模板与函数模板区别
类模板与函数模板区别主要有两点:
类模板没有自动类型推导的使用方式
类模板在模板参数列表中可以有默认参数
23.2.1类模板没有自动类型推导使用方式
#include<iostream>
using namespace std;
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;
}
NameType m_Name;
AgeType m_Age;
};
//类模板没有自动类型推导使用方式
void test01()
{
//Perosn p("算悟空", 1000);//报错,无法用自动类型推导
Person<string, int>p1("算悟空", 1000);//正确,只能用显示指定类型
p1.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
23.2.2类模板在模板参数列表中可以有默认参数
#include<iostream>
using namespace std;
//类模板在模板参数列表中可以有默认参数
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;
}
NameType m_Name;
AgeType m_Age;
};
//类模板在模板参数列表中可以有默认参数
void test01()
{
Person<string>p1("算悟空", 1000);//AgeType已经定义了类型,AgeType就不用写了
p1.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
总结:
类模板使用只能显示指定类型方式
类模板中的模板参数可以有默认参数(模板不可以)
23.3类模板中成员函数创建时机
类模板中成员函数和普通类中成员函数创建时机是有区别的:
普通类中的成员函数一开始就可以创建
类模板中的成员函数在调用时才创建
(在C++中,类模板是用于生成特定类型的类的蓝图。当我们定义一个类模板时,它并不会立即创建成员函数,而是在模板被实例化(即模板参数确定)时才生成相应的成员函数。
实际上,类模板中的成员函数的定义通常也包含在模板声明中,但只有在实例化时才会生成对应的函数代码。这是由编译器的实现方式决定的,编译器在遇到模板实例化之前不会为所有可能的类型都生成成员函数的定义。相反,当实例化发生时,编译器会根据使用的具体类型生成对应的成员函数。)
(
这样的延迟生成有以下原因:
-
代码效率:模板可以接受不同的数据类型作为参数,生成相应的类和成员函数。如果所有可能的成员函数都在一开始就生成了,将导致代码膨胀,增加编译时间和可执行文件的大小。通过延迟生成,只会生成实际使用到的成员函数,减少了额外开销,提高了代码效率。
-
代码灵活性:类模板能够根据模板参数的类型来适应不同的需求。不同类型的参数可能需要不同的成员函数实现。通过延迟生成,允许每个实例化的类模板根据具体需要生成自己的成员函数,从而灵活地满足特定情况下的需求。
)
#include<iostream>
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;
}
23.4类模板对象做函数参数
学习:类模板实例化出的对象,向函数传参的方式
一共有三种传入方式:
指定传入的类型---直接显示对象的数据类型
参数模板化---------将对象中的参数变为模板进行传递
整个类模板化------将这个对象类型模板化进行传递
#include<iostream>
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 printPerson1(Person<string, int>& p)
{
p.showPerson();
}
void test01()
{
Person<string, int>p("张三",10);
printPerson1(p);
}
//2,参数模板化(像是函数模板,只是用到类模板做形参)
template<class T1,class T2>
void printPerson2(Person<T1, T2>&p)
{
p.showPerson();
cout << "T1的类型为:" << typeid(T1).name() << endl;//看T1是什么类型,T1的名字会以字符串的方式显示出来
//string的名字很长,输出结果:class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
cout << "T2的类型为:" << typeid(T2).name() << endl;
//输出结果:int
}
void test02()
{
Person<string, int>p("李四", 20);
printPerson2(p);
}
//3,整个类模板化(也是函数模板配合着类模板)
template<class T>
void printPerson3(T &p)
{
p.showPerson();
cout << "T的类型为:" << typeid(T).name() << endl;
//输出结果为:class Person<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int>
//把整个Person的类都推了出来string和int
}
void test03()
{
Person<string, int>p("王五", 30);
printPerson3(p);
}
int main()
{
test01();
test02();
test03();
system("pause");
return 0;
}
总结:通过类模板创建的对象可以有三种方式向函数中传参
使用比较广泛的是第一种:指定传入类型
23.5类模板与继承
当类模板碰到继承时,需要注意:
当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型
如果不指定,编辑器无法给子类分配内存
如果想灵活指定出父类T的类型,子类也需变为类模板
#include<iostream>
using namespace std;
//类模板与继承
template<class T>
class Base
{
T m;
};
//class Son :public Base{};//会报错,必须要知道父类的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;
}
总结:如果父类是类模板,子类需要指定出父类中T的数据类型
#include<iostream>
using namespace std;
template<class T1,class T2>
class Person
{
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
cout << "111" << endl;
}
void showPerson()
{
cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
}
private:
T1 m_Name;
T2 m_Age;
};
template<class T1,class T2>
class Son : public Person<T1, T2>
{
public:
Son(T1 name, T2 age) : Person<T1, T2>(name, age)
{
cout << "T1的类型为:" << typeid(T1).name() << endl;
cout << "T2的类型为:" << typeid(T2).name() << endl;
}
};
void test01()
{
Son<string, int>son("李四",20);
son.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
23.6类模板成员函数类外实现
目标:能够掌握类模板中的成员函数类外实现
#include<iostream>
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;
};
//构造函数类外实现
template<class T1,class T2>
Person<T1,T2>::Person(T1 name, T2 age)//如果没有<T1,T2>就是一个普通成员函数类外实现
{
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("Tom", 20);
p.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
总结:类模板中成员函数类外实现时,需要加上模板参数列表
23.7类模板份文件编写
目标:掌握类模板成员函数份文件编写产生的问题以及解决方式
问题:类模板中成员函数创建时机是在调用阶段,导致分文件编写链接不到
解决:
解决方式1:直接包含.cpp文件
解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制
23.7.1解决方式1
#include"person.h"//正常情况下应该用.h,但是test01()里两行会报错,解决办法一是换成.cpp
因为类模板中的成员函数一开始是不创建的所以在.h看不到.cpp中代码
在.cpp中的#include"person.h"使编译器看了一遍.h,之后又看.cpp
头文件下 person.h
#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;
};
源文件下 person.cpp
#include"person.h"
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;
}
源文件下 类模板分文件编写.cpp
#include<iostream>
using namespace std;
//#include"person.h"//正常情况下应该用.h,但是test01()里两行会报错,解决办法一是换成.cpp
//因为类模板中的成员函数一开始是不创建的所以在.h看不到.cpp中代码
//在.cpp中的#include"person.h"使编译器看了一遍.h,之后又看.cpp
#include"person.cpp"
void test01()
{
Person<string, int>p("Tom", 20);
p.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
23.7.2解决方式2(更常用)
将.h和.cpp中的内容写到一起,将后缀名改为.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_Name = name;
this->m_Age = age;
}
template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
}
源文件下 类模板分文件编写.cpp
#include<iostream>
using namespace std;
#include"person.hpp"
void test01()
{
Person<string, int>p("Tom", 20);
p.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
总结:主流的解决方式是第二种,将类模板成员函数写一起,并将后缀名改为.hpp
23.8类模板与友元
目标:掌握类模板配合友元函数的类内和类外实现
全局函数类内实现---直接在类内声明友元即可
全局函数类外实现---需要提前让编辑器知道全局函数的存在
#include <iostream>
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;
}
//通过全局函数打印Person信息
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);
//加<>时因为没有<>时是一个函数模板的函数声明,得让它变成类模板
//如果全局函数是类外实现,需要让编译器提前知道这个函数的存在,把实现放到最上面
//放到最上面后因为还没有声明过Person,所以在前面写一个class Person;
//还得告诉编译器,Person是一个模板,所有在Person前加上template<class T1, class T2>
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("Tom", 20);
printPerson(p);
}
//全局函数在类外实现
void test02()
{
Person<string, int>p("Jerry", 10);
printPerson2(p);
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
总结:建议群居函数做类内实现,用法简单,而且编译器可以直接识别
23.9类模板案例
案例描述:实现一个通用的数组类,要求如下:
可以对内置数据类型以及自定义数据类型的数据进行存储
将数组中的数据存储到堆区
构造函数中可以传入数组的容量
提供对应的拷贝构造函数以及operator=防止浅拷贝问题
提供尾插法和尾删法对数组中的数据进行增加和删除
可以通过下标的方式问数组中的元素
可以获取数组中当前元素个数和数组的容量
第一步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[arr.m_Capacity];//深拷贝
//将arr中的数据都拷贝过来
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
//operator=防止浅拷贝问题
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_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;
}
~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>
using namespace std;
#include"MyArray.hpp"
void test01()
{
MyArray<int>arr1(5);
MyArray<int>arr2(arr1);
MyArray<int>arr3(100);
arr3 = arr1;//会先把arr3的100清空之后再把arr1的数据进行深拷贝到arr3
}
int main()
{
test01();
system("pause");
return 0;
}
第二步,完整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[arr.m_Capacity];//深拷贝
//将arr中的数据都拷贝过来
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
//operator=防止浅拷贝问题
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_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;
}
//尾插法
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--;
}
//通过下标方式访问数组中的元素
//通过重载()
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>
using namespace std;
#include"MyArray.hpp"
void printIntArray(MyArray<int>&arr)
{
for (int i = 0; i < arr.getSize(); i++)
{
cout << arr[i] << endl;
}
}
void test01()
{
MyArray<int>arr1(5);
MyArray<int>arr2(arr1);
MyArray<int>arr3(100);
arr3 = arr1;//会先把arr3的100清空之后再把arr1的数据进行深拷贝到arr3
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>arr4(arr1);
cout << "arr2的打印输出为:" << endl;
printIntArray(arr4);
//尾删
arr4.Pop_Back();
cout << "arr4的容量为:" << arr4.getCapacity() << endl;
cout << "arr4的大小为:" << arr4.getSize() << endl;
}
//测试自定义数据类型
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("张三", 10);
Person p2("李四", 20);
Person p3("王五", 30);
Person p4("老六", 40);
Person p5("隐身哇", 50);
//将数据插入到数组中
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();
test02();
system("pause");
return 0;
}