In: Computer Science
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.
Be sure to include comments throughout your code where appropriate.
Complete the C++ code using Visual Studio or Xcode, compress (zip) and upload the entire project folder to the Blackboard assignment area by clicking on the Browse My Computer button or by dragging the file inside the Attach Files box.
This assignment is worth 20 points and will be evaluated using the CIS 221 Assignment Rubric, which is available from the assignment submission area, as well as in the course Grade Center.
//contents of FileFilter.h #ifndef FILEFILTER_H #define FILEFILTER_H #include <iostream> #include <fstream> using namespace std; class FileFilter{ public: //function doFilter //this is a function that all the derived //classes will call, and we can define it //directly void doFilter(ifstream &in, ofstream &out){ //char variable to store characters read //and the updated one char ch, up; //read characters from file one by one //until no more characters to read, //using a while loop while(in.get(ch)){ //get the transformed character up = transform(ch); out.put(up); } } //the pure virtual function, which will be //overridden in the derived classes, is the //transform function virtual char transform(char ch) = 0; }; #endif
//contents of Upp.h #ifndef UPP_H #define UPP_H #include <iostream> using namespace std; class Upp : public FileFilter{ public: virtual char transform(char ch){ return toupper(ch); } }; #endif
//contents of Encrypt.h #ifndef ENCRYPT_H #define ENCRYPT_H #include <iostream> using namespace std; class Encrypt : public FileFilter{ private: int key; public: //default constructor Encrypt(){ key = 10; } //overloaded constructor Encrypt(int k){ key = k; } //remember that chars are represented //as integers, of their ASCII values virtual char transform(char ch){ return ch + key; } }; #endif
//contents of CopyFile.h #ifndef COPYFILE_H #define COPYFILE_H #include <iostream> using namespace std; class CopyFile : public FileFilter{ public: virtual char transform(char ch){ return ch; } }; #endif
//contents of main.cpp #include <iostream> #include <fstream> #include "FileFilter.h" #include "Upp.h" #include "Encrypt.h" #include "CopyFile.h" using namespace std; int main() { //define an ifstream object to read contents //of input file, and open it directly cout << "Now opening input file for reading...\n"; ifstream inputFile("inputFile.txt"); cout << "Done! \"inputFile.txt\" open for reading!\n"; //define an ofstream object to open //output file, first for the uppercase //file filter cout << "\nOpening output file for uppercase filter...\n"; ofstream outputFile("UppercaseOutputFile.txt"); cout << "Done! \"UppercaseOutputFile.txt\" open!\n"; //create Upp object to perform an uppercase //filtration Upp upperCaseFilter; //perform filtration using the object cout << "\nNow performing uppercase filtration...\n"; upperCaseFilter.doFilter(inputFile, outputFile); cout << "Filtration done! \"UppercaseOutputFile.txt\""; cout << " is ready!\n"; //output file should now be created //and written with relevant output //now close it, because we will open //another one for the next filter outputFile.close(); //clear input file flags and move read position //to the first byte inputFile.clear(); inputFile.seekg(0L, ios::beg); //open the next output file, for the //encryption filter cout << "\nNow opening output file for uppercase filter...\n"; outputFile.open("EncryptionOutputFile.txt"); cout << "Done! \"EncryptionOutputFile.txt\" open!\n"; //create Encrypt object Encrypt encryptionFilter; //perform file filtration cout << "\nNow performing encryption filtration...\n"; encryptionFilter.doFilter(inputFile, outputFile); cout << "Filtration done! \"EncryptionOutputFile.txt\""; cout << " is ready!\n"; //output file should now be created //and written with relevant output //now close it, because we will open //another one for the next filter outputFile.close(); //clear input file flags and move read position //to the first byte inputFile.clear(); inputFile.seekg(0L, ios::beg); //open the next output file, for the //file copy filter cout << "\nNow opening output file for copy filter...\n"; outputFile.open("CopyOutputFile.txt"); cout << "Done! \"CopyOutputFile.txt\" open!\n"; //create CopyFile object CopyFile copyFilter; //perform file filtration cout << "\nNow performing copy filtration...\n"; copyFilter.doFilter(inputFile, outputFile); cout << "Filtration done! \"CopyOutputFile.txt\""; cout << " is ready!\n"; //now close both files inputFile.close(); outputFile.close(); //return 0 to mark successful termination return 0; }
RESULT
note: plzzz don't give dislike.....plzzz comment if you have any problem i will try to solve your problem.....plzzz give thumbs up i am in need....