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,948 questions

51,890 answers

573 users

How to use generic data types in a class with C++

1 Answer

0 votes
#include <iostream>

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

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

int main()
{
	Class<int, double> o1(123, 3.14);
	Class<long, float> o2(93938L, 2.45f);
	Class<char, char *> o3('a', "c++ programming");

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

	return 0;
}


/*
run:

123 3.14
93938 2.45
a c++ programming

*/

 



answered Aug 2, 2018 by avibootz

Related questions

1 answer 203 views
1 answer 169 views
1 answer 150 views
2 answers 222 views
1 answer 197 views
1 answer 190 views
...