1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

#include <iostream>
#include <functional>
#include <cmath>
class generic_dis {
public:
virtual double operator() (const std::pair<int,int>& p1,const std::pair<int,int>& p2) const =0;
};
class manhattan_dis : public generic_dis {
public:
double operator() (const std::pair<int,int>& p1,const std::pair<int,int>& p2) const {
return std::abs(p1.first  p2.first) + std::abs(p1.second  p2.second);
}
};
class pythagoral_dis : public generic_dis {
public:
double operator() (const std::pair<int,int>& p1,const std::pair<int,int>& p2) const {
return std::sqrt(double(sqr(p1.firstp2.first)+sqr(p1.second  p2.second)));
}
protected:
template <typename T>
T sqr(const T& v) const {
return v * v;
}
};
template <typename distance_func>
class Student {
public:
Student(std::pair<int,int> _p1,std::pair<int,int> _p2,const distance_func& _pdis) : p1(_p1), p2(_p2), point_distance(_pdis) {
}
std::pair<int,int> p1;
std::pair<int,int> p2;
double calc_distance() {
return point_distance(p1,p2);
}
distance_func point_distance;
};
int main() {
Student<manhattan_dis> S(std::make_pair(0,0),std::make_pair(1,1),manhattan_dis());
S.point_distance = pythagoral_dis();
std::cout << S.calc_distance() <<std::endl;
system("pause");
}
 