//Hi, I\'m having some difficulty with this program in C++ . Perhaps I am doing
ID: 3756528 • Letter: #
Question
//Hi, I'm having some difficulty with this program in C++. Perhaps I am doing it all wrong. I think I have correctly used loops to stream in chars, to encrypt and decrypt but am having trouble //with my paramatized contructor calling the next function with an argument.
In this assignment you will create a class called EncryptedString that stores a string in encrypted format. You will then write a small driver program to test your class.
An EncryptedString is either empty, or it consists only of spaces and alphabetic characters.
Class Members
Your class should have one private data field (sometimes also called a member variable or data member) of type string. This variable will store a string in encrypted format. Do not store any other data in your EncryptedString object.
Your class should have the following public methods (member functions):
A default (no argument) constructor that stores an empty string in the data field.
A constructor that takes a string as a parameter and stores the encrypted version of the parameter in the data field. This method should not actually do any "work". It should call the set method to encrypt and store the string.
A method called set that takes a string as a parameter. It should encrypt the string parameter and store the result in the object. See the Encryption Algorithm section below for information on encrypting the characters of the string. This method should discard any illegal characters in the string parameter. Remmeber that only alphabetic characters (upper and lower case letter A through Z) and the space (blank) character are allowed in an EncryptedString. The easiest way to remove any illegal characters is to start with an empty string. Then add characters as you encrypt them, leaving out the illegal characters.
A method called get that returns a decrypted version of the string that is stored in the object. The returned value should be exactly the same as the original string that was stored but with any illegal characters removed. This method should not change the encrypted string stored in the object.
Your method prototypes should be:
Notice that inside an EncryptedString object, only the encrypted version of a string is stored. Code outside the class will only see "normal" (unencrypted) strings. Each EncryptedString object is responsible for encrypting the string to be stored, and decrypting the string before it is returned.
Normally, you would want to prevent code from outside the class from "seeing" the encrypted version of the string. However, for debugging purposes I want you to add one more public method to your class:
A method called getEncrypted that returns the string in encrypted format. This method should not change the encrypted string stored in the object.
The method prototype should be:
Note: If you know what a constant method is, make get() and getEncrypted() constant methods.
Encryption algorithm
The space character is not encrypted. A space is stored as a space.
An alphabetic character is replaced by its successor in the ASCII code sequence. For example, the successor of ‘C’ (ASCII code 67) is ‘D’ (ASCII code 68). Exceptions: the successor of ‘Z’ is ‘A’, and the successor of ‘z’ is ‘a’.
For example, the string “Hi Mom!” would be encrypted as “Ij Npn”.
Hint: If you do not know how to find the next character in the ASCII code sequence, try out this program:
Decryption algorithm
The space character is not decrypted. A space is stored as a space.
An alphabetic character is replaced by its predecessor in the ASCII code sequence. For example, the predecessor of ‘C’ (ASCII code 67) is ‘B’ (ASCII code 66). Exceptions: the predecessor of ‘A’ is ‘Z’, and the predecessor of ‘a’ is ‘z’.
Driver
Write a small driver program to test your class.
Your program must be split into 3 files:
the driver (test) program,
a class header file (class definition with data member definitions and method prototypes only),
and class implementation file containing class method definitions.
Your class methods should not contain any input or output operations. All input and output should be done in your driver.
Explanation / Answer
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. If you are satisfied with the solution, please rate the answer. Thanks
// EncryptedString.h
#ifndef EncryptedString_h
#define EncryptedString_h
#include<iostream>
using namespace std;
class EncryptedString{
private:
//string variable to store encrypted text
string data;
public:
//method prototypes
EncryptedString();
EncryptedString(string str);
void set(string str);
string get();
//returns the string in encrypted format (for debugging purposes)
string getEncrypted();
};
#endif
// EncryptedString.cpp
#include "EncryptedString.h"
//implementing all methods of the class
EncryptedString::EncryptedString(){
data="";
}
EncryptedString::EncryptedString(string str){
set(str);
}
void EncryptedString::set(string str){
data="";
//iterating through all characters
for(int i=0;i<str.length();i++){
char c=str[i];
//checking if character is uppercase
if(c>='A' && c<='Z'){
//finding next character
char temp=c+1;
//wrapping around if char goes out of range
if(temp>'Z'){
temp='A';
}
//appending to encrypted string
data+=temp;
}else if(c>='a' && c<='z'){ //checking if character is lower case
char temp=c+1;
if(temp>'z'){
temp='a';
}
data+=temp;
}else if(c==' '){//checking if character is white space
data+=c; //adding to string
}
}
}
string EncryptedString::get(){
string decrypted="";
//iterating through all characters in data variable
for(int i=0;i<data.length();i++){
char c=data[i];
if(c>='A' && c<='Z'){
//finding previous character
char temp=c-1;
//wrapping around if char goes out of range
if(temp<'A'){
temp='Z';
}
decrypted+=temp;
}else if(c>='a' && c<='z'){
char temp=c-1;
if(temp<'a'){
temp='z';
}
decrypted+=temp;
}else if(c==' '){
decrypted+=c;
}
}
return decrypted;
}
string EncryptedString::getEncrypted(){
//returning encrypted text
return data;
}
//main.cpp
#include "EncryptedString.h"
int main(){
//defining a string
string str="Brown fox and Zebra!";
//creating EncryptedString object
EncryptedString encString;
//setting the string
encString.set(str);
//displaying original, encrypted and decrypted strings
cout<<"Original text: "<<str<<endl;
cout<<"Encrypted: "<<encString.getEncrypted()<<endl;
cout<<"Decrypted: "<<encString.get()<<endl;
}
//output
Original text: Brown fox and Zebra!
Encrypted: Cspxo gpy boe Afcsb
Decrypted: Brown fox and Zebra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.