Test.hpp // Write a preprocessor guard which will prevent this header file from
ID: 3696328 • Letter: T
Question
Test.hpp
// Write a preprocessor guard which will prevent this header file from being included more than once. The name
// used in the preprocessor guard must be unique, so a common C++ convention is to use the name of the source
// code file, written in all uppercase letters, with an underscore replacing the dot.
???
// Write the class declaration for a class named 'Test' using the class diagram in the lab project document as
// a guide. The class shall declare two function members: (1) the default constructor; and (2) a void function
// named run(). Both functions are public and neither function has input parameters. There are no data members
// or private functions.
???
// Write the preprocessor directive that ends the preprocessor guard.
???
Test.cpp
#include "Decryptor.hpp" // For Decryptor class declaration
#include "Encryptor.hpp" // For Encryptor class declaration
#include "Test.hpp" // For Test class declaration
using namespace std;
//----- Test() -------------------------------------------------------------------------------------------------
// Default constructor. Every class must have at least one constructor even if there are no data members to
// initialize. This particular constructor does nothing so just write a pair of empty braces with no statements
// inside them.
//--------------------------------------------------------------------------------------------------------------
???
//----- run() --------------------------------------------------------------------------------------------------
// Performs the testing of the Encryptor and Decryptor classes. We instantiate an Encryptor object and use it to
// encrypt the secret message stored in the file "plain.txt", writing the encrypted message to a file named
// "cipher.txt". Then we instantiate a Decryptor object and use it to decrypt the encrypted message stored in
// "cipher.txt", writing the output to a file named "plain-new.txt". To verify the program works correctly, load
// both "plain.txt" and "plain-new.txt" into the editor pane, and verify the two files are identical (except
// that "plain-new.txt" will not contain the whitespace characters that are in "plain.txt").
//--------------------------------------------------------------------------------------------------------------
// Write the function header and the left brace
???
// Define and instantiate an Encryptor object named 'encryptor' calling the default constructor to
// initialize the object.
???
// Call the encryptFile() function on 'encryptor' and pass "plain.txt" as the plaintext file name and
// "cipher.txt" as the ciphertext file name.
???
// Define and instantiate a Decryptor object named 'decryptor' calling the default constructor to
// initialize the object.
???
// Call the decryptFile() function on 'decryptor' and pass "cipher.txt" as the ciphertext file name and
// "plain-new.txt" as the new plaintext file name.
???
// End the function
???
Explanation / Answer
1)
header guards are implemented generally to avoid content of header files are not included twice so these small codes are written
Header guards are implemented by using three preprocessor directives in a header file. Two are placed at the beginning of the file, before any pertinent code. The last is placed at the end of the file. The first header guard line is of the form
and is followed immediately by the line
The line
is placed at the end of the file.
2)
class Test
{
public:
Test()//default constructor same name as that of a class
{
}
void run()
{
}
};
main()
{
Test t;
t.run();
}
3)
4)
Encryptor () : | | + encryptFile (pPlainFile : string, pCipherFile : string) : void | | encryptPlainChar (pPlainChar : char) : string
Decryptor () : | | + decryptFile (pCipherFile : string, pPlainFile : string) : void | | decryptCipherString (pCipherString : string) : char | | readCipherString(pFin : ifstream&) : string
void Encryptor::encryptFile(string pPlainFile, string pCipherFile) { ifstream fin(pPlainFile.c_str()); // pass a Cstring to the ifsteam ctor ofstream fout(pCipherFile.c_str()); // pass a Cstring to the ofstream ctor ... }
ifstream fin("foo.txt"); // Define and instantiate an ifstream object named fin opening "foo.txt" for reading char ch1 = fin.get(); // ch1 '*' char ch2 = fin.get(); // ch2 '*' char ch3 = fin.get(); // ch3 ' ' char ch4 = fin.get(); // ch4 '*' char ch5 = fin.get(); // ch5 '*'
ifstream fin("cipher.txt"); // Define and instantiate an ifstream object named fin string cipherstring = ""; // A ciphertext "character" is really a 5char string
fin >> cipherstring; // No! No!! No!!! Will not work
char ch = fin.get(); // Read the first char, even if it is whitespace cipherstring = cipherstring + ch; // Concatenate ch onto the end of cipherstring ch = fin.get(); // Read the next char, even if it is whitespace cipherstring = cipherstring + ch; // Concatenate ch onto the end of cipherstring ch = fin.get(); // Read the next char, even if it is whitespace cipherstring = cipherstring + ch; // Concatenate ch onto the end of cipherstring ch = fin.get(); // Read the next char, even if it is whitespace cipherstring = cipherstring + ch; // Concatenate ch onto the end of cipherstring ch = fin.get(); // Read the next char, even if it is whitespace cipherstring = cipherstring + ch; // Concatenate ch onto the end of cipherstring
ifstream fin("cipher.txt"); // Define and instantiate an ifstream object named fin string cipherstring = ""; // A ciphertext "character" is really a 5char string for (cntr = 1; cntr <= 5; ++cntr) { // A counting loop which executes five times char ch = fin.get(); // Read the next char, even if it is whitespace cipherstring = cipherstring + ch;
plain.txt ABCDEFGHIJKLMNOPQRSTUVWXYZ After running the program, the contents of "cipher.txt" should be, cipher.txt ** * * * **** * * * *** * ** **** * * * ***** * ** ** ** * **** ** * *** ****** * * * ** ** * ** ** #####
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.