c++模板:类型参数化
函数模板:参数的类型不具体指定,用通用类型替代。在调用时,编译器会根据实参的类型推导出形参的类型
函数模板:
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
using namespace std;
void swap(int &x, int &y)
{
int tmp = x;
x = y;
y = tmp;
}
void swap(char &x, char &y)
{
char tmp = x;
x = y;
y = tmp;
}
// 函数模板实现
template <class T> // 定义一个模板 模板的通用类型为T
void swap_temp(T &a, T &b)
{
T tmp = a;
a = b;
b = tmp;
}
void test01()
{
// int a = 1;
// int b = 2;
// swap(a,b);
// cout <<a << " " << b<< endl;
// char a = '1';
// char b = '2';
// cout <<a << " " << b<< endl;
// swap(a,b);
// cout <<a << " " << b<< endl;
int c =1;
int d =2;
char c1 = 'a';
char d2 = 'b';
swap_temp(c,d);
swap_temp(c1,d2);
cout<< c<<" "<<d<<endl;
cout<< c1<<" "<<d2<<endl;
}
int main()
{
test01();
return 0;
}
类模板:
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
using namespace std;
template <class T1, class T2>
class Animal
{
public:
Animal(T1 a, T2 b)
{
age = a;
data = b;
}
T1 age;
T2 data;
};
template <class T1, class T2>
void show(Animal<T1,T2> &p)
{
cout<< p.age << " " << p.data << endl;
}
template <class T1>
void show1(T1 &p)
{
cout<< p.age << " " << p.data << endl;
}
void test01()
{
// 类模板不能自动类型推导
Animal<int,int> dog(10,11);
show(dog);
show1(dog);
Animal<int,string> cat(4,"lili");
show(cat);
show1(cat);
}
int main()
{
test01();
return 0;
}
类模板和继承
示例:
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
using namespace std;
template <class T>
class Base
{
public:
Base(T a)
{
this->a = a;
}
T a;
};
class Son1:public Base<int>
{
public:
Son1(int x1, int a):Base<int>(a),x(x1)
{}
int x;
};
template <class T1, class T2>
class Son2:public Base<T2>
{
public:
Son2(T1 x1, T2 a):Base<T2>(a), x(x1)
{}
T1 x;
};
void test01()
{
Son1 p(10,20);
Son2<int,string> p2(10,"lucy");
cout << p2.x << " " << p2.a << endl;
}
int main()
{
test01();
return 0;
}
类模板成员函数类内实现
类模板成员函数类外实现
举例:
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
using namespace std;
template <class T1, class T2>
class Person
{
public:
Person(T1 a, T2 b)
{
this->a = a;
this->b = b;
}
void show()
{
cout << a <<" "<< b << endl;
}
T1 a;
T2 b;
};
template <class T1, class T2>
class Person1
{
public:
Person1(T1 a, T2 b);
void show();
T1 a;
T2 b;
};
// 类模板的成员函数在类外实现, 需要写成函数模板
template <class T1, class T2>
Person1<T1, T2>::Person1(T1 a, T2 b)
{
this->a = a;
this->b = b;
}
template <class T1, class T2>
void Person1<T1, T2>::show()
{
cout << a <<" "<< b << endl;
}
void test01()
{
Person<int,int> p1(1,2);
p1.show();
Person1<int,int> p12(11,22);
p12.show();
}
int main()
{
test01();
return 0;
}
类模板和友元
举例:
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
using namespace std;
template <class T1, class T2>
class Person;
template <class T1, class T2>
void showPerson1(Person<T1, T2> &p);
template <class T1, class T2>
void showPerson(Person<T1, T2> &p)
{
cout<<p.a <<" " << p.b << endl;
}
template <class T1, class T2>
class Person
{
friend void showPerson1<>(Person<T1, T2> &p);
friend void showPerson<>(Person<T1, T2> &p);
friend void showPerson2(Person<T1, T2> &p) // 定义一个全局函数并声明为类的友元
{
cout<<p.a <<" " << p.b << endl;
}
public:
Person(T1 a, T2 b)
{
this->a = a;
this->b = b;
}
private:
T1 a;
T2 b;
};
template <class T1, class T2>
void showPerson1(Person<T1, T2> &p)
{
cout<<p.a <<" " << p.b << endl;
}
void test01()
{
Person<int, string> p(10, "lucy");
showPerson(p);
showPerson1(p);
showPerson2(p);
}
int main()
{
test01();
return 0;
}