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

  • 相关阅读:
    我的2007, 兼谈些对技术的看法
    回帖整理: 关于"学习Java社区"更清晰的思路
    回帖整理: 创业心态
    我的世界观 by 爱因斯坦
    回帖整理: 论团队中的设计工作
    请大家帮我一个忙
    回帖整理: Java社区有什么可学的?
    SSL原理及应用(1)SSL协议体系结构
    文件和目录的访问控制(4) 审核规则
    强名称(2)引用强名称签名的程序集
  • 原文地址:https://www.cnblogs.com/xin-le/p/4108319.html
Copyright © 2011-2022 走看看