C++入门经典-例9.5-为具体类型的参数提供默认值

1:默认模板参数是指类模板中由默认的数据类型作为参数的参数,在模板定义时,还可以为默认的数据类型声明,变量,并为变量赋值。代码如下:

// 9.5.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class T1,class T2,int num= 10 >
class MyTemplate
{
    T1 t1;
    T2 t2;
    public:
        MyTemplate(T1 tt1,T2 tt2)
        {t1 =tt1+num, t2=tt2+num;}
        void display()
        { cout << t1 << ' ' << t2 <<endl;}
};
void main()
{
    int a=123;
    double b=3.1415;
    MyTemplate<int ,double> mt1(a,b);
    MyTemplate<int ,double ,100> mt2(a,b);
    mt1.display();
    mt2.display();
}
View Code

运行结果:

posted @ 2017-09-22 21:44  一串字符串  阅读(373)  评论(0编辑  收藏  举报