模板-类模板

1.类模板和函数模板的区别

1.类模板没有自动类型推导的使用方式

2.类模板在模板参数列表中可以有默认参数

#include "pch.h"
#include <iostream>
#include  <string>
using namespace std;
//类模板
template<class nametype,class agetype=int>//agetype=int为默认参数,z则下面可以不写类型
class person {
public:
	//构造函数
	person(nametype name, agetype age) {

		this->m_name = name;
		this->m_age = age;

	}
	nametype m_name;
	agetype m_age;
};

void test(){
	//1.类模板没有自动类型推导的使用方式
	person p("悟空", 18);//错误,无法用自动类型推导
	//person<string, int>("随悟空", 18);//正确的
}



void test02() {


}
int main()
{
   
}

2.类模板中成员函数和普通类中成员函数时机是有区别的:

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

#include "pch.h"
#include <iostream>


#include <string>
using namespace std;


class person1 {

public:

	void showperson1() {

		cout << "personshow1" << endl;
	}
};


///
class person2 {

public:

	void showperson2() {

		cout << "personshow2" << endl;
	}
};

template<class T>
class myclass {
public:
	T obj;

	//类模板中成员函数
	void func1() {

		obj.showperson1();
	}

	void func2() {

		obj.showperson2();
	}


};



void test() {

	myclass<person1>m;
	m.func1();
	m.func2();//不可调用,说明person1中无showperson2函数,无法调用,
}
int main()
{
   
}

3。.类模板实例化出的对象,像函数传参的方式

一共有三种传入方式:
1.指定传入的类型:直接显示对象的数据类型 (常用)
2.参数模板化:将对相中的参数变为模板进行传递
3.整个类模板化:将这个对象类型模板化进行传递

*/

#include "pch.h"
#include <iostream>
#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 << endl;
		cout << "年龄" << this->m_age << endl;
	}


	T1 m_name;
	T2 m_age;

};

//指定传入的类型:直接显示对象的数据类型
void printperson1(person<string,int>&p) {

	p.showperson();

}

void test01() {
	//1.
	person<string, int>p("随悟空",19);
	
	printperson1(p);
}

//2.参数模板化:将对相中的参数变为模板进行传递
template<class T1,class T2>
void printperson2(person<T1, T2>&p) {

	p.showperson();

}

void test02() {
	//2.
	person<string, int>p("随悟空", 19);

	printperson2(p);
}

//3.整个类模板化

template<class T>
void printperson2(T &p) {

	p.showperson();

}

void test02() {
	
	person<string, int>p("随悟空", 19);

	printperson2(p);
}


int main()
{
    
}

4.当类模板碰到继承时,需要注意以下几点:
1.当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型
2.如果不指定,编译器无法给子类分配内存
3.如果想灵活指定出父类中T的类型,子类也需变为类模板

#include "pch.h"
#include <iostream>
#include<string>


using namespace std;

template<class T>
class base {

	T m;
};

//class son :public base //错误的,因为必须要知道父类中T的类型
class son:public base<int>
{



};
int main()
{
   
}

5.类模板成员函数类外实现

#include "pch.h"
#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();

	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() {


}


int main()
{
    
}

6.类模板分文件编写

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

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

7.类模板与友元

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

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

#include "pch.h"
#include <iostream>
#include <string>

using namespace std;
//通过全局函数,打印person信息


template<class T1,class T2>
class person {

	//1.全局函数  类内实现
	friend void printperson(person<T1,T2> p) {

		cout << "aa" << p.m_name << 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;


};


//类外实现内容
template<class T1, class T2>
void printperson(person<T1, T2> p) {


	cout << "aa" << p.m_name << endl;

}

int main()
{
   

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值