Created
September 22, 2013 13:30
-
-
Save praveendhinwa/6659912 to your computer and use it in GitHub Desktop.
template_specialization.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <bits/stdc++.h> | |
| using namespace std; | |
| template <class T> | |
| class SquareIt | |
| { | |
| public: | |
| T x; | |
| SquareIt () | |
| { | |
| } | |
| SquareIt (T _x) | |
| { | |
| x = _x; | |
| } | |
| T getResult () | |
| { | |
| return x * x; | |
| } | |
| }; | |
| template <> | |
| class SquareIt <string> | |
| { | |
| public: | |
| string x; | |
| SquareIt () | |
| { | |
| } | |
| SquareIt (string _x) | |
| { | |
| x = _x; | |
| } | |
| string getResult () | |
| { | |
| return x + x; | |
| } | |
| }; | |
| int main() | |
| { | |
| SquareIt <int> sq1 (5); | |
| cout << sq1.getResult() << endl; | |
| SquareIt <string> sq2 ("abc"); | |
| cout << sq2.getResult() << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment