Question

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 private member variable of the string type to hold the encrypted message. A constructor that receives the file name as a parameter and reads the text into the object. A pure virtual function decode that will be implemented in the derived classes. A function that prints the message on the screen.

Define two derived classes: CypherA and CypherB. Each one should implement its own version of decode according to the following algorithms:

CypherA should use the following key to decode the message: input character: abcdefghijklmnopqrstuvwxyz decoded character: iztohndbeqrkglmacsvwfuypjx Each 'a' in the input text should be replaced with an 'i', each 'b' with a 'z' and so forth.

CypherB should implement the "rotational cypher" algorithm. In this encryption method, a key is added to each letter of the original text. In order to decode you need to subtract 4. For example:

Cleartext: A P P L E Key: 4 4 4 4 4 Ciphertext: E T T P I

Main Program here
#include
#include
#include

#include "CypherA.h"
#include "CypherB.h"

using namespace std;

int main(int argc, const char * argv[]) {

string filename;

cout << "Enter file name to be decoded with algorithm A: ";
cin >> filename;

class CypherA messageA (filename);

messageA.decode();
cout << "Decoded message: \n";
messageA.print();
cout << endl << endl;

cout << "Enter file name to be decoded with algorithm B: ";
cin >> filename;

class CypherB messageB (filename);
messageB.decode();
cout << "Decoded message: \n";
messageB.print();
cout << endl;

return 0;
}

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

Below are the complete codes of classes. It is well explained inside the code using classes:

// Cypher.h
#pragma once

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


// abstract class Cypher
class Cypher
{
private:
   string encryptedMessage;
public:
   // constructor recieves file name as parameter
   Cypher(string fileName)
   {
       ifstream file(fileName);   // open file to read

       // if file is open
       if (file.is_open())
       {
           // read content into encryptedMessage
           getline(file, encryptedMessage);
           file.close();
       }
       else
       {
           cout << "Unable to open the file" << endl;
       }
   }

   // Pure Virtual Function
   virtual void decode() = 0;

   // print function
   void print();

   // return the encrypted message
   string getEncryptedMessage()
   {
       return encryptedMessage;
   }
};

********************************************************************************************************************

// CypherA.h
#pragma once
#include "Cypher.h"

class CypherA : public Cypher
{
private:
   string decodedMessage;
public:
   // constructor
   CypherA(string fileName) :Cypher(fileName){}

   // decodes the message
   void decode()
   {
       // decode key
       string key = "iztohndbeqrkglmacsvwfuypjx";

       decodedMessage = "";
       int pos;
       char decodedChar;

       // get encrypted
       string encrypted = getEncryptedMessage();

       // iterate though character by character and decode
       for (int i = 0; i < encrypted.length(); i++)
       {
           // if character is space, append to decoded string as it is
           if (encrypted[i] == ' ')
               decodedMessage += ' ';
           else
           {
               // get position of character
               pos = tolower(encrypted.at(i)) - 'a';

               // get decoded character
               decodedChar = key.at(pos);

               // add to message
               decodedMessage += decodedChar;
           }
       }
   }

   void print()
   {
       cout << decodedMessage << endl;
   }
};

*******************************************************************************************************************

// CypherB.h
#pragma once
#include "Cypher.h"

class CypherB : public Cypher
{
private:
   string decodedMessage;
public:
   // constructor
   CypherB(string fileName) :Cypher(fileName){}

   // decodes the message
   void decode()
   {
       decodedMessage = "";
       int pos;
       char decodedChar;

       // get encrypted
       string encrypted = getEncryptedMessage();

       // iterate though character by character and decode
       for (int i = 0; i < encrypted.length(); i++)
       {
           // if character is space, append to decoded string as it is
           if (encrypted.at(i) == ' ')
               decodedMessage += ' ';
           else
           {
               // subtract 4 position to decode
               decodedChar = toupper(encrypted.at(i)) - 4;

               // if decoded character is less than 'A', rotate it
               if (decodedChar < 'A')
               {
                   decodedChar = 26 + decodedChar;
               }

               decodedMessage += decodedChar;
           }
       }
   }

   void print()
   {
       cout << decodedMessage << endl;
   }
};

Below is the sample output:

This completes the requirement. Let me know if you have any questions.

Thanks!

Add a comment
Know the answer?
Add Answer to:
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
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
  • This is my assignment prompt This is an example of the input and output This is...

    This is my assignment prompt This is an example of the input and output This is what I have so far What is the 3rd ToDo in the main.cpp for open the files and read the encrypted message? Does the rest of the code look accurate? Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

  • Abstract Classes and Interfaces. Write the code for all the necessary classes and/or interfaces for a...

    Abstract Classes and Interfaces. Write the code for all the necessary classes and/or interfaces for a solution to the problem below. Focus on class structure and interaction. You may implement your solution however you wish, but you will be graded on the appropriateness of your solution to the requirements. Note the use of capit and bold for clarification in the problem. You may use whatever constructors or additional methods you wish. - Define a structure that can represent Animals. -...

  • C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require...

    C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a file. The encryption method being used on the file is called a shift cipher (Info Here). I will provide you with a sample encrypted message, the offset, and the decrypted message for testing. For this project I will provide you main.cpp, ShiftCipher.h, and ShiftCipher.cpp....

  • IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality...

    IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...

  • In C++ Having heard you have gotten really good at programming, your friend has come to...

    In C++ Having heard you have gotten really good at programming, your friend has come to ask for your help with a simple task. She would like you to implement a program to encrypt the messages she exchanges with her friends. The idea is very simple. Every letter of the alphabet will be substituted with some other letter according to a given key pattern like the one below. "abcdefghijklmnopqrstuvwxyz" "doxrhvauspntbcmqlfgwijezky" // key pattern For example, every 'a' will become a...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...

    WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...

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