模版是泛型编程的基础,泛型编程即以一种独立于任何特定类型的方式编写代码。每个容器都有一个单一的定义,可定义不同类型的容器,如vector <int>
函数模版
template <typename T>
inline T const& Max (T const& a, T const& b){
return a < b ? b:a;
}
类模版
类模版允许用户为类定义一种模式,使类中的数据成员、成员函数的参数、成员函数的返回值,能够取任意类型。
如果类中数据成员的数据类型不确定,或成员函数的参数或返回值的类型不能确定,就须将此类声明为模版,它不是代表一个具体的类,而是代表一类类。
对成员数据类型不同的类的抽象,只要创建一个类模版,给出一套程序代码,就可以用来生成多种具体的类。(类模版的实例)
template <class Type>
class Stack
{
private:
int top;
public:
Stack();
}
template <class Type>
Stack<Type>:: Stack()
{ top = 0; }
模版类
模版类是类模版的实例化。类模版的使用是将类模版实例化成具体的类。
Stack<int> Sint;
Stack<string> Sstring;
编译器将Stack<Type>
模版生成两个独立的类声明和两组独立的类方法。类声明Stack<int>
将用int替换模版中的所有的Type,而类声明Stack<string>
将用string替换Type。
用模版类定义 int 和 string 类型的类,来实现栈。
#include <iostream>
#include <vector>
#include <stdexcept> //throw
//#include <cstdlib> <==> #include <stdlib.h>
using namespace std;
template <class T> //此时class与typename等同,作用是表明后边的T是一个类型
class Stack
{
private:
vector<T> elems; // 元素
public:
void push(T const&); // 入栈
void pop(); // 出栈
T top() const; // 返回栈顶元素
bool empty() const // 如果为空则返回真
{ return elems.empty(); }
};
template <class T>
void Stack<T>::push (T const& elem){
elems.push_back(elem); // 追加传入元素的副本
}
template <class T>
void Stack<T>::pop (){
if (elems.empty())
{ throw out_of_range("Stack<>::pop(): empty stack"); }
elems.pop_back(); //删除最后的一个元素
}
template <class T>
T Stack<T>::top () const
{
if (elems.empty())
{ throw out_of_range("Stack<>::top(): empty stack"); }
return elems.back(); //返回最后一个元素的副本
}
int main()
{
Stack<int> intStack; //int 类型的栈
Stack<string> stringStack; //string 类型的栈
//操作int类型的栈
intStack.push(7);
cout << intStack.top() <<endl;
//操作string类型的栈
stringStack.push("hello");
cout << stringStack.top() << std::endl;
stringStack.pop();
stringStack.pop();
}
/* result: 7 hello Exception: Stack<>::pop(): empty stack */
模版函数
用来实现不同类型值的比较
#include <iostream>
#include <string>
using namespace std;
template <typename T>
inline T const& Max (T const& a, T const& b){
return a < b ? b:a;
}
int main ()
{
int i = 39; int j = 20;
cout << "Max(i, j): " << Max(i, j) << endl;
double f1 = 13.5; double f2 = 20.7;
cout << "Max(f1, f2): " << Max(f1, f2) << endl;
string s1 = "Hello"; string s2 = "World";
cout << "Max(s1, s2): " << Max(s1, s2) << endl;
return 0;
}
类模版的继承
#include <iostream>
#include <string>
using namespace std;
enum eColor { none = 0, red, white};
class Color
{
public:
Color(eColor color);
string getStrColor();
protected:
eColor mColor;
};
Color::Color(eColor _color){
mColor = _color;
}
string Color::getStrColor(){
switch (mColor){
case red:return "red";
case white:return "white";
}
}
template <typename T>
class Circle : public Color{
public:
Circle(T radius, eColor color);
T area();
protected:
T radius;//半径r
};
template <typename T>
Circle<T>::Circle(T _radius, eColor _color) : Color(_color){
radius = _radius;
}
template <typename T>
T Circle<T>::area(){
return 3.14 * radius * radius;
}
template <typename T>
class Sphere : public Circle<T>{
public:
Sphere(T radius, eColor color);
T volume();
};
template <typename T>
Sphere<T>::Sphere(T _radius, eColor _color) : Circle<T>::Circle(_radius, _color){}
template <typename T>
T Sphere<T>::volume(){
return 4 * 3.14 * this->radius * this->radius * this->radius / 3;
}
int main(int argc, char* argv[])
{
Sphere<double> sphereA(10.0, red);
cout << "Volume: " << sphereA.volume() << endl;
cout << "Area: " << sphereA.area() << endl;
cout << "Color: " << sphereA.getStrColor() << endl;
}