templates/functions/point-norms-and-compare/prog.cc - Class Point parameterized with a policy class. | Lecture 5 - slide 37 : 40 Program 1 |
// Ilustration of class point parameterized with different policy types. // A bundle of (related) functions passed to Point (and all members of Point) at compile time. #include <iostream> #include <string> #include "point.cc" #include "norms.cc" template<typename C>void do_tell_about(C x, C y){ double dist = x.distance_to(y); std::cout << "Distance: " << dist << std::endl; std::cout << "Less than? " << (x.less_than(y) ? "true" : "false") << std::endl << std::endl; } int main(){ using namespace std; double dist; Point<FixedNorm> p1(1,2), // Here we parameterize p1 and p2 with a bundle of functions from Fixed form. p2(4,11); do_tell_about(p1,p2); Point<HorizontalNorm> p3(1,2), // Ditto p3 and p4 with HorizontalNorm p4(4,11); do_tell_about(p3,p4); Point<VerticalNorm> p5(1,2), // Ditto p5 and p6 with VerticalNorm p6(4,11); do_tell_about(p5,p6); Point<Norm> p7(1,2), // Ditto p7 and p8 with Norm p8(4,11); do_tell_about(p7,p8); }