zoukankan      html  css  js  c++  java
  • 第2章 C++模板技术

    /*
    
    第2章 C++模板技术
       2.1 函数模板
       2.2 类模板
       2.3 模板完全特化
       2.4 函数模板重载
       2.5 类模板继承
       2.6 本章小结
    
    
    */
    
    
    //第2章 C++模板技术
    //   2.1 函数模板 ----------------------------------------------------------------------------------------------------
    
    // p 26
    #include <stdio.h>
    template <class T> T max(T a, T b)
    {
        return a> b ? a : b;
    }
    
    int main(void)
    {
      printf("%d
    ", max < int > (3, 10));
      printf("%f
    ", max < double > (16.9, 2.8));
      return 0;
    }
    
    
    // 27
    #include <stdio.h>
    template <class T1, class T2> T1 max(T1 t1, T2 t2)
    {
        return static_cast<T1> (t1> t2 ? t1 : t2);
    }
    int main(void)
    {
      printf("%f
    ", max(10.9, 3));
      return 0;
    }
    
    
    //   2.2 类模板 ----------------------------------------------------------------------------------------------------
    
    // p 28 , my test
    #include <cstdio>
    #include <typeinfo>
    
    int main(void){
      int i(3);
      printf("%s
    ",typeid(i).name());
      return 0;
    }
    
    
    // 28
    #include <stdio.h>
    #include <typeinfo.h>
    template <class T1, class T2> class A
    {
        T1 i;
        T2 j;
      public:
        A(T1 t1, T2 t2)
        {
            i = t1;
            j = t2;
        }
        bool comp()
        {
            return i > j;
        }
        void print_Type();
    };
    template <class T1, class T2> void A<T1, T2>::print_Type()
    {
        printf("i的类型是%s
    ", typeid(i).name());
        printf("j的类型是%s
    ", typeid(j).name());
    }
    int main(void)
    {
      A < int, double > a(3, 67.8);
      if(a.comp())
        printf("i>j 
    ");
      else
        printf("i<=j 
    ");
      a.print_Type();
      return 0;
    }
    
    
    //   2.3 模板完全特化 ----------------------------------------------------------------------------------------------------
    
    // 28
    #include <stdio.h>
    template <class T> void func(T a)
    {
        printf("hello
    ");
    }
    //函数模板完全特化
    template <> void func<int> (int a)
    {
      printf("hello there
    ");
    }
    int main(void)
    {
      func(2); //打印hello there
      func('y'); //打印hello
      return 0;
    }
    
    
    
    // 29
    #include <stdio.h>
    //类模板A
    template <class T> class A
    {
        T i;
      public:
        A(T t)
        {
            i = t;
        }
        T compute()
        {
            return i *i;
        }
    };
    //类模板A的完全特化
    template <> class A<int>
    {
        int i;
        int k; //添加新数据成员
      public:
        A(int t)
        {
            i = t;
            printf("hello
    ");
        }  //添加打印
        int compute()
        {
            return i *i * i;
        } //改为立方计算
        void f(){}
        //添加新成员函数
    };
    
    int main(void)
    {
      A < double > dObj(2.5);
      A < int > iObj(5); //打印hello
      printf("%f
    ", dObj.compute()); //平方计算6.25
      printf("%d
    ", iObj.compute()); //立方计算125
      return 0;
    }
    
    
    
    //   2.4 函数模板重载 ----------------------------------------------------------------------------------------------------
    
    // 30
    #include <stdio.h>
    template <class T> void func(T a)
    {
        printf("使用func(T a)模板
    ");
    }
    //函数模板的重载
    template <class T1, class T2> int func(T1 t1, T2 t2)
    {
        printf("使用func(T1 t1, T2 t2)模板
    ");
        return 1;
    }
    int main(void)
    {
      func(30, 60); //使用重载的函数模板
      func(19);
      return 0;
    }
    
    
    
    //   2.5 类模板继承 ----------------------------------------------------------------------------------------------------
    
    // 30-31
    #include <stdio.h>
    
    template <class T> class A
    {
      public:
        void func(T a)
        {
            printf("使用func(T a)成员函数模板
    ");
        }
    };
    template <class T1, class T2> class B: public A<T1>
    {
      public:
        void func(T1 t1, T2 t2)
        {
            printf("使用func(T1 t1, T2 t2)成员函数模板
    ");
        }
    };
    
    int main(void)
    {
      B < int, double > b;
      b.func(30, 60);
      A < int > a = static_cast < A < int >  > (b);
      a.func(10);
      return 0;
    }
    
    //   2.6 本章小结 ----------------------------------------------------------------------------------------------------

    TOP

  • 相关阅读:
    asp.net获取当前页面的url地址
    取多个name值相同的input里面的值
    多线程实践
    《谷物大脑》是骗子写的伪科学书:樊登著作4本,都是3星
    历史远未终结,全球化面临挑战:4星|《世界不是平的》
    大众汽车的恐吓文化导致了排放门:4星|《像职场赢家一样做减法》
    穿越回秦朝能发电吗?能:4星|《1分钟物理》
    作者没有实战经验,案例老旧,图表水平差:2星|《社群思维》
    滴滴优步面临各地竞争对手难以通吃:4星|《哈佛商业评论》第3期
    2星|《重新定义物流》:形式像PPT,内容像公关稿
  • 原文地址:https://www.cnblogs.com/xin-le/p/4108319.html
Copyright © 2011-2022 走看看