C++模版——类模版

目录

1.类模版语法

2.类模版与函数模版区别

3.类模版中成员函数创建时机

4.类模版对象做函数参数

5.类模版与继承

6.类模版成员函数类外实现

7.类模版分文件编写

8.类模版与友元

9.类模版案例


1.类模版语法

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

语法:

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

示例:

#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;
	}
	NameType m_Name;
	AgeType m_Age;
};
void test01()
{
	Person<string, int> p1("乒乓球", 999);
	p1.showPerson();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

2.类模版与函数模版区别

类模版与函数模版区别主要有两点:

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

3.类模版中成员函数创建时机

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

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

4.类模版对象做函数参数

通过类模版创建的对象,可以有三种方式向函数中进行传参:

  1. 指定传入类型——直接显示对象的数据类型
  2. 参数模板化——将对象中的参数变为模版进行传递
  3. 整个类模板化——将这个对象类型模板化进行传递
#include<iostream>
using namespace std;
#include <string>
//类模板
template<class NameType, class AgeType = int>
class Person
{
public:
	Person(NameType name, AgeType age)
	{
		this->mName = name;
		this->mAge = age;
	}
	void showPerson()
	{
		cout << "name: " << this->mName << " age: " << this->mAge << endl;
	}
public:
	NameType mName;
	AgeType mAge;
};
//1、指定传入的类型
void printPerson1(Person<string, int>& p)
{
	p.showPerson();
}
void test01()
{
	Person <string, int >p("孙悟空", 100);
	printPerson1(p);
}
//2、参数模板化
template <class T1, class T2>
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("猪八戒", 90);
	printPerson2(p);
}
//3、整个类模板化
template<class T>
void printPerson3(T& p)
{
	cout << "T的类型为: " << typeid(T).name() << endl;
	p.showPerson();
}
void test03()
{
	Person <string, int >p("唐僧", 30);
	printPerson3(p);
}
int main() {
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}

注:使用比较广泛的是第一种——指定传入的类型

5.类模版与继承

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

  • 当子类继承的父类是一个类模版时,子类在声明的时候要指定出父类中T的类型。
  • 如果不指定,编译器无法给予子类分配内存。
  • 如果想灵活指定出父类中T的类型,子类也需要变为类模版。
