A File Filter Reads An Input File
A File Filter Reads An Input File
Write an abstract file filter class that defines a pure virtual function for transforming a character.
Create one derived class 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 the following member function: void doFilter(ifstream &in, 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) The encryption class should have a
constructor that takes an integer as an argument and uses it as the encryption key.
Answer
//contents of FileFilter.h
#ifndef FILEFILTER_H
#define FILEFILTER_H
#include <iostream>
#include <fstream>
class FileFilter{
public:
//function doFilter
//directly
while(in.get(ch)){
up = transform(ch);
out.put(up);
//transform function
};
#endif
2
//contents of Upp.h
#ifndef UPP_H
#define UPP_H
#include <iostream>
public:
return toupper(ch);
};
#endif
3
//contents of Encrypt.h
#ifndef ENCRYPT_H
#define ENCRYPT_H
#include <iostream>
private:
int key;
public:
//default constructor
Encrypt(){
key = 10;
//overloaded constructor
Encrypt(int k){
key = k;
return ch + key;
};
#endif
4
//contents of CopyFile.h
#ifndef COPYFILE_H
#define COPYFILE_H
#include <iostream>
public:
return ch;
};
#endif
5
//contents of main.cpp
#include <iostream>
#include <fstream>
#include "FileFilter.h"
#include "Upp.h"
#include "Encrypt.h"
#include "CopyFile.h"
int main()
{
//define an ifstream object to read contents
//file filter
ofstream outputFile("UppercaseOutputFile.txt");
//filtration
Upp upperCaseFilter;
upperCaseFilter.doFilter(inputFile, outputFile);
outputFile.close();
inputFile.clear();
inputFile.seekg(0L, ios::beg);
//encryption filter
outputFile.open("EncryptionOutputFile.txt");
Encrypt encryptionFilter ;
encryptionFilter.doFilter(inputFile, outputFile);
outputFile.close();
inputFile.seekg(0L, ios::beg);
outputFile.open("CopyOutputFile.txt");
CopyFile copyFilter;
copyFilter.doFilter(inputFile, outputFile);
inputFile.close();
outputFile.close();
return 0;