Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of class template in C++
#include <iostream>
using namespace std;
template <class T>
class Two_values {
T a;
T b;
public:
Two_values (T first_arg, T second_arg) {
a = first_arg;
b = second_arg;
}
void custom_add ();
};
template <class T>
void Two_values<T>::custom_add () {
cout << "Result = " << (a + b) << endl;
}
int main () {
Two_values <int> Two_int_values (1, 2);
Two_values <float> Two_float_values (10.1, 12.2);
Two_values <char> Two_char_values ('a', 'b');
Two_int_values.custom_add();
Two_float_values.custom_add();
Two_char_values.custom_add();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.