Created
September 22, 2013 11:30
-
-
Save praveendhinwa/6659078 to your computer and use it in GitHub Desktop.
class_template.cpp: Using templates for classes in C++
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 point | |
| { | |
| public : | |
| T x, y; | |
| point () | |
| { | |
| } | |
| point (T _x, T _y) | |
| { | |
| x = _x; | |
| y = _y; | |
| } | |
| T dist () | |
| { | |
| return x * x + y * y; | |
| } | |
| }; | |
| int main() | |
| { | |
| point <int> p (3, 4); | |
| cout << p.dist() << endl; // 25 | |
| point <double> q (3.5, 4.5); | |
| cout << q.dist() << endl; // 32.5 | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment