We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C++
- Other Concepts
- C++ Class Templates
- Discussions
C++ Class Templates
C++ Class Templates
Sort by
recency
|
242 Discussions
|
Please Login in order to post a comment
My solution for C++14 as it gives time exceed due to stupidly using endl everytime (don't know why it does not happen to C++11)
you are required to use class templates to create a class that can work with different data types. The task demonstrates how templates help achieve generic programming in C++. Khelo24bet Register
Answer
include
include
using namespace std; template class AddElements { T element; public: AddElements(T arg) : element(arg) {} T add(T arg) { return element + arg; } }; template <> class AddElements { string element; public: AddElements(const string &arg) : element(arg) {} string concatenate(const string &arg) { return element + arg; } };
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n;
}