2021-8-9 C++提升学习 -> 1.3 类模板

目录

1.3.1类模板语法

1.3.2 类模板与函数模板区别

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

1.3.4类模板对象中做函数参数

1.3.5类模板与继承

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

1.3.7类模板分文件编写

1.3.8类模板与友元

1.3.9类模板案例


1.3.1类模板语法

template<class NameType,class AgeType>
class person{

public:

NameType m_Name;
    AgeType m_Age;
}

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

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

template<class NameType,class AgeType>
class person
{
public:
	person(NameType name,AgeType age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
	void showperson()
	{
		cout << "name = " << this->m_Name << endl;
		cout << "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");
}

1.3.2 类模板与函数模板区别

类模板与函数模板区别
1、类模板不能自动推导类型
2、类模板在模板参数列表中可以有默认参数

template<class NameType, class AgeType = int>//默认参数

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

//类模板与函数模板区别
//1、类模板不能自动推导类型
//2、类模板在模板参数列表中可以有默认参数

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

};


void test01()
{
	//person p1("孙悟空", 999);错误的
	person<string, int> p1("孙悟空", 999);//正确,只能显示指定类型
	p1.showperson();
}

void test02()
{
	person<string> p2("猪八戒", 499);
	p2.showperson();
}
int main() {

	test01();
	test02();
	system("pause");
}

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

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

#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");
}

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_Age = age;
		this->m_Name = name;
	}
	void showperson()
	{
		cout << "name = " << this->m_Name << endl;
		cout << "age = " << 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("孙悟空",999);
	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>p1("猪八戒", 499);
	printperson2(p1);
}

//3、整个类模板化
template <class T>
void printperson3(T &p)
{
	p.showperson();
	cout << "T 的类型为 " << typeid(T).name() << endl;
}


void test03()
{
	person<string, int>p2("唐僧", 29);
	printperson3(p2);
}
int main() {

	test03();
	test02();
	test01();
	system("pause");
}

1.3.5类模板与继承

class Son :public Base//错误的,必须知道父类中的T的数据类型才能继承子类

如果想灵活的指定父类中的T的类型,子类也需要变类模板

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

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

//class Son :public Base//错误的,必须知道父类中的T的数据类型才能继承子类
class Son :public Base<int>
{
public:
};

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");
}

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

类外实现的时候,需要加上模板的参数列表以及函数的作用域

template<class T1,class T2>
void person<T1, T2>::showperson()

#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_Age = age;
	this->m_Name = name;
}

//成员函数类外实现
template<class T1,class T2>
void person<T1, T2>::showperson()
{
	cout << "name = " << this->m_Name << endl;
	cout << "age = " << this->m_Age << endl;
}


void test01()
{
	person<string,int>p1("TON",21);
	p1.showperson();
}
int main() {

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

1.3.7类模板分文件编写

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

1、第一种解决方法, 直接包含源文件
inlcude "person.cpp"

2、第二种解决方法,将.h和.cpp的内容写到一起,将后缀名改为.hpp文件

1.3.8类模板与友元

全局函数 类外实现
   加一个空参数列表
   如果全局函数是类外实现的话,需要让编译器提前知道这个函数存在

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

template<class T1,class T2>
class person;

template <class T1, class T2>
void printPerson2(person<T1, T2>p)
{
	cout << "类外实现的内容:" << endl;
	cout << "name = " << p.m_Name << endl;
	cout << "age = " << p.m_Age << endl;
}

template<class T1, class T2>
class person
{
	//全局函数,类内实现
	friend void printPerson(person<T1, T2>p)
	{
		cout << "name = " << p.m_Name << endl;
		cout << "age = " << p.m_Age << endl;
	}

	//全局函数 类外实现
	//加一个空参数列表
	//如果全局函数是类外实现的话,需要让编译器提前知道这个函数存在
	friend void printPerson2<>(person<T1, T2>p);
public:
	person(T1 name, T2 age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}

private:
	T1 m_Name;
	T2 m_Age;

};



void test01()
{
	person<string, int> p("tom", 20);
	printPerson(p);
}
void test02()
{
	person<string, int> p("jerry", 20);
	printPerson2(p);

}

int main() {

	test02();
	test01();
	system("pause");
}

1.3.9类模板案例

cpp:

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

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;

	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 << endl;
		cout << "年龄: " << arr[i].m_Age << endl;
	}
}


void test02()
{
	MyArray<person>arr(10);

	person p1("孙悟空", 999);
	person p2("猪八戒", 499);
	person p3("唐僧", 29);
	person p4("白龙马",199);
	person p5("沙和尚", 399);

	//将数据插入到数组中
	arr.Push_Back(p1);
	arr.Push_Back(p2);
	arr.Push_Back(p3);
	arr.Push_Back(p4);
	arr.Push_Back(p5);

	//打印数组
	printPersonArray(arr);


}

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



	system("pause");
}

hpp:

//自己的通用的数组类
#pragma once
#include <iostream>
#include <string>
using namespace std;

template<class T>
class MyArray
{
public:
	//有参构造
	MyArray(int capacity)
	{
		//cout << "Myarray的有参构造调用" << endl;
		this->m_Capacity = capacity;
		this->pAddress = new T[this->m_Capacity];
		this->m_Size = 0;


	}

	//拷贝构造
	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)
	{
		//先判断原来堆区是否有数据,如果有,先释放
		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--;
	}

	//返回数组的容量
	int getCapacity()
	{
		return this->m_Capacity;
	}
	//返回数组大小
	int getSize()
	{
		return this->m_Size;
	}

	//通过下标的方式来访问数组中的元素
	T& operator[](int index)
	{
		return this->pAddress[index];
	}


	//析构函数
	~MyArray()
	{
		//cout << "Myarray的析构构造调用" << endl;
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
private:
	T* pAddress;//指针指向堆区开辟的真实数组

	int m_Capacity;//数组容量

	int m_Size;//数组大小
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值