How to use template class with two generic data types in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

template <class Type1, class Type2> 
class test {
	Type1 a;
	Type2 b;
public:
	test(Type1 _a, Type2 _b) { a = _a; b = _b; }
	void print() { cout << a << ' ' << b << endl; }
};

int main()
{
	test<double, int> o1(3.14, 998);
	test<char, char *> o2('z', "c++");
	test<int, long> o3(222, 8888889);
	test<int, int> o4(5, 10);

	o1.print();
	o2.print();
	o3.print();
	o4.print();

	return 0;
}


/*
run:

3.14 998
z c++
222 8888889
5 10

*/

 



answered Mar 16, 2018 by avibootz
edited Aug 2, 2018 by avibootz

Related questions

1 answer 229 views
1 answer 183 views
1 answer 231 views
2 answers 257 views
1 answer 213 views
1 answer 181 views
...