Question: When deleting the keyword virtual from the transform function in the base class, what happens based on the result you see when run? Explain why

When deleting the keyword virtual from the transform function in the base class, what happens based on the result you see when run? Explain why you would make the transform function a virtual function in the base class. (C++)

//deomonstrates an application of pure virtual functions

#include

#include

using namespace std;

class encryption // page 956

{

protected:

int key;

ifstream infile;

ofstream outfile;

public:

encryption(char *infilename, char *outfilename);

~encryption();

//pure virutal function

virtual char transform(char ch)const = 0;

void setkey(int key)

{

this->key = key;

}

int getkey()

{

return key;

}

//do actual work

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);

}

}

//desctructor closes files

encryption::~encryption()

{

infile.close();

outfile.close();

}

//encrypt frunction uses the virtual transforms member fruction to transform individual characters

void encryption::encrypt()

{

char ch;

char transCh;

infile.get(ch);

while (!infile.fail())

{

transCh = transform(ch);

outfile.put(transCh);

infile.get(ch);

}

}

//subcalls simple overides the virtual transformation function

class simpleencryption: public encryption

{

public:

char transform(char ch) const

{

return ch + key;

}

simpleencryption(char *infilename, char *outfilename):encryption(infilename, outfilename)

{}

};

void main(void)

{

char infilename[80], outfilename[80];

int k;

cout << "Enter name of the file to encrypt: ";

cin >> infilename;

cout << "Enter name of file to recieve the encrypted text: ";

cin >> outfilename;

simpleencryption obfuscate(infilename, outfilename);

//ask the user to enter the encryption key

cout << "Enter an encryption key "<

cin >> k;

obfuscate.setkey(k);

obfuscate.encrypt();

system("pause");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!