c++ template | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

c++ template

You are given a Div class template, which has a constructor that takes two parameters and outputs their division. You need to specialize the class for strings, which should output the division of the lengths of the parameter strings, as the division operator is not defined for strings. Create the template specialization so that the code in main executes correctly. attempt in comments

2nd Dec 2021, 2:57 PM
Mando
Mando - avatar
1 Answer
+ 1
#include <iostream> using namespace std; template <class T> class Div { public: Div (T x, T y) { cout <<x / y<<endl; } }; template < > class MyClass<char> { public: MyClass (char x) { cout << x <<endl; } }; int main () { string a, b; cin >> a >> b; int x, y; cin >> x >> y; Div <string> d2(a, b); Div <int> d1(x, y); }
2nd Dec 2021, 2:58 PM
Mando
Mando - avatar