Provide a software solution for the following: A file filter reads an input file
ID: 3846405 • Letter: P
Question
Provide a software solution for the following: A file filter reads an input file, transforms it in some way, and writes the results to an output file. Write an abstract file filter class that defines a pure virtual function for transforming a character. Create a derived class of your file filter class that transforms a file to all uppercase. The class should have the following member function: void doFilter (ifstream ∈, ofstream &out;) This function should be called to perform the actual filtering. The member function for transforming a single character should have the prototype: char transform (char ch)Explanation / Answer
#include<iostream>
#include <ctype.h>
#include <fstream>
using namespace std;
// Base class
class Filter
{
public:
virtual void doFilter(ifstream &in, ofstream &out) = 0;
};
// Derived class
class UpperCaseFilter: public Filter
{
public:
void doFilter(ifstream &in, ofstream &out) {
char ch;
while (in >> noskipws >> ch) {
out << transform(ch);
}
}
private:
char transform(char ch) {
return toupper(ch);
}
};
int main(void)
{
Filter *filter = new UpperCaseFilter;
ifstream in("data.txt");
ofstream out("output.txt");
if(!in.is_open() || !out.is_open()) {
cout << "Error in input/output file." << endl;
return 1;
}
filter->doFilter(in, out);
cout << "Done!!" << endl;
return 0;
}
data.txt:
This is the initial data.
It will be changed.
output.txt:
THIS IS THE INITIAL DATA.
IT WILL BE CHANGED.
You need to keep the data in a file named as "data.txt" as shown here. the output will be generated in a file named as "output.txt" in the same directory.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.