C++自学笔记(23 模板-类模板)
22 模板-类模板
本次记录类模板,还请各位大佬批评指正!
类模板语法
作用: 建立一个通用类,类中的成员 数据类型可以不具体制定,用一个虚拟的类型来代表。
语法:
template< typename T>
类
解释:
template - 声明创建模板
typename - 表明其后面的符号是一种数据类型,可以用class代替
T - 通用的数据类型,名称可以替换,通常为大写字母
#include<iostream>
#include <stdlib.h>
#include<string>
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 << endl << "Age: " << this->m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
void test01()
{
Person<string, int> p1("Tom", 20);
p1.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
总结: 类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板。
类模板与函数模板的区别
类模板与函数模板区别主要有两点:
1、类模板没有自动类型推导的使用方式,只能使用显示指定类型方式;
2、类模板在模板参数列表中可以有默认参数。
#include<iostream>
#include <stdlib.h>
#include<string>
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 p1("Tom", 20);//错误 无法使用自动类型推导
Person<string, int> p1("孙悟空", 20);//只能用显示指定类型
p1.showPerson();
}
void test02()
{
Person<string> p2("猪八戒", 30);
p2.showPerson();
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
类模板中成员函数创建时机
类模板中成员函数和普通类中成员函数创建时机是有区别的:
普通类中的成员函数一开始就可以创建;
类模板中的成员函数在调用时才创建。
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
//类模板中成员函数创建时机
class Person1
{
public:
void showPerson1()
{
cout << "Show Penson1" << endl;
}
};
class Person2
{
public:
void showPerson2()
{
cout << "Show Penson2" << endl;
}
};
template<class T>
class myClass
{
public:
T m_obj;
//类模板中的成员函数 并不是一开始就创建的,而是在模板调用时再创建
void func1()
{
m_obj.showPerson1();
}
void func2()
{
m_obj.showPerson2();
}
};
void test01()
{
myClass<Person1> m1;
m1.func1();
//m1.func2();//错误 说明函数调用才会去创建成员函数
myClass<Person2> m2;
//m2.func1();//错误
m2.func2();
}
int main()
{
test01();
system("pause");
return 0;
}
总结: 类模板中成员函数并不是一开始就创建的,在调用时才去创建。
类模板对象做函数参数
目标:类模板实例化出的对象,向函数传参的方式。
一共有三种传入方式:
1、指定传入的类型 — 直接显示对象的数据类型
2、参数模板化 — 将对象中的参数变为模板进行传递
3、整个类模板化 — 将这个对象类型 模板化进行传递
#include<iostream>
#include <stdlib.h>
#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 printPerson1(Person<string, int> &p)
{
p.showPerson();
}
void test01()
{
Person<string, int> p("大师兄", 20);
printPerson1(p);
}
//2、参数模板化
template<class T1,class T2>
void printPerson2(Person<T1, T2>& p)
{
p.showPerson();
//利用typeid查看模板中指定的数据类型
cout << "T1 的类型为:" << typeid(T1).name() << endl;
cout << "T2 的类型为:" << typeid(T2).name() << endl;
}
void test02()
{
Person<string, int> p("二师兄", 30);
printPerson2(p);
}
//3、整个类模板化递
template<class T>
void printPerson3(T &p)
{
p.showPerson();
cout << "T 的数据类型为:" << typeid(T).name() << endl;
}
void test03()
{
Person<string, int> p("三师弟", 40);
printPerson3(p);
}
int main()
{
test01();
test02();
test03();
system("pause");
return 0;
}
总结:
1、通过类模板创建的对象,可以有三种方式向函数进行传参;
2、使用比较广泛的是第一种 — 指定传入的类型,可读性更强。
类模板与继承
当类模板碰到继承时,需要注意以下几点:
1、当子类继承的父类是一个类模板时,子类在声明的时候 要指定出父亲中 T 的类型
2、如果不指定,编译器无法给子类分配内存
3、如果想灵活指出父类中 T 的类型,子类也需要变为类模板
#include<iostream>
#include <stdlib.h>
#include<string>
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传给了T1 ;char传给了T2
//T1 是子类中obj 的数据类型,现在为整型
//T2 给父类,父类Base中的 T 是char数据类型
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
类模板成员函数的类外实现
目标:掌握类模板中的成员函数类外实现。
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
//类模板成员函数的类外实现
template<class T1,class T2>
class Person
{
public:
Person(T1 name, T2 age);//类外实现
/*Person(T1 name,T2 age)//类内实现
{
this->m_Name = name;
this->m_Age = age;
}*/
void showPerson();
/*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)
//Person<T1, T2>中如果没有尖括号,就是普通成员函数的类外实现
//加上模板的参数列表<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;
}
总结: 类模板中成员函数类外实现时,需要加上模板的参数列表。
类模板分文件编写
目标: 掌握类模板成员函数份文件编写产生的问题以及解决方式。
问题: 类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到
解决:
方式1:直接包含.cpp源文,但是一般不这么用
Perosn.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;
};
Perosn.cpp
#include"Perosn.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;
}
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include"Person.cpp"
//类模板分文件编写
void test01()
{
//包含"Perosn.h"时,下面两行代码出错,2个无法解析的外部命令
//解决方法之一:直接包含源文件"Perosn.cpp"
Person<string, int>p("Tom", 30);
p.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
方式2:将声明(.h)和实现(.cpp)写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,不是强制的
Perosn.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<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include"Perosn.hpp"
//类模板分文件编写
void test01()
{
//包含"Perosn.h"时,下面两行代码出错,2个无法解析的外部命令
//解决方法之二:将声明(.h)和实现(.cpp)写到同一个文件中,并更改后缀名为.hpp
Person<string, int>p("Tom", 30);
p.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
总结: 主流解决方式是第二种,将类模板成员函数写到一起,并将后缀名改为.hpp 。
类模板与友元
目标:掌握类模板配合友元函数的类内和类外实现。
全局函数类内实现 - 直接在类内声明友元即可
全局函数类外实现 - 需要提前让编译器知道全局函数的存在
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
//类模板与友元
//通过全局函数 打印Person的信息
//提前让编译器知道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 printPerson1(Person <T1, T2>p)
{
cout << "类内实现: 姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}
//全局函数 类外实现
//friend void printPerson2(Person <T1, T2>p);//普通函数的声明
//加空模板参数列表
//如果全局函数是 类外实现的,需要让编译器提前知道
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("Tom", 20);
printPerson1(p);
}
void test02()
{
Person <string, int>p("Jack", 30);
printPerson2(p);
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
总结: 建议全局函数做类内实现,用法简单,而且编译器可以直接识别。
类模板案例
案例描述:实现一个通用的数组类,要求如下:
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[arr.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_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)
{
cout << "数组容量已满!" << endl;
return;
}
this->pAddress[this->m_Size] = val;//在数组末尾插入数据
this->m_Size++;//更新数组大小
}
//尾删法
void Pop_Back()
{
//让用户访问不到最后一个元素,即为尾删
if (this->m_Size == 0)
{
cout << "数组为空!" << endl;
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;//数组大小
};
#include<iostream>
#include <stdlib.h>
#include<string>
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>arr3(100);//有参构造
//arr3 = arr1;//operator=
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;
}
//测试自定义数据类型
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("蒙犽", 27);
Person p2("百里", 30);
Person p3("虞姬", 18);
Person p4("项羽", 80);
Person p5("耀", 25);
//将数据插入到数组中
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;
}