Text Filters

Programming Workshop 2 (CSCI 1061U)

Faisal Qureshi

Faculty of Science, UOIT

http://vclab.science.uoit.ca


Introduction

The goal of this lab is to learn the use of abstract classes in C++. Consider the following abstract class Filter.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Filter
{
protected:
  string _name;

public:  
  Filter(const string& name) : _name(name) {}
  virtual ~Filter() {};

  virtual string apply(const string& input) = 0;

  string get_name() { return _name; }
};

This class specifies a generic string filter. Using this class, we have implemented 7 string filters:

Each of these filter is implemented in a child class of the abstract Filter class. We can apply these filters to an arbitrary string as seen below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int num_filters = 7;
Filter* filters[num_filters];

filters[0] = new Capitalize();
filters[1] = new CapitalizeFirstLetter();
filters[2] = new CapitalizeFirstLetterOfEveryWord();
fitlers[3] = new Obenglobish();
filters[4] = new ReverseString();
filters[5] = new ToSmall();
filters[6] = new Randomize();
  
string input;
cout << "Enter srring: ";  getline(cin, input);
  
cout << "Input string: " << input << endl;
for (int i=0; i<num_filters; ++i) {
  cout << "Filter name:  " << filters[i]->get_name() << endl;
  cout << "Output:       " << filters[i]->apply(input) << endl; 
}

for (int i=0; i<num_filters; ++i) delete filters[i];

You are asked to provide code for the 7 classes that implement these filters. The functionality of each filter is clear from its name. Randomize filter randomly move the letter locations in the provided file. Consider the following output from the program.

Enter srring: lab no 8
Input string: lab no 8
Filter name:  Capitalize
Output:       LAB NO 8
Filter name:  CapitalizeFirstLetter
Output:       Lab no 8
Filter name:  CapitalizeFirstLetterOfEveryWord
Output:       Lab No 8
Filter name:  Obenglobish
Output:       lobab no 8
Filter name:  ReverseString
Output:       8 on bal
Filter name:  ToSmall
Output:       lab no 8
Filter name:  Randomize
Output:       alb 8o n

Submit

Submit filters.cpp file via Blackboard.