#include<iostream>
using namespace std;
template<class T>
class Base
{
	T m;
};
//class Son:public Base //错误,c++编译需要给子类分配内存,必须知道父类中T的类型才可以向下继承
class Son :public Base<int>//必须指定一个类型
{

};
void test01()
{
	Son c;
}
//类模版继承类模版可以用T2指定父类中的T类型
template<class T1,class T2>
class Son2 :public Base<T2>
{
public:
	Son2()
	{
		cout << typeid(T1).name() << endl;
		cout << typeid(T2).name() << endl;
	}
};
void test02()
{
	Son2<int, char>child1;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

6.类模版成员函数类外实现

注:类模版中成员函数类外实现时,需要加上模版的参数列表。

#include<iostream>
using namespace std;
#include<string>
//类模版成员函数类外实现
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 << endl;
	//	cout << "年龄:" << this->m_age << endl;
	//}
	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 << endl;
		cout << "年龄:" << this->m_age << endl;
}
void test01()
{
	Person<string, int>p1("回归服", 45);
	p1.showPerson();

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

7.类模版分文件编写

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

解决:

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

注:主流是解决方式2,将类模版成员函数写到一起,并将后缀名改为.hpp。

person.hpp中的代码:

#pragma once
#include <iostream>
using namespace std;
#include <string>
template<class T1, class T2>
class Person {
public:
	Person(T1 name, T2 age);
	void showPerson();
public:
	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.h"
//#include "person.cpp" //解决方式1,包含cpp源文件
//解决方式2,将声明和实现写到一起,文件后缀名改为.hpp
#include "person.hpp"
void test01()
{
	Person<string, int> p("Tom", 10);
	p.showPerson();
}
int main() {
	test01();
	system("pause");
	return 0;
}

8.类模版与友元

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

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

#include<iostream>
using namespace std;
#include <string>
//2、全局函数配合友元 类外实现 - 先做函数模板声明,下方在做函数模板定义,在做友元
template<class T1, class T2> class Person;
//如果声明了函数模板,可以将实现写到后面,否则需要将实现体写到类的前面让编译器提前看到
//template<class T1, class T2> void printPerson2(Person<T1, T2> & p);
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
{
	//1、全局函数配合友元 类内实现
	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;
};
//1、全局函数在类内实现
void test01()
{
	Person <string, int >p("Tom", 20);
	printPerson(p);
}
//2、全局函数在类外实现
void test02()
{
	Person <string, int >p("Jerry", 30);
	printPerson2(p);
}
int main() {
	//test01();
	test02();
	system("pause");
	return 0;
}

9.类模版案例

实现一个通用的数组类,要求如下:

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

myArray.hpp中的代码:

#pragma once
#include <iostream>
using namespace std;
template<class T>
class MyArray
{
public:
	//构造函数
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		pAddress = new T[this->m_Capacity];
	}
	//拷贝构造
	MyArray(const MyArray& arr)
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[this->m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			//如果T为对象,而且还包含指针,必须需要重载 = 操作符,因为这个等号不是 构造 而是赋值,
			// 普通类型可以直接= 但是指针类型需要深拷贝
			this->pAddress[i] = arr.pAddress[i];
		}
	}
	//重载= 操作符 防止浅拷贝问题
	MyArray& operator=(const MyArray& myarray) {
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}
		this->m_Capacity = myarray.m_Capacity;
		this->m_Size = myarray.m_Size;
		this->pAddress = new T[this->m_Capacity];
		for (int i = 0; i < this->m_Size; i++) {
			this->pAddress[i] = myarray[i];
		}
		return *this;
	}
	//重载[] 操作符 arr[0]
	T& operator [](int index)
	{
		return this->pAddress[index]; //不考虑越界,用户自己去处理
	}
	//尾插法
	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--;
	}
	//获取数组容量
	int getCapacity()
	{
		return this->m_Capacity;
	}
	//获取数组大小
	int getSize()
	{
		return this->m_Size;
	}
	//析构
	~MyArray()
	{
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}
	}
private:
	T* pAddress; //指向一个堆空间,这个空间存储真正的数据
	int m_Capacity; //容量
	int m_Size; // 大小
};

 数组类封装.cpp中的代码:

#include "myArray.hpp"
#include <string>
void printIntArray(MyArray<int>& arr) {
	for (int i = 0; i < arr.getSize(); i++) {
		cout << arr[i] << " ";
	}
	cout << endl;
}
//测试内置数据类型
void test01()
{
	MyArray<int> array1(10);
	for (int i = 0; i < 10; i++)
	{
		array1.Push_back(i);
	}
	cout << "array1打印输出:" << endl;
	printIntArray(array1);
	cout << "array1的大小:" << array1.getSize() << endl;
	cout << "array1的容量:" << array1.getCapacity() << endl;
	cout << "--------------------------" << endl;
	MyArray<int> array2(array1);
	array2.Pop_back();
	cout << "array2打印输出:" << endl;
	printIntArray(array2);
	cout << "array2的大小:" << array2.getSize() << endl;
	cout << "array2的容量:" << array2.getCapacity() << endl;
}
//测试自定义数据类型
class Person {
public:
	Person() {}
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
public:
	string m_Name;
	int m_Age;
};
void printPersonArray(MyArray<Person>& personArr)
{
	for (int i = 0; i < personArr.getSize(); i++) {
		cout << "姓名:" << personArr[i].m_Name << " 年龄: " << personArr[i].m_Age << endl;
	}
}
void test02()
{
	//创建数组
	MyArray<Person> pArray(10);
	Person p1("孙悟空", 30);
	Person p2("韩信", 20);
	Person p3("妲己", 18);
	Person p4("王昭君", 15);
	Person p5("赵云", 24);
	//插入数据
	pArray.Push_back(p1);
	pArray.Push_back(p2);
	pArray.Push_back(p3);
	pArray.Push_back(p4);
	pArray.Push_back(p5);
	printPersonArray(pArray);
	cout << "pArray的大小:" << pArray.getSize() << endl;
	cout << "pArray的容量:" << pArray.getCapacity() << endl;
}
int main() {
	//test01();
	test02();
	system("pause");
	return 0;
}

  • 26
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值