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 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.
Hii, I have answered this question before. 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 FileFilter class
class FileFilter{
public:
//defining pure virtual function
virtual void doFilter(ifstream &in, ofstream &out)=0;
};
//EncryptionFilter class
class EncryptionFilter: public FileFilter{
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 FileFilter{
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 FileFilter{
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.c_str());
upFilter.doFilter(inFile1,outFile2);
inFile1.close();
inFile1.clear();
inFile1.open(filename.c_str());
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
In this module you learned how to create polymorphic code using virtual functions. A file filter...
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);...
Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'MailorderSale' having one private member variable called 'shipping charge'. It should have constructor function and virtual function 'bill' (this function adds shipping charge to the price). Define all these functions (no need of default constructors). b) In the main function, define one object of DiscountSale with (11,10) initial values and...
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: 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...
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 (+)...
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...
Use Java and please try and show how to do each section. You are creating a 'virtual pet' program. The pet object will have a number of attributes, representing the state of the pet. You will need to create some entity to represent attributes in general, and you will also need to create some specific attributes. You will then create a generic pet class (or interface) which has these specific attributes. Finally you will make at least one subclass of...
C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private: string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){ return name; } }; class Circle : public...
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...
How do I set up my read function? here's my code so far: int read_sudoku_board(const char file_name[], int board[9][9]) { FILE * fp = fopen("sudoku.txt", "r"); int a,i,j,c; int count = 0; for(i = 0; i < a; i++){ for(j = 0;j < 9;j++){ c = fgetc(fp); if(c == '-'){ board[i][j] = 0; } else if(c >= 1 && c <= 9) printf(" %d"); else return -2; } count++; } if(count != a-1) return -1; else return 0; }12 Read...