So why didn't you use the same std::string class in your Enigma and Rotor classes instead of char*? Then you wouldn't need to write copy constructor and assignment operator -- that's the real point I was trying to make.
Secondly, you did not actually write any implementation for the copy constructor or assignment operator.
Code:
#include <iostream>
#include <string>
using namespace std;
class Rotor
{
public:
Rotor(char pos);
int GetSteps()const{return steps;}
static int CharacterMap(char the_char);
void SetRotorPosition(int NewPos);
void AdvanceRotor(int Steps);
void ReverseRotor(int Steps);
char GetCurrentCharacter()const;
char GetCharacterIndex(int Index)const;
char GetCharacterInverse(int i)const;
private:
std::string Alphabet;
int CurPos;
int steps;
};
Code:
static const char* alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Rotor::Rotor(char pos=0):CurPos(0),steps(0), Alphabet(alphabet)
{
SetRotorPosition(CharacterMap(pos));
}
Code:
#pragma once
#include "Rotor.hpp"
#include <string>
class Enigma
{
public:
Enigma(char r1,char r2,char r3,char r4,char r5);
std::string Encrypt(const string& text);
private:
char plugboard(char Char);
Rotor R1;
Rotor R2;
Rotor R3;
Rotor R4;
Rotor R5;
std::string Reflector;
};
Code:
Enigma::Enigma(char r1,char r2,char r3,char r4,char r5): Reflector("COAYIWV7E3809TBUZ26NPGF4DQL5RJX1SHKM"),
R1(r1),R2(r2),R3(r3),R4(r4),R5(r5) { }
There is no need for dynamic memory allocation at all in any of your code -- instead a std::string is used.
And lastly, your "plugboard" function could have easily been written this way:
Code:
char Enigma::plugboard(char Char)
{
static const char* returnData = "0QWEDTYUIOPSNMJKBZLFHXCVGRA987654321";
if (Char >= 'A' && Char <= 'Z')
return returnData[Char - 'A'];
else
if ( Char >= '0' && Char <= '9')
return returnData[26 + Char - '0'];
return ' ';
}
There was no need for endless if/else statements.
Regards,
Paul McKenzie