Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ hello there, can anybody please help me? i have been trying all day to do th

ID: 3666512 • Letter: C

Question

C++

hello there,

can anybody please help me? i have been trying all day to do this but i couldn't. i am taking a computer science class and i am not majoring in it. the materials we take are too hard for me to understand.

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

class Encryption
{
   protected:
      ifstream inFile;
      ofstream outFile;
   public:
      Encryption(char *inFileName, char *outFileName);
      ~Encryption();
      virtual char transform(char ch) const = 0;
      void encrypt();

};

Encryption::Encryption(char *inFileName, char *outFileName)
{
   inFile.open(inFileName);
   outFile.open(outFileName);
   if (!inFile)
   {
      cout << "The file " << inFileName
         << " cannot be opened.";
      exit(1);
   }
   if (!outFile)
   {
      cout << "The file " << outFileName
         << " cannot be opened.";
      exit(1);
   }
}
Encryption::~Encryption()
{
   inFile.close();
   outFile.close();
}


void Encryption::encrypt()
{
   char ch;
   char transCh;
   inFile.get(ch);
   while (!inFile.fail())
   {
      transCh = transform(ch);
      outFile.put(transCh);
      inFile.get(ch);
   }
}

class SimpleEncryption : public Encryption
{
   public:
      char transform(char ch) const
      {
         return ch+1;
      }
      SimpleEncryption(char *inFileName, char *outFileName)
         : Encryption(inFileName, outFileName)
      {
      }
};

int main()
{
   char inFileName[80], outFileName[80];
   cout << "Enter name of file to encrypt: ";
   cin >> inFileName;
   cout << "Enter name of file to receive "
      << "the encrypted text: ";
   cin >> outFileName;
   SimpleEncryption obfuscate(inFileName, outFileName);
   obfuscate.encrypt();
   return 0;
}

Explanation / Answer

1. We can use key value as data member in class Encryption and not necessary to store in file

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
class Encryption
{
   protected:
      ifstream inFile;
      ofstream outFile;
      int key;
   public:
      Encryption(char *inFileName, char *outFileName);
      ~Encryption();
      virtual char transform(char ch) const = 0;
      void encrypt();
      void decrypt(char *inFileName, char *outFileName );
};

Encryption::Encryption(char *inFileName, char *outFileName)
{
   cout <<" Enter Key value for encryption ";
   cin >>key;

   inFile.open(inFileName);
   outFile.open(outFileName);
   if (!inFile)
   {
      cout << "The file " << inFileName
   << " cannot be opened.";
      exit(1);
   }
   if (!outFile)
   {
      cout << "The file " << outFileName
   << " cannot be opened.";
      exit(1);
   }
}
Encryption::~Encryption()
{
   inFile.close();
   outFile.close();
}


void Encryption::encrypt()
{
   char ch;
   char transCh;
   inFile.get(ch);
   while (!inFile.fail())
   {

      transCh = transform(ch);
      outFile.put(transCh);
      inFile.get(ch);
   }
}

void Encryption::decrypt( char *inFileName, char *outFileName)
{
   char ch;
   char transCh;
   //closing file that was already opened
   inFile.close();
   inFile.open(outFileName);
   if (!inFile)
   {
       cout << "The file " << outFileName
   << " cannot be opened.";
      exit(1);
   }

   inFile.get(ch);
   cout<<" EDecrypted File content ";
   while (!inFile.fail())
   {

//   decrypt the file by subtracting the key value
      transCh = ch-key;
      cout.put(transCh);
      inFile.get(ch);
   }
}


class SimpleEncryption : public Encryption
{
public:
      char transform(char ch) const
      {
   return ch+key;
      }
      SimpleEncryption(char *inFileName, char *outFileName)
      : Encryption(inFileName, outFileName)
      {
      }
};

int main()
{
   char inFileName[80], outFileName[80];
   cout << "Enter name of file to encrypt: ";
   cin >> inFileName;
   cout << "Enter name of file to receive "
      << "the encrypted text: ";
   cin >> outFileName;
   SimpleEncryption obfuscate(inFileName, outFileName);
   obfuscate.encrypt();
   obfuscate.decrypt(inFileName,outFileName);

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote