You are viewing a single comment's thread. Return to all comments →
using namespace std;
template 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() { std::ios_base::sync_with_stdio(false); int n ; cin >> n ; for(int i =0 ; i < n ; i++) {
string select_type ; cin >> select_type ; if (select_type == "string") { string e1 ,e2 ; cin >> e1 >> e2 ; MyTemplate<string> m1(e1); cout << m1.concatenate(e2) << "\n"; } else if ( select_type == "int" ){ int e1,e2 ; cin >> e1 >> e2 ; MyTemplate<int> m1(e1); cout << m1.add(e2) << "\n"; } else if ( select_type == "float" ) { double e1,e2 ; cin >> e1 >> e2 ; MyTemplate<double> m1(e1); cout << m1.add(e2) << "\n"; } } 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 →
include
include
include
include
include
using namespace std;
template class MyTemplate { T element1;
public: MyTemplate(T num) : element1(num) {}
};
int main() { std::ios_base::sync_with_stdio(false); int n ; cin >> n ; for(int i =0 ; i < n ; i++) {
}