A file filter reads an input file, transforms it in some way, and writes the res
ID: 3681286 • Letter: A
Question
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 one subclass of your file filter class that performs encryption, another that transforms a file to all uppercase, and another that creates an unchanged copy of the original file. The class should have a member function void doFilter(ifstream &in, ofstream &out) that is called to perform the actual filtering. The member function for transforming a single character should have the prototype char transform(char ch) The encryption class should have a constructor that takes an integer as an argument and uses it as the encryption key.
Answer in c++ please
Explanation / Answer
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
class filefilter
{
virtual char transform(char ch) = 0;
public:
void dofilter(ifstream &in , ofstream &out)
{
char c;
while((c=getchar())!=EOF)
{
c = transform(c);
out<<c;
}
in.close();
out.close();
}
};
class Encryption : public filefilter
{
int key;
public:
Encryption(int a)
{
key= a;
}
char transform(char c)
{
return (c+key);
}
};
class Uppercase : public filefilter
{
public:
char transform(char c)
{
int ch = c-'32';
return ch;
}
};
class Unchanged : public filefilter
{
public:
char transform(char c)
{
return c;
}
};
int main()
{
int k=4;
filefilter *f,*g,*h;
Encryption e(k);
Uppercase up;
Unchanged uc;
f = &e;
g = &up;
h = &uc;
string iFilePath,oFilePath;
std::cout << "Please enter a input file path: ";
std::cin >> iFilePath;
std::cout << "Please enter a output file path: ";
std::cin >> oFilePath;
ifstream in(iFilePath.c_str());
ofstream out(oFilePath.c_str());
f->dofilter(in,out);
g->dofilter(in,out);
h->dofilter(in,out);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.