You are viewing a single comment's thread. Return to all comments →
#include<bits/stdc++.h> using namespace std; template <class T> class MyTemplate { T element1; public: MyTemplate(T num): element1(num) {} T add(T element2) { return element1 + element2; } T concatenate(T element2) { return element1 + element2; } }; int main() { int n; cin >> n; for(int i = 0; i < n; i++) { string type; cin >> type; if(type == "string") { string e1, e2; cin >> e1 >> e2; MyTemplate<string> mt1(e1); cout << mt1.concatenate(e2) << endl; } else if(type == "int") { int e1, e2; cin >> e1 >> e2; MyTemplate<int> mt2(e1); cout << mt2.add(e2) << endl; } else if(type == "float") { double e1, e2; cin >> e1 >> e2; MyTemplate<double> mt3(e1); cout << mt3.add(e2) << endl; } } return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
C++ Class Templates
You are viewing a single comment's thread. Return to all comments →