C++ : 模版

模版是泛型编程的基础,泛型编程即以一种独立于任何特定类型的方式编写代码。每个容器都有一个单一的定义,可定义不同类型的容器,如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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春夏与冬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值