1.3类模板

1.3.1类模板语法

类模板作用:

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

语法:
 

template <typename T>

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

template <class stringName,class intName>
class Person
{
public:

	Person(stringName name, intName age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	void showMenu()
	{
		cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
	}
	stringName m_Name;
	intName m_Age;
};
void test01()
{
	Person<string, int>p1("孙悟空", 999);
	p1.showMenu();

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

1.3.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 showMenu()
	{
		cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
	}
	nameType m_Name;
	ageType m_Age;
};
//类模板在模板参数列表中可以有默认参数

void test01()
{
	Person<string>p("猪八戒", 999);
	p.showMenu();
}

int main()
{

	test01();
	system("pause");
	return 0;
}

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

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

  • 普通类中的成员函数一开始就可以创建
  • 类模板中的成员函数在调用时才创建
#include <iostream>
using namespace std;

//类模板中成员函数创建时机
//类模板中成员函数在调用时才去创建
class Person1
{
public:
	void showPerson1()
	{
		cout << "showperson1" << endl;
	}
};
class Person2
{
public:
	void showPerson2()
	{
		cout << "showperson2" << endl;
	}
};
template <class T>
class myClass
{
public:

	T obj;
	void fun1()
	{
		obj.showPerson1();
	}
	void fun2()
	{
		obj.showPerson2();
	}
};

int main()
{

	myClass<Person1>p;
	p.fun1();
	//p.fun2();//函数出错,说明函数调用才会创建成员函数
	system("pause");
	return 0;
}

总结:类模板中的成员函数并不是一开始就创建的,在调用时才去创建

1.3.4 类模板对象做函数参数

学习目标:

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

一共有三种入方式:

  1. 指定传入的类型-------直接显示对象的数据类型
  2. 参数模板化-------------将对象中的参数变为模板进行传递
  3. 整个类模板化----------将这个对象类型 模板化进行传递
#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 << "  年龄为:" << 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("孙悟空", 333);
	printPerson1(p);
}
//2.参数模板化
template <class T1,class T2>
void printPerson2(Person<T1, T2>&p)
{
	p.showPerson();
}
void test02()
{
	Person<string, int>p("猪八戒", 99);
	printPerson2(p);

}
//3.整个类模板化
template <class T>
void printPerson3(T &p)
{
	p.showPerson();
	//cout << "T数据类型" << typeid(T).name << endl;
}
void test03()
{
	Person<string, int>p("唐僧", 199);
	printPerson3(p);
	
}
int main()
{
	//test01();
	//test02();
	test03();
	system("pause");
	return 0;
}

总结:

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

1.3.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的数据类型

1.3.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 << " 年龄为:" << 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 << " 年龄为:" << this->m_Age << endl;
}
void test01()
{
	Person<string, int>p("Tom",19);
	p.showPerson();

}

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

1.3.7 类模板分文件编写

学习目标:

掌握类模板成员函数分文件编写产生的问题以及解决方式

问题:

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

解决:

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

1.3.8类模板与友元

学习目标:

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

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

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

#include <iostream>
using namespace std;
#include<string>
template <class T1,class T2>
class Person;
template<class T1, class T2>
void frintPerson2(Person<T1, T2>p)
{
	cout << "姓名为: " << p.m_Name << " 年龄为:" << p.m_Age << endl;
}

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

public:
	Person(T1 name, T2 age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	/*void showPerson()
	{
		cout << "姓名为:" << this->m_Name << "  年龄为:" << this->m_Age << endl;
	}*/
private:
	T1 m_Name;
	T2 m_Age;
};


void test01()
{
	Person <string, int>p("Tom", 18);
	frintPerson(p);
}
void test02()
{
	Person<string, int>p2("Jerry", 18);
	frintPerson2(p2);
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

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

1.3.9 类模板封装案例——数据管理

main函数

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

void printMyarr(myArray<int> &arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr[i] << endl;
	}
	cout << "---------" << 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 printMyarr2(myArray<Person> &arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << "姓名为:" << arr[i].m_Name << " 年龄为:" << arr[i].m_Age << endl;
	}
	
}
void test01()
{
	myArray<int>arr1(5);
	for (int i = 0; i < 5; i++)
	{
		arr1.Push_Back(i);
	}
	cout << "打印输出为:" << endl;
	printMyarr(arr1);

	myArray<int>arr2(arr1);
	printMyarr(arr2);
	arr2.Del_Back();
	printMyarr(arr2);
}
void test02()
{
	myArray<Person>arr(10);
	Person p1("韩信", 25);
	Person p2("李白", 26);
	Person p3("芈月", 24);
	Person p4("小乔", 30);
	Person p5("韩信", 20);

	arr.Push_Back(p1);
	arr.Push_Back(p2);
	arr.Push_Back(p3);
	arr.Push_Back(p4);
	arr.Push_Back(p5);

	printMyarr2(arr);
	cout << "数组容量为:" << arr.getCapcity() << endl;
	cout << "数组大小为:" << arr.getSize() << endl;
}
int main()
{

	test02();
	system("pause");
	return 0;

}

.hpp文件

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

template <class T>
class myArray
{
public:
	//有参构造 参数 容量
	myArray(int capcity)
	{
		this->m_Capcity = capcity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capcity];
	}
	//拷贝构造
	myArray(const myArray&arr)
	{
		this->m_Capcity = arr.m_Capcity;
		this->m_Size = arr.m_Size;
		//深拷贝
		this->pAddress=new T[arr.m_Capcity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	//operator= 防止浅拷贝问题
	myArray& operator = (const myArray &arr)
	{
		if (this->pAddress != NULL)
		{
			delete[]this->pAddress;
			this->pAddress = NULL;
			this->m_Capcity = 0;
			this->m_Size = 0;
		}
		this->m_Capcity = arr.m_Capcity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[arr.m_Capcity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;//返回自身
	}
	//尾插法
	void Push_Back(T &val)
	{
		if (this->m_Capcity == this->m_Size)
		{
			return;
		}
		this->pAddress[this->m_Size] = val;//在数组的最后一个位置插入数据
		this->m_Size++;
	}
	//尾删法
	void Del_Back()
	{
		//让用户访问不到最后一个数据 就是尾删,逻辑删除
		if (this->m_Size == 0)
			return;
		this->m_Size--;
	}

	//通过下标方式访问数组中的元素 arr[0]=100; 函数重载
	T& operator[](int index)
	{
		return this->pAddress[index];
	}
	//返回数组容量
	int getCapcity()
	{
		return this->m_Capcity;
	}

	//返回数组大小
	int getSize()
	{
		return this->m_Size;
	}
	//析构函数
	~myArray()
	{
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值