Example for default in template

posted Dec 1, 2014, 1:46 AM by Javad Taghia
#include <iostream>

using namespace std;

template <class T, class P = int>
class testDefault
{
public:
T tval;
P pval;
testDefault(T a,P b);
P doAdd();
T doMul(T a = 12  , P b =13 );

};

template<class T, class P>
testDefault<T, P>::testDefault(T a, P b)
{
tval = a;
pval = b;
}

template<class T, class P >
P testDefault<T, P>:: doAdd()
{

return tval + pval;
}

template<class T, class P>
T testDefault<T, P>:: doMul( T a , P b )
{

return  a * b;
}

int main()

{
testDefault<double> obj1 (1.3, 4);
std::cout << obj1.doAdd() << std::endl;
std::cout << obj1.doMul() << std::endl;
std::cout << obj1.doMul (2.2) << std::endl;
std::cout << obj1.doMul (2.2, 2) << endl;
testDefault<double, double> obj11 (1.3, 4);
std::cout << obj11.doAdd() << std::endl;

std::cout << obj11.doMul (2.2, 2) << endl;
getchar();

}

Comments