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 242 views
1 answer 192 views
1 answer 178 views
2 answers 271 views
1 answer 242 views
1 answer 221 views
...