User Tools

Site Tools


c_-_c_templates:template_parameters

C - C++ Templates - Template Parameters

#include <iostream.h>
#include <math.h>
 
template <class T>
 
class Rect
{  
  T x1,x2,y1,y2;
public:
  Rect(T a,T b,T c,T d)
  {
    x1=a;
    y1=b;
    x2=c;
    y2=d;
  }
  T Area();
  T Distance();
};
 
 
template <class T>
T Rect<T>::Area()
{      return (x1-x2)*(y1-y2);}
 
 
template <class T>
T Rect<T>::Distance()
{ 
  return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
 
 
main()
{
  Rect<int> p1(2.5,3.5,5.5,7);
  cout <<p1.Area()<<endl;
  cout <<p1.Distance()<<endl;
  Rect <float> p2(2.5,3.5,5.5,7.0);
  cout <<p2.Area()<<endl;
  cout <<p2.Distance()<<endl;
  return 0;
}
c_-_c_templates/template_parameters.txt · Last modified: 2020/07/15 10:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki