fork download
  1. #include <iostream>
  2.  
  3. class TextInput
  4. {
  5. public:
  6. std::string value;
  7. virtual void add(char c) { value += c; }
  8. std::string getValue() { return value; }
  9. };
  10.  
  11. class NumericInput : public TextInput
  12. {
  13. public:
  14. void add(char c)
  15. {
  16. if (isdigit(c)) { TextInput::add(c); }
  17. }
  18.  
  19. // You already get this for free from the inheritance
  20. // std::string getValue()
  21. // {
  22. // return value;
  23. // }
  24. };
  25.  
  26. int main()
  27. {
  28. TextInput* input = new NumericInput();
  29. input->add('1');
  30. input->add('a');
  31. input->add('0');
  32. std::cout << input->getValue();
  33. }
Success #stdin #stdout 0s 4864KB
stdin
Standard input is empty
stdout
10