-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-input.cpp
More file actions
74 lines (67 loc) · 1.37 KB
/
user-input.cpp
File metadata and controls
74 lines (67 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @file user-input.cpp
* @author nirmeet baweja
* @brief User interface contains two types of user input controls: TextInput,
* which accepts all characters and NumericInput, which accepts only digits
*
* Implement the following methods:
* add on class TextInput - adds the given character to the current value
* getValue on class TextInput - returns the current value add on class
* NumericInput - overrides the base class method so that each non-numeric
* character is ignored
*
* For example, the following code should output "10":
* TextInput* input = new NumericInput();
* input->add('1');
* input->add('a');
* input->add('0');
* std::cout << input->getValue();
*
* @version 0.1
* @date 2022-02-13
*
* @copyright Copyright (c) 2022
*
*/
#include <iostream>
#include <string>
class TextInput
{
private:
std::string input_variable;
public:
TextInput()
{
input_variable = "";
}
virtual void add(char c)
{
this->input_variable.push_back(c);
}
std::string getValue()
{
return input_variable;
}
};
class NumericInput : public TextInput
{
public:
void add(char) override;
};
void NumericInput::add(char c)
{
if (c >= '0' && c <= '9')
{
TextInput::add(c);
}
}
#ifndef RunTests
int main()
{
TextInput *input = new NumericInput();
input->add('1');
input->add('a');
input->add('0');
std::cout << input->getValue();
}
#endif