类模板

总结:

1) 先写出一个实际的类。 由于其语义明确, 含义清楚, 一般不会出错。2) 将此类中准备改变的类型名(int 要改变为 float char)改用一个自己指定的虚拟类型名(如上例中的 numtype)3) 在类声明前面加入一行, 格式为:template <class 虚拟类型参数>如:template <class numtype> //注意本行末尾无分号class Compare{}; //类体4) 用类模板定义对象时用以下形式:类模板名类模板名<实际类型名> 对象名;类模板名<实际类型名> 对象名(实参表列);如:Compare<int> cmp;Compare<int> cmp(3,7);5) 如果在类模板外定义成员函数, 应写成类模板形式:template <class 虚拟类型参数>函数类型 类模板名<虚拟类型参数>::成员函数名(函数形参表列) {}关于类模板的几点说明:1) 类模板的类型参数可以有一个或多个, 每个类型前面都必须加 class, 如:template <class T1,class T2>class someclass {…};在定义对象时分别代入实际的类型名, 如:someclass<int,double> obj;2) 和使用类一样, 使用类模板时要注意其作用域, 只能在其有效作用域内用它定义对象。3) 模板可以有层次, 一个类模板可以作为基类, 派生出派生模板类。

#include<iostream>
using namespace std;
 template <class numtype>//声明
 class Compare
 {public:
	 Compare(numtype a, numtype b);
	 numtype max();
	 numtype min();
 private:
	 numtype x;
	 numtype y;


 };
 template<class numtype>
 Compare<numtype>::Compare(numtype a, numtype b)//类外定义时需要先声明,需要在域运算符前加上<numtype>
 {
	 x = a;
	 y = b;
 }
 template<class numtype>
 numtype Compare<numtype>::max()
 {
	 if (x > y)
		 return x;
	 else
		 return y;
 }
 template<class numtype>
 numtype Compare<numtype>::min()
 {
	 if (x< y) ? x : y;
 }
 int main()
 {
	 Compare<int>c1(1, 2);//类模板的调用
	 cout << c1.max() << endl;

 
 
 
	 system("pause ");
	 return 0;

 }

//类模板派生普通类:

#include<iostream>
using namespace std;
template<class T>
class A1
{
public:
	A1(T a)
	{
		this->a = a;
	}
	int  get()
	{
		return a;
	}
private:
	T a;
};
class B:public A1<int>//从模板类派生时,需要知道父类所占内存空间大小
{
public:
	B(int a,int b) :A1<int>(a)
	{
		this->b = b;
	}
	void printf()
	{
		cout << get() << "   " << b << endl;
	}
private:
	int b;
};
int main03()
{
	B b1(1,2);//
	b1.printf();
	system("pause");
	return 0;
}

//类模板派生模板类
#include<iostream>
using namespace std;
template<class T>
class A2
{
public:
	A2(T a)
	{
		this->a = a;
	}
	int get()
	{
		return a;
	}
private:
	T a;

};
template<class T>
class B1:public A2<T>
{public:
	B1(T a,T b) :A2<T>(a)
	{
		this->b = b;
	}
	void print()
	{
		cout << get() << b << endl;
	}
private:
	T b;

};
int main()
{
	B1 <int> c(23, 20);
		c.print();


	system("pause");
	return 0;
}

#include<iostream>
using namespace std;
template<typename T>
class Complex
{
public:
	Complex(T real, T imag);
	/*{
		this->real = real;
		this->imag = imag;
	}*/
public:
	void print();
	Complex operator+(Complex &c1);
	Complex operator-(Complex &c1);
	friend ostream& operator<< <T>(ostream&out, Complex &c3);//注意写法



	//void print()//函数体写在类的内部,没有问题
	/*{
		cout << "real:" << real << "imag:" << imag << endl;
	}*/
	//Complex operator+(Complex &c1)//函数体写在类的内部,没有问题
	/*{
		Complex tem(real + c1.real, imag + c1.imag)
			return tem;
	}*/
	//friend ostream& operator<<(ostream&out, Complex &c3)//函数体写在类的内部,没有问题
	/*{
		out << c3.real << "+" << c3.imag << "i" << endl;
			return out;
	}*/

private:
	T real;
	T imag;
};
//构造函数写在类的外部
template<typename T>
Complex<T> ::Complex(T real, T imag)
{
	this->real = real;
	this->imag = imag;
}

//成员函数写在类的外部
template<typename T>
void Complex<T>::print()
{
	cout << "real:" << real << "imag:" << imag << endl;
}


//运算符重载写在类的外部
//模板类需要具体化,分配内存,注意写法
template<typename T>
Complex<T> Complex<T>::operator+(Complex<T> &c1)
{
	Complex tem(real + c1.real, imag + c1.imag);
	return tem;
}
template<typename T>
Complex<T> Complex<T>::operator-(Complex<T> &c1)
{
	Complex temp(real - c1.real, imag - c1.imag);
	return temp;
}
//友元函数写在类的外部
//本质:模板是两次编译,第一次生成的函数头与第二次函数头不同
//错误	1	error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & 
//__cdecl operator<<
//(class std::basic_ostream<char,struct std::char_traits<char> > &,class Complex<int> &)" 
//(??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Complex@H@@@Z),
//该符号在函数 _main 中被引用	C:\Users\HSY\Desktop\函数模板001\函数模板001\复数类的模板类.obj	函数模板001

template<typename T>
ostream& operator<<(ostream&out, Complex<T> &c3)
{ if (c3.imag>0)
	out << c3.real << "+" << c3.imag << "i" << endl;
else
    out << c3.real << c3.imag << "i" << endl;
	return out;
}

int main()
{
	//模板类需要具体化,分配内存,注意写法
	Complex<int> c1(1,2);
	Complex<int> c2(3, 4);
	Complex<int> c3 = c1 + c2;
	Complex<int> c4 = c1 - c2;
	cout << c3;
	cout << endl;
	cout << c4;
	cout << endl;
	system("pause");
	return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值