fork download
  1. #include <iostream>
  2.  
  3. typedef bool (*Require)(int x);
  4. typedef int (*Convert)(int x);
  5.  
  6. class Base {
  7. private:
  8. int _x;
  9. public:
  10. virtual void print() {
  11. std::cout << _x << std::endl;
  12. }
  13. protected:
  14. Base(int x, Require r, Convert c) {
  15. if (r(x))
  16. _x = x;
  17. else
  18. _x = c(x);
  19. }
  20. };
  21.  
  22. class Derived : public Base {
  23. private:
  24. static bool require(int x) {
  25. return x >= 0;
  26. }
  27. static int convert(int x) {
  28. return -1 * x;
  29. }
  30. public:
  31. Derived(int x) : Base(x, require, convert) {}
  32. };
  33.  
  34. int main() {
  35. Derived a(1);
  36. Derived b(-1);
  37. a.print();
  38. b.print();
  39. return 0;
  40. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1
1