c++ 泛型编程

link

#include <iostream>

using namespace std;

template < typename T >
void Swap(T& a, T& b)
{
    T t = a;
    a = b;
    b = t;
}

template < typename T >
class Op
{
public:
    T process(T v)
    {
        return v * v;
    }
};

int main()
{
    int a = 1;
    int b = 2;

    Swap(a, b);

    cout << "a = " << a << ", " << "b = " << b << endl;

    double c = 0.01;
    double d = 0.02;

    //Swap<double>(d, c);
    Swap<double>(d, c);

    cout << "c = " << c << ", " << "d = " << d << endl;

    Op<int> opInt;
    Op<double> opDouble;

    cout << "5 * 5 = " << opInt.process(5) << endl;
    cout << "0.5 * 0.5 = " << opDouble.process(0.5) << endl;

    return 0;

}

posted @ 2022-08-19 22:44  luoganttcc  阅读(4)  评论(0编辑  收藏  举报