私と一緒にtemplateテンプレートモードを徹底的に理解します


#include 
using namespace std;
//template  。

class Base
{
public:
    void DealWhat()
    {
        this->Printf1();
        this->Printf2();
    }
protected:
    virtual void Printf1() = 0;
    virtual void Printf2() = 0;//            。
};

class Son1 : public Base
{
public:
    void Printf1()
    {
        cout << "Son1::Printf1()" << endl;
    }
    void Printf2()
    {
        cout << "Son1::Printf2()" << endl;
    }
};

class Son2 : public Base
{
public:
    void Printf1()
    {
        cout << "Son2::Printf1()" << endl;
    }
    void Printf2()
    {
        cout << "Son2::Printf2()" << endl;
    }
};

int main()
{
    Base *b = new Son2();
    b->DealWhat();
    return 0;
}

悟り:ベースクラスの方法は異なるサブクラスによって具体的に実現され、私たちはどんな具体的なアルゴリズムやアルゴリズムが必要で、直接関連するサブクラスを構築すれば使用することができるが、共通のインタフェースはベースクラスに定義されており、このようにすべてのサブクラスがC++マルチステートメカニズムを利用して上書き書き換えを行うことができる.以下は私の具体的な事例の詳細です.
#include 
using namespace std;
//template  。
class Base
{
public:
    Base(int a[],int n)
    {
        data = new int[n];
        int i = 0;
        for (; i < n; i++)
        {
            data[i] = a[i];
        }
        size = n;
    }
    void Printf()
    {
        int i = 0;
        for (; i < size; i++)
        {
            cout << data[i] << " ";
        }
        cout << endl;
    }
    void DoWhat()
    {
        this->sort();
        //                 ,            。
    }
protected:
    virtual void sort() = 0;//       。
    int *data;
    int size;
};
class Bubble : public Base//    。
{
public:
    Bubble(int a[], int n) :Base(a, n){}
    void sort()
    {
        int i = 0;
        int temp;
        for (; i < size; i++)
        {
            for (int j = 0; j1); j++)
            {
                if (data[j+1]1];
                    data[j+1] = temp;
                }
            }
        }
    }
};
class Insert : public Base//    。
{
public:
    Insert(int a[], int n) :Base(a, n){}
    void sort()
    {
        int i = 0;
        int j;
        int k;
        int temp;
        for (; i < size; i++)
        {
            j = i;
            temp = data[i];
            for (k = 0; k <= i; k++)
            {
                if (data[k]>temp)
                {
                    break;
                }
            }
            for (; j > k; j--)
            {
                data[j] = data[j - 1];
            }
            data[j] = temp;
        }
    }
};
class Qulick : public Base//    。
{
public:
    Qulick(int a[], int n) :Base(a, n){}
    void sort()
    {
        sort(data,0,size);
    }
private:
    void sort(int a[], int low, int high)
    {
        int i = -1;
        int j = 0;
        if ( low>=high )return;
        int key = data[high-1];
        int temp;
        while (jwhile (data[j]>key)j++;
            i++;
            temp = data[i];
            data[i] = data[j];
            data[j] = temp;
            j++;
        }
        data[i] = key;
        sort(a,0,i-1);
        sort(a,i+1,high);
    }
};
class Select : public Base//    。
{
public:
    Select(int a[], int n) :Base(a, n){}
    void sort()
    {
        int i = 0;
        int j;
        int temp;
        for (; i < size - 1; i++)
        {
            for (j = i + 1; j < size; j++)
            {
                if (data[i]>data[j])
                {
                    temp = data[i];
                    data[i] = data[j];
                    data[j] = temp;
                }
            }
        }
    }
};

int main()
{
    int a[] = { 4, 6, 7, 9, 1, 23, 4 };
    /*Base *p = new Qulick(a, 7);
    p->DoWhat();
    p->Printf();//    。
    */
    Base *p = new Bubble(a,7);
    p->DoWhat();
    p->Printf();//    。

    return 0;
}