类模板template的使用!

本文深入探讨C++中的类模板template,介绍其基本概念、用法及常见应用,包括如何创建、实例化和使用模板,以实现代码复用和泛型编程。通过实例解析模板在解决类型无关问题中的优势。
摘要由CSDN通过智能技术生成


#include "stdafx.h"
#include <queue>
#include <iostream>
#include <map>
#include <string>
using namespace std;

// 类模板的使用
// 类模板中 类成员函数的申明与定义
// 结构体模板的使用
// 函数模板的使用

template <typename T> class A
{
public:
	A();
	A(T _a,T _b); // T 代表任何类型的数据
	T sum();

private:
	T a;
	T b;

};

template <class T1,class T2>
struct Test
{
	T1 H;
	T2 W;
};

template <class T>A<T>::A() //类模板的成员函数是模板函数
{
	a=0;
	b=0;
}

template <class T>A<T>::A(T _a,T _b) 
{
	a=_a;
	b=_b;
}

template <class T>T A<T>::sum()//注意观察成员函数的写法
{
	return a + b;
}

template <class X>void swapFun(X &x,X &y)  // X 代表任何数据类型
{
	X tmp;
	tmp=x;
	x=y;
	y=tmp;
}

int _tmain(int ar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿的杂货店

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值