metaprogramming/conditional-declaration/point.cc - Implementation of Point<C> members. | Lecture 6 - slide 36 : 40 Program 2 |
// point.cc in which move is only present if C is a floating point type. #include <cmath> #include <iostream> #include "point.h" template<typename C> Point<C>::Point(C x_coord, C y_coord): x(x_coord), y(y_coord){ } template<typename C> Point<C>::Point(): x(0.0), y(0.0){ } template<typename C> C Point<C>::getx () const{ return x; } template<typename C> C Point<C>::gety () const{ return y; } // if C is not a floating point type, the move function is not present. template<typename C> Enable_if<std::is_floating_point<C>::value, Point<C>>& Point<C>::move(C dx, C dy){ x += dx; y += dy; return *this; } template<typename C> double Point<C>::distance_to(Point<C> p){ return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); // Similar assumptions for - and * } template<typename C> std::ostream& operator<< (std::ostream& s, const Point<C>& p){ return s << "(" << p.getx() << "," << p.gety() << ")" ; }