Question

A file filter reads an input file, transforms it in some way, and writes the results...

A file filter reads an input file, transforms it in some way, and writes the results to an out-put 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

#include<fstream>

using namespace std;

//abstract Filter class

class Filter{

                public:

                //defining pure virtual function

                virtual void doFilter(ifstream &in, ofstream &out)=0;

};

//EncryptionFilter class

class EncryptionFilter: public Filter{

                private:

                //encryption key

                int key;

                public:

                //contstructor taking encryption key

                EncryptionFilter(int k){

                                key=k;

                }

                //method to apply filter

                void doFilter(ifstream &in, ofstream &out){

                                char ch;

                                //reading all characters one by one until eof

                                while(in.get(ch)){

                                               //transforming ch

                                               ch=transform(ch);

                                               //writing to output stream

                                               out<<ch;

                                }

                }

                //method to transform a character

                char transform(char ch){

                                //checking if ch is lower case

                                if(ch>='a' && ch<='z'){

                                               //adding key value

                                               ch+=key;

                                               //wrapping around if ch >'z'

                                               if(ch>'z'){

                                                               int index=ch-'z';

                                                               ch='a'+index;

                                               }

                                }

                                //doing the same for upper case

                                else if(ch>='A' && ch<='Z'){

                                               ch+=key;

                                               if(ch>'Z'){

                                                               int index=ch-'Z';

                                                               ch='A'+index;

                                               }

                                }

                                return ch;

                }

};

//UpperCaseFilter class

class UpperCaseFilter: public Filter{

                private:

                int key;

                public:

                UpperCaseFilter(){

                               

                }

               

                void doFilter(ifstream &in, ofstream &out){

                                char ch;

                                while(in.get(ch)){

                                               ch=transform(ch);

                                               out<<ch;

                                }

                }

                char transform(char ch){

                                //if ch is lower case, converting to upper case

                                if(ch>='a' && ch<='z'){

                                               int index=ch-'a';

                                               ch='A'+index;

                                }

                                return ch;

                }

};

//UnchangedFilter class

class UnchangedFilter: public Filter{

                private:

                int key;

                public:

                UnchangedFilter(){

                               

                }

               

                void doFilter(ifstream &in, ofstream &out){

                                char ch;

                                while(in.get(ch)){

                                               ch=transform(ch);

                                               out<<ch;

                                }

                }

                char transform(char ch){

                                //returning ch with no changes

                                return ch;

                }

};

int main(){

                //creating objects for each Filter

                EncryptionFilter encFilter(3);

                UpperCaseFilter upFilter;

                UnchangedFilter unchangedFilter;

                //asking user to enter an input file name

                cout<<"Enter input file name: "<<endl;

                string filename;

                cin>>filename;

                //opening input file, verifying if it is opened correctly

                ifstream inFile1(filename.c_str());

                if(!inFile1){

                                cout<<"Input file not found!"<<endl;

                                return 0;

                }

               

                //opening three output files for writing the filtered results

                ofstream outFile1("output1.txt");

                ofstream outFile2("output2.txt");

                ofstream outFile3("output3.txt");

               

                //applying each filter, storing in each output file

                encFilter.doFilter(inFile1,outFile1);          

                //resetting ifstream pointer to point to the beginning of file

                inFile1.close();

                inFile1.clear();

                inFile1.open(filename);

               

                upFilter.doFilter(inFile1,outFile2);

                inFile1.close();

                inFile1.clear();

                inFile1.open(filename);

                unchangedFilter.doFilter(inFile1,outFile3);

                inFile1.close();

                inFile1.clear();

                //closing each file, saving output

                outFile1.close();

                outFile2.close();

                outFile3.close();

                return 0;

}

/*OUTPUT*/

Enter input file name:

input.txt

//input.txt

some text

in this file

some

more text

following

some text

in this file

some

more text

following

//output1.txt

vrph whbw

lq wklv iloh

vrph

pruh whbw

iroorzlqj

vrph whbw

lq wklv iloh

vrph

pruh whbw

iroorzlqj

//output2.txt

SOME TEXT

IN THIS FILE

SOME

MORE TEXT

FOLLOWING

SOME TEXT

IN THIS FILE

SOME

MORE TEXT

FOLLOWING

//output3.txt

some text

in this file

some

more text

following

some text

in this file

some

more text

following

Add a comment
Know the answer?
Add Answer to:
A file filter reads an input file, transforms it in some way, and writes the results...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this module you learned how to create polymorphic code using virtual functions. A file filter...

    In this module you learned how to create polymorphic code using virtual functions. 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...

  • Description 1. This project will create a base account class that has the members: std::string account_code;...

    Description 1. This project will create a base account class that has the members: std::string account_code; std::string first_name; std::string last_name; double balance; Provide a constructor that initializes ALL members in its initialization list with data passed in as arguments to the constructor. Provide any accessor functions you may need (e.g. to get the account code and balance and to set the balance). In addition, include two pure virtual functions that look like the following: virtual void monthly_update() = 0; virtual...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • General Requirements . . . Write a program that reads letters from a file called "letters.txt"....

    General Requirements . . . Write a program that reads letters from a file called "letters.txt". Your program will open the letters.txt file read in one character at a time For this assignment the test file will contain at least 30 letters, uppercase OR lowercase In your program you will change each letter (solution) to uppercase Use the function toupper in #include <ctype.h> You create a numerical version of the uppercase letter from the file . o Use int numberSolution...

  • OUTCOMES After you finish this assignment, you will be able to do the following: Define an...

    OUTCOMES After you finish this assignment, you will be able to do the following: Define an abstract class Create concrete classes from an abstract class Overload an operator Split classes into .h and .cpp files Open files for reading Write to files Use output manipulators such as setw, fixed, and setprecision DESCRIPTION A binary arithmetic operation takes two double operands (left and right) to perform addition, subtraction, multiplication, or division on. For example, 10 + 11 is an addition (+)...

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • this is the essay.h file this is the gradedActivity.h file this is the gradeActivity.cpp file Can...

    this is the essay.h file this is the gradedActivity.h file this is the gradeActivity.cpp file Can someone help me write the code for this in c++. I'm really struggling with this. If you can include comments and a screenshot of the test run it would be greatly appreciated Part 1: Implement Essay.h and demonstrate the Essay class The Essay class is derived from the GradedActivity class presented in chapter 15. The Essay class determines the grade a student receives on...

  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • Does any one can show me the code in C++ ll cricket LTE 80% 16:58 Back...

    Does any one can show me the code in C++ ll cricket LTE 80% 16:58 Back P2.B MYString v1 Detail Submission Grade For this program, your MYString variables will never need to grow beyond length 20, in program 3 you will need to be allow your string that is held in the class to be able to be larger than 20 chars. So you may want to start allowing your strings to be able to grow....if you have extra time....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT