I am having an extremely difficult time with this programming challenge I have to do. I have no idea where to begin on this. I am a beginning programmer (obvious I know), and here are my instructions for the challenge:

"Write a class EncryptableString that is derived from the STL string class. The EncryptableString class adds a member function

void encrypt( )

That encrypts the string contained in the object by replacing each letter with its successor in the ASCII ordering. For example, the string baa would be encrypted to cbb. Assume that all characters that are part of an EncryptableString object are letters a..z and A...Z, and that the successor of z is a and the successor of Z is A. Test your class with a program that asks the user to enter strings that are then encrypted and printed.

If someone can maybe lay these instructions out in a more clear and simplified form so I can possibly comprehend it better. I am quite confused on how this should be done.

This is what tiny little beginning I have, hopefully it's right. I still need a lot of help on laying out where the stuff should go, and then of course actually doing it. Any help would be greatly appreciated.

EncryptableString.h

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

class EncryptableString
{
public:
   EncryptableString();
   void encrypt();
};

EncryptableString.cpp

#include "EncryptableString.h"
#include <iostream>
#include <string>
using namespace std;


void EncryptableString::encrypt()
{
	// Encryption Code Here
}


EncryptString.cpp

#include "EncryptableString.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
EncryptableString.encrypt();

	cin.get();
	return 0;
}

Recommended Answers

All 3 Replies

I think those instructions are pretty clear.

Create a class, that is derived from the string class. You will have to create your own constructor(s), the encryption method, and ideally input and output methods. See below for a general framework

#include <iostream>
#include <string>

using namespace std;

class myString: public string
{
private:
	

public:
	myString( );
	myString( string source );
	void modify( );
	string theString;  //should be private, and need to include input output methods
};

myString::myString( )
{
	theString = "";
}

myString::myString(string source )
{
	theString = source;
}

void myString::modify( )
{
	theString[0] = 'A';
}


int main()
{    
	myString s1;
	myString s2( "Hello, World" );
	cout << s1.theString << endl << s2.theString << endl << endl;

	s2.modify( );
	cout << s2.theString << endl;

	return 0;
}

You can use this to encrypt your string.

void EncryptableString::encrypt()
{
	for( int i = 0; i < content.length(); i++ )
	{
		if( (content[i] >= 'a' && content[i] < 'z') || (content[i] >= 'A' && content[i] < 'Z'))
		{
			content[i] += 1;
		}
		else if( content[i] == 'z' || content[i] == 'Z' )
		{
			content[i] -= 25;
		}
	}	
}

I made a class that has a private variable content and has two public functions: print() and encrypt().

Based on some of your start code you showed I have a feeling you are going to run into a few problems while doing this project.

#include "EncryptableString.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
EncryptableString.encrypt();

	cin.get();
	return 0;
}

EncryptableString.encrypt() means nothing because it is a variable type not a variable its self.

Coming across this thread made me wonder how to output a variable within a class without having to call a function. I made a new thread for that but not knowing how to do that forced me to use a print() function, which I think is messy unless this is going to be a closed source submission.

I believe your homework is complete. All you have to do know is put those two in one and study it well, understanding how to program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.