Question

In: Computer Science

Q: IN C++ -  Redesign the CaesarCipher class as a subclass of the SubstitutionCipher from the previous...

Q: IN C++ -  Redesign the CaesarCipher class as a subclass of the SubstitutionCipher from the previous problem

my code is:

#include <iostream>
#include <string>

using namespace std;

class SubstitutionCipher {
public:
//Define your key which is a permutation of 26 alphabets
string key;

//Constructor to create an instance which initializes the key with the given permutation
SubstitutionCipher(string k) {
key = k;
}

//function to encode a string
string encode(string data) {
//initialize encoded data
string encodedData = data;
//replace each character in encodedData with the corresponding mapped data in key. We subtract a char with 'A' to get its position
for (int i = 0; i < data.length(); i++) {
encodedData[i] = key.at(data.at(i) - 'A');
}
//return the encoded data
return encodedData;
}

//function to decode a string
string decode(string data) {
//initialize decoded data
string decodedData = data;
//replace each character in decodedData with the corresponding reverse mapped data in key. We add the position with 'A' to get the corresponding char
for (int i = 0; i < data.length(); i++) {
decodedData[i] = key.find(data.at(i)) + 'A';
}
//return the decoded data
return decodedData;
}

};

//main() method to test the implementation
int main() {
SubstitutionCipher cipher("ZYXWVUTSRQPONMLKJIHGFEDCBA");
string data = "DEBAJYOTI";
string encodedData = cipher.encode(data);
cout << data << " encoded as " << encodedData << endl;
string decodedData = cipher.decode(encodedData);
cout << encodedData << " decoded as " << decodedData;
}

Solutions

Expert Solution

#include <iostream>
#include <string>

using namespace std;

class SubstitutionCipher {
public:
//Define your key which is a permutation of 26 alphabets
string key;

//Constructor to create an instance which initializes the key with the given permutation
SubstitutionCipher(string k) {
key = k;
}

//function to encode a string
string encode(string data) {
//initialize encoded data
string encodedData = data;
//replace each character in encodedData with the corresponding mapped data in key. We subtract a char with 'A' to get its position
for (int i = 0; i < data.length(); i++) {
encodedData[i] = key.at(data.at(i) - 'A');
}
//return the encoded data
return encodedData;
}

//function to decode a string
string decode(string data) {
//initialize decoded data
string decodedData = data;
//replace each character in decodedData with the corresponding reverse mapped data in key. We add the position with 'A' to get the corresponding char
for (int i = 0; i < data.length(); i++) {
decodedData[i] = key.find(data.at(i)) + 'A';
}
//return the decoded data
return decodedData;
}

};
class ccipher: public SubstitutionCipher
{
   public:
void encrypt(string text, int s)
{
   string result = "";

   // traverse text
   for (int i=0;i<text.length();i++)
   {
       // apply transformation to each character
       // Encrypt Uppercase letters
       if (isupper(text[i]))
           result += char(int(text[i]+s-65)%26 +65);

   // Encrypt Lowercase letters
   else
       result += char(int(text[i]+s-97)%26 +97);
   }

   // Return the resulting string
//   return result;
cout << "\n C_Cipher: " <<result;
}   
};

//main() method to test the implementation
int main() {
SubstitutionCipher cipher("ZYXWVUTSRQPONMLKJIHGFEDCBA");
string data = "DEBAJYOTI";
string encodedData = cipher.encode(data);

cout << data << " encoded as " << encodedData << endl;
string decodedData = cipher.decode(encodedData);
cout << encodedData << " decoded as " << decodedData;
ccipher cc;
cc.encrypt(data, 4);
}


Related Solutions

C. Design a RandomCipher class as a subclass of the substitutionCipher from exercise P-3.40, so that...
C. Design a RandomCipher class as a subclass of the substitutionCipher from exercise P-3.40, so that each instance of the class relies on a random permutation of letters for its mapping.
Q. Explain how to invoke a superclass method from a subclass method for the case in...
Q. Explain how to invoke a superclass method from a subclass method for the case in which the subclass method overrides a superclass method and the case in which the subclass method does not override a superclass method. [1 Mark] this is a correct answer but i want answer it  in different way   : In the case where the subclass method overrides the superclass method, it is necessary to explicitly use the keyword super, to invoke the method of the superclass as...
1 .RuntimeException class is a subclass of the Exception class. Fill in the blanks to complete...
1 .RuntimeException class is a subclass of the Exception class. Fill in the blanks to complete the declaration of the RuntimeException class. _____________   ________________    _________________ . . . 3. RuntimeException is a subclass of Exception. The constructor RuntimeException(String message) calls the parent class's constructor with the message argument. Which of the following makes the call correctly? this(message); super(message); super.Exception(message); Exception(message); 4. RuntimeException has overloaded constructors: the 1-argument constructor RuntimeException(String message) and the 2-argument constructor RuntimeException(String message, Throwable cause). The 2-argument...
Python Clean up the code Recall that once a subclass inherits from a parent class, it...
Python Clean up the code Recall that once a subclass inherits from a parent class, it automatically has access to all of the parents' methods. Sometimes, the subclass needs to do extra things within a method which the parent does not do. For example, both UserPlayer and BasicMonster have their specialized __init__ methods which override the one from Player. Discuss the following question with your partner: What are the other methods that are being overridden by a subclass in the...
You decide to redesign your undergraduate lunch survey from the previous item to improve its sampling...
You decide to redesign your undergraduate lunch survey from the previous item to improve its sampling method. Describe two ways you could improve your sampling, mentioning the potential advantages and limitations of each.
Write a class that extends the LeggedMammal class from the previous laboratory exercise.
C++ code on Visual Studio Code:Write a class that extends the LeggedMammal class from the previous laboratory exercise. The class will represent a Dog. Consider the breed, size and is registered. Initialize all properties of the parent class in the new constructor. This time, promote the use of accessors and mutators for the new properties. Instantiate a Dog object in the main function and be able to set the values of the properties of the Dog object using the mutators....
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
Locate your Student project from the previous in-class assignment. You will add a new class to...
Locate your Student project from the previous in-class assignment. You will add a new class to that project. 1. Create a class called Course. It should have a field to hold an ArrayList of Student objects. 2. Create a constructor for Course that accepts an ArrayList of Student objects, and set field to the parameter. 3. In the Course class, create a method called addStudent(Student s). This should add the parameter "s" to the ArrayList of Student objects. 4. In...
ASAP (Use BigInteger for the Rational class) Redesign and implement the Rational class in Listing 13.13...
ASAP (Use BigInteger for the Rational class) Redesign and implement the Rational class in Listing 13.13 using BigInteger for the numerator and denominator. Use the code at https://liveexample.pearsoncmg.com/test/Exercise13_15Test.txt to test your implementation. Here is a sample run: Sample Run Enter the first rational number: 3 454 Enter the second second number: 7 2389 3/454 + 7/2389 = 10345/1084606 3/454 - 7/2389 = 3989/1084606 3/454 * 7/2389 = 21/1084606 3/454 / 7/2389 = 7167/3178 7/2389 is 0.0029300962745918793 Class Name: Exercise13_15 If...
Java Apply inheritance to write a super class and subclass to compute the triangle area and...
Java Apply inheritance to write a super class and subclass to compute the triangle area and the surface area of triangular pyramid, respectively. Assume that each side has the same length in the triangle and triangular pyramid. You need also to override toString() methods in both of super class and subclass so they will return the data of an triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT