我正在製作一個程序,用於加密和解密使用2D表格的文本短語。我有一個單獨的類,它包含密碼所需的所有內容。但是,在處理桌子時我遇到了麻煩。我已經把它構建得夠好了,但是我很難將它封裝在課堂上。我覺得應該在創建對象時自動構建它,但現在我不得不通過main調用它。如何將數組作爲私有類成員並確保正確封裝?
#include <iostream>
#include <string>
using namespace std;
class Cipher {
public:
Cipher();
Cipher(string key);
Cipher(string key, string message);
void buildTable(char table[][26]);
void newKey(string keyphrase);
void inputMessage();
string encipher(string message);
string decipher(string message);
string getPlainText() const;
string getCipherText() const;
private:
string key;
string plaintext;
string ciphertext;
};
。 。 。 。
void Cipher::buildTable(char table[][26]) {
char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o', 'p','q','r','s','t','u','v','w','x','y','z'};
int alphaIndex = 0;
for (int index1 = 0; index1 < 26; index1++) {
for (int index2 = 0; index2 < 26; index2++) {
if ((index1 + index2) < 26) {
alphaIndex = index1 + index2;
table[index1][index2] = alphabet[alphaIndex];
}
else
alphaIndex = 0;
while (((index1 + index2) > 25) && index2 < 26) {
table[index1][index2] = alphabet[alphaIndex];
index2++;
alphaIndex++;
}
}
}
}
此表是程序運行的關鍵,並且沒有理由改變它。我試圖將其作爲私人成員加入,但遇到了很多麻煩。我是否應該在構造函數中包含這個,或者封裝它的正確方法是什麼?
我說用PIMPL方法使實現私有。 http://stackoverflow.com/questions/60570/why-should-the-pimpl-idiom-be-used雖然我很困惑,當你讓你的表私人時,你有什麼問題。 – drescherjm
據我所知,該表應該在程序調用時保持不變,並在Cipher類的所有實例中共享,對吧? – user3159253
絕對應該是私人會員。你遇到什麼麻煩? –