I am also supposed to include a member function:
void doFilter(ifstream &in, ofstream &out)
When he brought these up to me I hit a brick wall and I am having problems rewriting program to take these changes.
Here is my main cpp file
#include"Filter.h"
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
// declare local variables
const int NAME_LENGTH = 80;
char inFileName[NAME_LENGTH], outFileName[NAME_LENGTH];
// get the file to open and encrypt
cout << "\n\n\n";
cout << " Enter the file name: ";
cin >> inFileName;
// enter the file to write the encrypted data to
cout << "\n\n";
cout << " Enter the file to hold the Encrypted data: ";
cin >> outFileName;
// encrypt the data
Encryption fileEncrypt(inFileName, outFileName);
fileEncrypt.encrypt();
// enter the file to write the uppercase transformation to
cout << "\n\n";
cout << " Enter the file name to hold the uppercase data: ";
cin >> outFileName;
// transform file to uppercase
Upper fileUpper(inFileName, outFileName);
fileUpper.encrypt();
// enter the file to write the original data to
cout << "\n\n";
cout << " Enter the file to hold the original data: ";
cin >> outFileName;
// copy original data
Original fileOriginal(inFileName, outFileName);
fileOriginal.encrypt();
return 0;
}
Here is my class header file.
#include<iostream>
#include<fstream>
using namespace std;
// main class file
class Filter
{
protected:
ifstream inFile;
ofstream outFile;
public:
Filter(char *inFileName, char *outFileName); // constructor
~Filter(); // destructor
virtual char transform(char ch) = 0; // pure virtual function
void encrypt();
};
// constructor
Filter::Filter(char *inFileName, char *outFileName)
{
// open the file to recieve the data from and send the data to
inFile.open(inFileName);
outFile.open(outFileName);
// validate the file exists
if(!inFile)
{
cout << "\n\n";
cout << " The file " << inFileName << " cannot be opened!! \n\n";
exit(1);
}
// validates the fiel can be written
if(!outFile)
{
cout << "\n\n";
cout << " The file " << outFileName << " cannot be opened!! \n\n";
exit(1);
}
}
// desturctor
Filter::~Filter()
{
// close the files opened and created
inFile.close();
outFile.close();
}
// encryption function
void Filter::encrypt()
{
// local variables
char ch;
char transCh;
// get the data from the file
inFile.get(ch);
// get the data until you reach the end
while(!inFile.fail())
{
// sets transCh to the returned value of the virtual function
transCh = transform(ch);
// writes the data to the created file as it is transformed
outFile.put(transCh);
// gets the next character
inFile.get(ch);
}
}
// sub class to encrypt the data
class Encryption:public Filter
{
public:
// virtual function and returns encrypted data
char transform(char ch)
{
return ch + 1;
}
Encryption(char *inFileName, char *outFileName):Filter(inFileName, outFileName)
{}
};
// sub class to tranform data to all uppercase
class Upper:public Filter
{
public:
// virtual that transforms the data to all uppercase
char transform(char ch)
{
return toupper(ch);
}
Upper(char *inFileName, char *outFileName):Filter(inFileName, outFileName)
{}
};
// sub class to copy original data
class Original:public Filter
{
public:
// virtual function to return original data
char transform(char ch)
{
return ch;
}
Original(char *inFileName, char * outFileName):Filter(inFileName, outFileName)
{}
};
I am confused cause it looks like its gonna change every part of my code just slightly but I dont even know where to begin.

New Topic/Question
Reply



MultiQuote




|