C++的学习日记day7(模板)

模板
生活中 模具
外形不变,(球体模具) 铁球,塑料球
int myadd(int a,int b)
double myadd(double a,double b)
char myadd(char a,char b);

void swap(int *,int *)
值传递 指针传递 引用传递

假如一个程序的功能仅仅是针对某种特定的数据类型进行处理,
可以将这些特定的数据类型说明为参数,即将该程序说明为模板

优点:
节省代码量;

函数模板用法:
template <模板参数表>
<函数返回类型> 函数名(<函数参数列表>)

1.template 模板声明
2.<模板参数表>可以有一个 可以有多个,逗号隔开
3.<函数返回类型> 一种就是模板参数
                 一种就是普通类型
  bool check(int a,int b)
  bool check(double a,double b)
4.<函数参数列表>  可以是模板参数 也可以是普通类型

template void swap(T *a,T *b){}
template T myadd(T a,T b){}

实现原理: “两次编译”
(声明)函数模板不是一个真正的函数,编译系统不给他产生任何可执行代码
(函数模板只是对函数的描述)
(编译)当编译系统发现函数调用的时候,编译系统会根据实参的类型,
先匹配(看是否正确),如果匹配成功,会生成一个“重载函数”。
该重载函数的定义体与函数模板的定义体相同,
只不过是用实参的实际类型去替换函数形参表中与模板参数表相同的数据类型
该重载函数成为模板函数

宏定义
#define myMax(x,y) ((x>y)?a:b)
宏定义与函数模板的区别:
宏定义只是简单的文本替换,不做类型检查(万一两个参数类型都不一样)

函数模板遇上重载函数
*如果有一个非模板函数的参数与函数调用的参数一致,那么调用这个非模板函数
(int myMax(int a,int b)) (优先考虑普通函数调用)
*如果从相应模板生成的模板函数中,有一个模板函数的参数与之相匹配,那就…
*从相对应参数类型转换之后匹配非模板函数
(模板函数没有隐式类型转换,普通函数有)

类模板:(比较适用于各种数据结构)

语法:
声明:
template class <类模板名>
{

}
	class_1 :模板参数声明
	class_2 :类的声明

类外写成员函数:
	template <class T>
	<返回值类型> 类模板名(<模板参数表>)::成员函数(<形参表>)

创建实例:
类名<模板数据类型表> 对象名(<…>);
A a(100);

#include <iostream>
using namespace std;

template <class T> 
class A
{
private:
	T a;
public:
	A(T a)
	{
		this->a = a;
	}
	T get_a();
};

template <class T> 
T A<T>::get_a()
{
	return a;
}

int main()
{
	A<int> a(100);
	cout << a.get_a() << endl;

	return 0;
}

类模板实现栈

#include <iostream>
using namespace std;

template <class T>
class mystack   //类型:  mystack<T>
{
private:
	int size;    //数组大小
	int top;     //栈顶指针(不是真的指针,只是用来表明栈顶元素位置)
	T *stackptr; //指向堆栈存储空间的指针(数组名)
public:
	mystack(int s);  
	~mystack(){delete[] stackptr;}
	bool push(T pushvalue);
	bool pop();
	bool isEmpty(){return top == -1;}
	bool isFull(){return top == size-1;}
	int get_top(){return top;}
};

template <class T>
mystack<T>::mystack(int s)
{
	size = s>0?s:10;
	top = -1;
	stackptr = new T[size];
}

template <class T>
bool mystack<T>::push(T pushvalue)
{
	if(!isFull())
	{
		stackptr[++top] = pushvalue;
		return true;
	}
	return false;
}

template <class T>
bool mystack<T>::pop()
{
	if(!isEmpty())
	{
		stackptr[top--];
		return true;
	}
	return false;
}


int main()
{
	mystack<double> doublestack(5);
	double f=0.1;
	while(doublestack.push(f))
	{
		cout << f << " ";
		f++;
	}
	cout << endl;
	cout << doublestack.isFull() << endl;
	while(doublestack.pop())
	{
		cout << doublestack.get_top() << endl;
	}
	cout << endl;
	cout << doublestack.isEmpty() << endl;

	return 0;
}

普通类派生类模板

#include <iostream>
using namespace std;

class Base
{
private:
	int a;
protected:
	int b;
public:
	void fun()
	{
		cout << "base fun" << endl;
	}
};

template <class T>
class Derived:public Base   //普通类派生出类模板
{
protected:
	T t;
public:
	void fun()
	{
		b = 10;
		cout <<  "derived fun" << endl;
	}
};

int main()
{
	Derived<int> dint;
	dint.fun();

	return 0;
}

类模板派生出类模板

#include <iostream>
using namespace std;

template <class T>
class Base
{
protected:
	T t;
public:
	Base(T t)
	{
		this->t = t;
	}
	void fun()
	{
		cout << "t:" << t << endl; 
	}
};

template <class T1,class T2>
//class T1表示派生类自身的模板参数
//class T2表示基类中模板参数
class Derived:public Base<T2>
{
private:
	T1 x;
	T2 y;
public:
	Derived(T1 x,T2 y,T2 t):Base(t)
	{
		this->x = x;
		this->y = y;
	}
	void fun()
	{
		cout << "t:" << t << endl; 
		cout << "x:" << x << endl; 
		cout << "y:" << y << endl; 
	}
};

int main()
{
	Derived<double,int> d1(10.07,20,30);
	d1.fun();

	return 0;
}

类模板相关说明
所有的类模板函数写在类的内部 安全
所有的类模板函数写在类的外部,在一个cpp中

#include <iostream>
using namespace std;

template <class T>
class Complex;
template <class T>
Complex<T> mysub(Complex<T> &c1,Complex<T> &c2);


template <class T>
class Complex
{
private:
	T a;
	T b;
public:
	Complex(T a,T b)
	{
		this->a = a;
		this->b = b;
	}
	Complex operator+(Complex &c)
	{
		Complex tmp(c.a+this->a , c.b+this->b);
		return tmp;
	}
	friend ostream& operator<< <T>(ostream& os,Complex &c);
	//声明友元关系  
	//外部定义时候 
	friend Complex<T> mysub<T>(Complex<T> &c1,Complex<T> &c2);
	//普通友元函数:
	  //要先在函数开始时候声明  不要加<T>
	  //声明友元关系时候要加<T>
	  //定义函数体时候要加<T>

};

template <class T>
ostream& operator<< (ostream& os,Complex<T> &c)
{
	os << c.a << "+" << c.b << "i" << endl;
	return os;
}

template <class T>
Complex<T> mysub<T>(Complex<T> &c1,Complex<T> &c2)
{
	Complex<T> temp(c1.a+c2.a , c2.b+c1.b);
	return temp;
}

int main()
{
	Complex<int> a(1,2);
	Complex<int> b(3,4);
	Complex<int> tmp =  a+b;
	cout << tmp << endl;

	Complex<int> c4 = mysub<int>(a,b);
	cout << c4 << endl;

	return 0;
}

类模板中的static关键字
类模板根据类型产生不同的模板类
Test
Tets , Test
在不同的模板类的对象中共享这个static成员变量

#include <iostream>
using namespace std;

template <class T>
class Test
{
public:
	static T a;
};

template <class T>
T Test<T>::a = 20;   //类模板中静态成员变量的声明

int main()
{
	Test<int> t1;
	cout << t1.a << endl;  20

	t1.a = 10;  

	Test<double> t2;
	cout << t2.a << endl;

	Test <int> t3;
	cout << t3.a << endl;

	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值