zoukankan      html  css  js  c++  java
  • 类模板

    1.类模板的定义和使用

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 //类模板
     6 template <typename T>
     7 class Teacher
     8 {
     9 public:
    10     Teacher(T a):Age(a){}
    11     void printT(){cout<<Age<<endl;}
    12 private:
    13     T Age;
    14 };
    15 
    16 
    17 int main()
    18 {
    19     //模板类(本身就是类型化的)===>需要指明具体的类===>定义具体的变量
    20     Teacher<int> Zhang(25);
    21     Zhang.printT();
    22 
    23 
    24     return 0;
    25 }

    2.类模板做函数参数

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 //类模板
     6 template <typename T>
     7 class Teacher
     8 {
     9 public:
    10     Teacher(T a):Age(a){}
    11     void printT(){cout<<Age<<endl;}
    12 private:
    13     T Age;
    14 };
    15 
    16 //类模板做函数参数
    17 void fun(Teacher<int>& t)
    18 {
    19     t.printT();
    20 }
    21 
    22 int main()
    23 {
    24     //模板类(本身就是类型化的)===>需要指明具体的类===>定义具体的变量
    25     Teacher<int> Zhang(25);
    26     
    27     fun(Zhang);
    28 
    29     return 0;
    30 }

    3.从模板类派生出一个普通类

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 //类模板
     6 template <typename T>
     7 class Teacher
     8 {
     9 public:
    10     Teacher(T a):Age(a){}
    11     void printT(){cout<<Age<<endl;}
    12 private:
    13     T Age;
    14 };
    15 
    16 //模板类派生出一个子类。如果说类的对象的抽象,则模板类是类的抽象,在继承时,需要把模板类具体化(指定模板类的类型)
    17 class Student:public Teacher<int>
    18 {
    19 public:
    20     Student(int a,int s):Teacher(a),Score(s){}
    21     void PrintS(){printT();cout<<Score<<endl;}
    22 
    23 private:
    24     int Score;
    25 };
    26 
    27 int main()
    28 {
    29     //模板类(本身就是类型化的)===>需要指明具体的类===>定义具体的变量
    30     Student Li(98,45);
    31     Li.PrintS();
    32     
    33     return 0;
    34 }

    4.从模板类派生出一个模板子类

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 //类模板
     6 template <typename T>
     7 class Teacher
     8 {
     9 public:
    10     Teacher(T a):Age(a){}
    11     void printT(){cout<<Age<<endl;}
    12 private:
    13     T Age;
    14 };
    15 
    16 //模板类派生出一个模板子类。如果说类的对象的抽象,则模板类是类的抽象,在继承时,需要把模板类具体化(指定模板类的类型)
    17 template <typename T1,typename T2>
    18 class Student:public Teacher<T1>
    19 {
    20 public:
    21     Student(T1 a,T2 s):Teacher(a),Score(s){}
    22     void PrintS(){printT();cout<<Score<<endl;}
    23 
    24 private:
    25     T2 Score;
    26 };
    27 
    28 int main()
    29 {
    30     //模板类(本身就是类型化的)===>需要指明具体的类===>定义具体的变量
    31     Student<int,float> Li(45,98.5);
    32     Li.PrintS();
    33     
    34     return 0;
    35 }
  • 相关阅读:
    openfire 部署后报错: java.lang.IllegalArgumentException: interface xx is not visible from class loader
    Calendar
    list 移除值
    fastjson 返回json字符串,JSON.parse 报错
    tomcat 跨域
    spring boot 笔记
    Mybatis处理列名—字段名映射— 驼峰式命名映射
    hadoop的NullWritable
    CentOS中用Nexus搭建maven私服,为Hadoop编译提供本地镜像
    CentOS中配置xrdp,通过微软远程桌面访问CentOS桌面
  • 原文地址:https://www.cnblogs.com/jswu-ustc/p/8512593.html
Copyright © 2011-2022 走看看