類模板

單個模板類

template<typename T>
class A{
public:
	A(T a= 0):m_a(a){}
	void print(){
		cout << "a = " << this->m_a << endl;
	}
	T m_a;
};

void Aprint(A<int>& a1){//注意一定要有<int>
	a1.print();
}
int main()
{
	A<int>a(11), a1(12), a2(13);
	Aprint(a);
	system("pause");
	return 0;
}

模板類派生子類

template<typename T>
class A{
public:
	A(T a= 0):m_a(a){}
	void print(){
		cout << "a = " << this->m_a << endl;
	}
	T m_a;
};
class B :public A<int>{//派生的時候必須指明父類的數據類型
	//要知道父類所佔內存大小是多少,才能爲其分配內存
public:
	B(int a = 10, int b = 20) :A(10), m_b(b){}
	int m_b;
	void print(){
		cout << "a = " << this->m_a <<" b = "<<this->m_b<< endl;
	}

};
void Aprint(A<int>& a1){
	a1.print();
}
int main()
{
	B b(10, 20);
	b.print();
	system("pause");
	return 0;
}

模板類派生模板類

class A{
public:
	A(T a= 0):m_a(a){}
	void print(){
		cout << "a = " << this->m_a << endl;
	}
	T m_a;
};
class B :public A<int>{//派生的時候必須指明父類的數據類型
	//要知道父類所佔內存大小是多少,才能爲其分配內存
public:
	B(int a = 10, int b = 20) :A(10), m_b(b){}
	int m_b;
	void print(){
		cout << "a = " << this->m_a <<" b = "<<this->m_b<< endl;
	}

};
template<typename T>
class C :public A<T>{
public:
	C(T a, T c ): A(a), m_c(c){}
	void print(){
		cout << "a = " << this->m_a << " c = " << this->m_c << endl;
	}
	T m_c;
};
void Aprint(A<int>& a1){
	a1.print();
}
int main()
{
	C<int> c('j', 20.1);//不會報錯會轉換因爲指定了類型int
	c.print();
	system("pause");
	return 0;
}

函數全部寫在類中

template<typename T>
class Complex{
public:
	Complex(T a = 0, T b = 0){
		this->a = a;
		this->b = b;
	}
	Complex(const Complex& c1) :a(c1.a), b(c1.b){
		cout << "Complex(const Complex& c1)" << endl;
	}
	void operator=(const Complex& c1){
		cout << "operator= " << endl;
		this->a = c1.a;
		this->b = c1.b;
	}
	void printCom()
	{
		cout << " a " << this->a << " b " << this->b << endl;
	}
	Complex operator+(const Complex& c1){
		Complex temp;
		temp.a = this ->a + c1.a;
		temp.b = this->b + c1.b;
		return temp;
	}
	friend ostream& operator<<(ostream&os, const Complex& c1){
		os << " a " << c1.a << " b " << c1.b << endl;
		return os;
	}
	friend Complex operator-(const Complex& c1, const Complex& c2){
		Complex temp;
		temp.a = c1.a - c2.a;
		temp.b = c1.b - c2.b;
		return temp;
	}
private:
	T a;
	T b;
};
int main()
{
	Complex<int> c1(1, 2);
	Complex<int> c2(3, 4);
	Complex<int> c3;
	c3 = c1 + c2;
	cout << c3;
	c3 = c1 - c2;
	cout << c3;
	system("pause");
	return 0; 
}

函數全部寫在類外

#define _CRT_SECURE_NO_WARNINGS 2

//作者:蔣偉泉
#include <iostream>
#include<string>
using namespace std;

template<typename T>
class Complex;

template<typename T>
void myswap(Complex<T> &c1, Complex<T>&c2);

template<typename T>
class Complex{
public:
	Complex(T a = 0, T b = 0);
	Complex(const Complex& c1);

	void printCom();

	Complex operator+(const Complex& c1);
	//當在類外部定義的時候 加<T> 
	friend ostream& operator<< <T>(ostream&os, const Complex& c1);

	friend Complex operator-(const Complex& c1, const Complex& c2){
		Complex temp;
		temp.a = c1.a - c2.a;
		temp.b = c1.b - c2.b;
		return temp;
	}
	friend void myswap<T>(Complex<T> &c1, Complex<T> &c2);
private:
	T a;
	T b;
};
template<typename T>
Complex<T>::Complex( T a = 0, T b = 0){
	this->a = a;
	this->b = b;
}
template<typename T>
Complex<T>::Complex(const Complex<T>& c1):a(c1.a),b(c1.b){
	cout << "Complex(const Complex& c1)" << endl;
}
template<typename T>
void Complex<T>::printCom()
{
	cout << " a " << this->a << " b " << this->b << endl;
}
template<typename T>
Complex<T> Complex<T>::operator+(const Complex<T>& c1){
	cout << "operator+ successed" << endl;
	Complex temp;
	temp.a = this->a + c1.a;
	temp.b = this->b + c1.b;
	return temp;
}
//模板是兩次編譯生成的,第一次編譯生成函數頭 和第二次生成的
//函數頭不一樣
template<typename T>
//這是個全局函數 不屬於Complex
ostream& operator<<(ostream&os, const Complex<T>& c1){
	os << " a " << c1.a << " b " << c1.b << endl;
	return os;
}
template<typename T>
//不要濫用友元函數,尤其是在模板類中
void myswap(Complex<T> &c1, Complex<T>&c2){
	T temp;
	temp = c1.a;
	c1.a = c2.a;
	c2.a = temp;
	temp = c1.b;
	c1.b = c2.b;
	c2.b = temp;
}
int main()
{
	Complex<int> c1(1, 2);
	Complex<int> c2(3, 4);
	Complex<int> c3(c1);
	c3 = c1 + c2;
	cout << c3;
	c3 = c1 - c2;
	cout << c3;
	myswap<int>(c3, c1);
	cout << c3;
	system("pause");
	return 0; 
}

當friend模板類函數用.h(聲明)和.cpp(定義)分開寫時.cpp包含.h的頭文件,在mian函數的源文件中還要報含.cpp
這裏的cpp其實是hpp

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章