fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class T>
  5. class Exponentiation
  6. {
  7. public:
  8. Exponentiation() {};
  9. virtual ~Exponentiation() {};
  10.  
  11. // computes C = A^n
  12. virtual void power() = 0;
  13. };
  14.  
  15. template <class T>
  16. class ExpA : public Exponentiation<T>
  17. {
  18. public:
  19. ExpA() {};
  20. ~ExpA() {};
  21.  
  22. void power () override { cout<<"power A\n"; }
  23. };
  24.  
  25. template <class T>
  26. class ExpB : public Exponentiation<T>
  27. {
  28. protected:
  29. //var1;
  30. //var2;
  31.  
  32. public:
  33. ExpB() {};
  34. ~ExpB() {};
  35.  
  36. void func1(){ cout<<"f1\n"; }
  37. void func2(){ cout<<"f2\n"; }
  38. void power () override { cout<<"power B\n"; };
  39. };
  40.  
  41. template <class T, template <class U> class Exp>
  42. void performExp(Exp<T>& obj)
  43. {
  44. obj.power();
  45. }
  46. template <class T>
  47. void performExp(ExpB<T>& obj)
  48. {
  49. obj.func1();
  50. obj.func2();
  51. obj.power();
  52. }
  53.  
  54. int main() {
  55. ExpA<long> a;
  56. performExp(a);
  57.  
  58. ExpB<long> b;
  59. performExp(b);
  60. // your code goes here
  61. return 0;
  62. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
power A
f1
f2
power B