Skip to content

Instantly share code, notes, and snippets.

@praveendhinwa
Created September 22, 2013 11:30
Show Gist options
  • Select an option

  • Save praveendhinwa/6659078 to your computer and use it in GitHub Desktop.

Select an option

Save praveendhinwa/6659078 to your computer and use it in GitHub Desktop.
class_template.cpp: Using templates for classes in C++
#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