Skip to content

Commit 4c11a4a

Browse files
solves cpp class templates
1 parent b4ec369 commit 4c11a4a

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ The C++ Domain is further Divided into the following sub-domains.
8383

8484
| Problem | Difficulty | Solution |
8585
|---------|:----------:|:--------:|
86-
| [C++ Class Templates](https://www.hackerrank.com/challenges/c-class-templates) | Easy | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)]() |
86+
| [C++ Class Templates](https://www.hackerrank.com/challenges/c-class-templates) | Easy | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](other-concepts/cpp-class-templates.cpp) |
8787
| [Preprocessor Solution](https://www.hackerrank.com/challenges/preprocessor-solution) | Easy | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)]() |
8888
| [Operator Overloading](https://www.hackerrank.com/challenges/operator-overloading) | Medium | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)]() |
8989
| [Overload Operators](https://www.hackerrank.com/challenges/overload-operators) | Easy | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)]() |
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <cmath>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <iostream>
5+
using namespace std;
6+
7+
/*Write the class AddElements here*/
8+
9+
template <class T> class AddElements {
10+
public:
11+
T element;
12+
AddElements(T i) {element = i;}
13+
T add(T i) {return element+i;}
14+
private:
15+
16+
};
17+
18+
// the actual solution
19+
template <> class AddElements <string> {
20+
public:
21+
string element;
22+
AddElements(string i) {element = i;}
23+
string concatenate(string element2) {return element+element2;}
24+
private:
25+
26+
};
27+
28+
// boiler plate required because the locked stub fof code by hackerrank is slow
29+
int start_up() {
30+
ios_base::sync_with_stdio(false);
31+
cin.tie(NULL);
32+
return 0;
33+
}
34+
35+
int static r = start_up();
36+
37+
#define endl '\n';
38+
39+
40+
int main () {
41+
int n,i;
42+
cin >> n;
43+
for(i=0;i<n;i++) {
44+
string type;
45+
cin >> type;
46+
if(type=="float") {
47+
double element1,element2;
48+
cin >> element1 >> element2;
49+
AddElements<double> myfloat (element1);
50+
cout << myfloat.add(element2) << endl;
51+
}
52+
else if(type == "int") {
53+
int element1, element2;
54+
cin >> element1 >> element2;
55+
AddElements<int> myint (element1);
56+
cout << myint.add(element2) << endl;
57+
}
58+
else if(type == "string") {
59+
string element1, element2;
60+
cin >> element1 >> element2;
61+
AddElements<string> mystring (element1);
62+
cout << mystring.concatenate(element2) << endl;
63+
}
64+
}
65+
return 0;
66+
}

0 commit comments

Comments
 (0)