美文网首页
C++ 函数模板、类模板

C++ 函数模板、类模板

作者: gaookey | 来源:发表于2020-08-28 10:53 被阅读0次

    函数模板

    #include <iostream>
    using namespace std;
    
    template <class T>
    T abs(T x) {
        return x < 0 ? -x : x;
    }
    // ---
    //多个参数相同
    template <typename T>
    void Swap(T &x, T &y) {
        T tmp = x;
        x = y;
        y = tmp;
    }
    
    class myDate {
    public:
        myDate();
        myDate(int month, int day);
        void printDate() const;
    private:
        int month, day;
    };
    myDate::myDate() {
        month = 5;
        day = 20;
    }
    myDate::myDate(int m, int d) {
        month = m;
        day = d;
    }
    void myDate::printDate() const {
        cout<<month<<" "<<day<<endl;
    }
    // ---
    //多个参数不同
    template <class T1, class T2>
    void print(T1 m, T2 n) {
        cout<<m<<" "<<n<<endl;
    }
    
    int main(int argc, const char * argv[]) {
        
        cout<<abs(-5.00324)<<endl;
        
        int m = 1, n = 2;
        Swap(m, n);
        cout<<m<<"-"<<n<<endl;
        
        myDate d1, d2(6, 30);
        swap(d1, d2);
        d1.printDate();
        d2.printDate();
        
        print<int, double>(5, 5.8198);
        
        return 0;
    }
    

    类模板

    #include <iostream>
    using namespace std;
    
    template <typename T>
    class TestClass {
    public:
        T buffer[11];
        T getData(int j);
    };
    template <typename T>
    T TestClass<T>::getData(int j) {
        return *(buffer + j);
    };
    
    // ---
    template <int i>
    class TestClass2 {
    public:
        int buffer[11];
        int getData(int j);
    };
    template <int i>
    int TestClass2<i>::getData(int j) {
        return *(buffer + j);
    }
    
    // --- 继承
    template <typename T>
    class Tbase {
        T data;
    public:
        void print() { }
    };
    class Derived: public Tbase<int> { };
    
    class Tbase2 {
        int i;
    public:
        void print() { cout<<i<<endl; }
    };
    template <typename T>
    class Derived2: public Tbase2 {
        T data;
    public:
        void setData(T x) {
            data = x;
        }
        void print() {
            Tbase2::print();
            cout<<data<<endl;
        }
    };
    
    template <class T>
    class Father {
        T data;
    public:
        void print() { cout<<data<<endl; }
    };
    template <class T1, class T2>
    class Son: Father<T1> {
        T2 data2;
    public:
        void print() { cout<<data2<<endl; }
    };
    
    int main(int argc, const char * argv[]) {
        
        TestClass<char> c;
        char cArr[6] = "abcd";
        for (int i = 0; i < 5; i ++) {
            c.buffer[i] = cArr[i];
        }
        for (int i = 0; i < 5; i ++) {
            char res = c.getData(i);
            cout<<res<<" ";
        }
        cout<<endl;
        
        TestClass<double> d;
        double dArr[6] = {12.35, 23.23, 56.8, 26.9, 62.6, 21.5};
        for (int i = 0; i < 6; i ++) {
            d.buffer[i] = dArr[i] - 10;
        }
        for (int i = 0; i < 6; i ++) {
            double res = d.getData(i);
            cout<<res<<" ";
        }
        cout<<endl;
        
        TestClass2<7> t;
        double dArr2[7] = {12.35, 23.23, 56.8, 26.9, 62.6, 21.5, 123.13};
        for (int i = 0; i < 7; i ++) {
            t.buffer[i] = dArr2[i] - 10;
        }
        for (int i = 0; i < 7; i ++) {
            double res = t.getData(i);
            cout<<res<<" ";
        }
        cout<<endl;
        
        Derived2<string> d2;
        d2.setData("2020");
        d2.print();
        
        Son<int, int> s;
        s.print();
        Son<string, string> s2;
        s2.print();
        
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C++ 函数模板、类模板

          本文链接:https://www.haomeiwen.com/subject/ykzksktx.html