Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

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 191 views
1 answer 168 views
1 answer 193 views
2 answers 219 views
1 answer 188 views
1 answer 160 views
...