fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. //---------------------------------------------------------
  5.  
  6. class Logger {
  7. public:
  8. void write(int x);
  9. void write(double x);
  10.  
  11. template <typename T>
  12. void writeLine(T x);
  13. };
  14.  
  15. void Logger::write(int x) {
  16. std::cout << x;
  17. }
  18.  
  19. void Logger::write(double x) {
  20. std::cout << std::setprecision(3) << x;
  21. }
  22.  
  23. template <typename T>
  24. void Logger::writeLine(T x) {
  25. write(x);
  26. std::cout << std::endl;
  27. }
  28.  
  29. //---------------------------------------------------------
  30.  
  31. class FooType1 {};
  32. class FooType2 {};
  33.  
  34. class FooLogger : public Logger {
  35. public:
  36. using Logger::write;
  37.  
  38. void write(FooType1 x);
  39. void write(FooType2 x);
  40. };
  41.  
  42.  
  43. void FooLogger::write(FooType1 x) {
  44. std::cout << "<value of type FooType1>";
  45. }
  46. void FooLogger::write(FooType2 x) {
  47. std::cout << "<value of type FooType2>";
  48. }
  49.  
  50. //---------------------------------------------------------
  51.  
  52. int main(int argc, char **argv) {
  53. FooLogger log;
  54. FooType1 foo1;
  55. FooType2 foo2;
  56.  
  57. log.write(42); // OK
  58. log.write(3.14159); // OK
  59. log.write(foo1); // OK
  60. log.write(foo2); // OK
  61.  
  62. std::cout << std::endl;
  63.  
  64. log.writeLine(42); // OK
  65. log.writeLine(3.14159); // OK
  66. // log.writeLine(foo1); // error: no matching member function for call to 'write'
  67. // log.writeLine(foo2); // error: no matching member function for call to 'write'
  68. }
  69.  
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
423.14<value of type FooType1><value of type FooType2>
42
3.14