C++模板(二)

目录

8、类模板的分文件编写问题以及解决

9、类模板碰到友元函数

10、类模板的应用——数组封装


8、类模板的分文件编写问题以及解决
  • .h .cpp分别写声明和实现
  • 但是由于 类模板的成员函数运行阶段才去创建,导致包含.h头文件,不会创建函数的实现,无法解析外部命令
  • 解决方案  包含 .cpp文件 (不推荐)
  • 不要进行分文件编写,写到同一个文件中,进行声明和实现,后缀名改为.hpp
  • 约定俗成的
//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;
}
//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include"Person.cpp"
using namespace std;
 
//建议模板不要做分文件编写,一般写到一个类中即可
int main()
{
	Person<string, int>p("猪八戒", 10);
	p.showPerson();
 
	system("pause");
	return 0;
}

注意:一般在头文件中不会出现 #include"Person.cpp",所以需要将Person.cpp中的成员函数实现拷贝到Person.h中,并且将Person.h命名为Person.hpp,同时需要把main.cpp中的#include"Person.cpp"改为#include"Person.hpp"即可。
 

9、类模板碰到友元函数
  • 友元函数类内实现
  • friend void printPerson( Person<T1 ,T2> & p )
  • 友元函数类外实现
  • friend void printPerson<>(Person<T1, T2> & p); //没有<>普通函数 声明  加上 <>模板函数声明
  • 让编译器看到 函数 并且看到这个Person类型

类内实现

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
 
template<class T1,class T2>
class Person
{
	//友元函数类内实现
	friend void printPerson(Person<T1,T2> & p)
	{
		cout << "姓名:" << p.m_Name << "   年龄:" << p.m_Age << endl;
	}
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", 10);
	printPerson(p);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

类外实现

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
 
//让编译器看到Person类的声明
template<class T1, class T2>class Person;
//让编译器提前看到printPerson声明
template<class T1, class T2>void printPerson(Person<T1, T2> & p);
 
 
template<class T1, class T2>
class Person
{
	//友元函数类内实现  利用空参数列表告诉编译器 模板函数的声明
	friend void printPerson<>(Person<T1, T2> & p);  //普通函数  声明
	/*{
		cout << "姓名:" << p.m_Name << "   年龄:" << p.m_Age << endl;
	}*/
public:
	Person(T1 name, T2 age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
 
private:
	T1 m_Name;
	T2 m_Age;
};
 
//类外实现
template<class T1,class T2>
void printPerson(Person<T1, T2> & p)
{
	cout << "姓名:" << p.m_Name << "   年龄:" << p.m_Age << endl;
}
 
void test01()
{
	Person<string, int>p("Tom", 10);
	printPerson(p);
}
int main()
{
	test01();
	system("pause");
	return 0;
}
10、类模板的应用——数组封装
//MyArray.hpp
#pragma once
#include<iostream>
using namespace std;
 
template<class T>
class MyArray
{
public:
	//构造
	explicit MyArray(int capacity)  //防止隐式类型转换 eg:MyArray arr = 10;
	{
		this->m_Capcity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capcity];
	}
 
	MyArray(const MyArray &array)
	{
		this->m_Capcity = array.m_Capcity;
		this->m_Size = array.m_Size;
		this->pAddress = new T[this->m_Capcity];
 
		for (int i = 0; i < m_Size; i++)
		{
			this->pAddress[i] = array[i];
		}
	}
	
	~MyArray()
	{
		if (this->pAddress==NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
 
	//赋值操作符重载
	MyArray& operator=(MyArray& array)
	{
		//先判断原始数据,有就清空
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
 
		this->m_Capcity = array.m_Capcity;
		this->m_Size = array.m_Size;
		this->pAddress = new T[this->m_Capcity];
 
		for (int i = 0; i < m_Size; i++)
		{
			this->pAddress[i] = array[i];
		}
	}
 
	//[]重载
	/*MyArray arr(10);
	arr[0] = 100;*/
	T & operator[](int index)
	{
		return this->pAddress[index];
	}
 
	//尾插法
	void push_Back(T val)
	{
		this->pAddress[this->m_Size] = val;
		this->m_Size++;
	}
 
	//获取大小
	int getSize()
	{
		return m_Size;
	}
	//获取容量
	int getCapacity()
	{
		return this->m_Capcity;
	}
private:
	T *pAddress; //指向堆区指针
	int m_Capcity; //容量
	int m_Size; //大小
 
};
//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include"MyArray.hpp"
using namespace std;
 
//输出int类型数组
void printIntArray(MyArray<int>& array)
{
	for (int i = 0; i < array.getSize(); i++)
	{
		cout << array[i] << endl;
	}
}
 
class Person
{
public:
	Person()
	{
 
	}
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
 
//输出Person类型的数组
void printPersonArray(MyArray<Person>& array)
{
	for (int i = 0; i < array.getSize(); i++)
	{
		cout << "姓名 " << array[i].m_Name << "年龄:" << array[i].m_Age << endl;
	}
}
 
 
 
int main()
{
	MyArray <int>arr(10);
 
	for (int i = 0; i < 10; i++)
	{
		arr.push_Back(i + 100);
	}
	printIntArray(arr);
 
 
	Person p1("MT", 10);
	Person p2("DD", 11);
	Person p3("yy", 12);
	Person p4("Md", 13);
 
	MyArray<Person>arr2(10);
	arr2.push_Back(p1);
	arr2.push_Back(p2);
	arr2.push_Back(p3);
	arr2.push_Back(p4);
	printPersonArray(arr2);
 
	system("pause");
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值