#include <iomanip>
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;
class STLstring
{
private:
string word;
public:
STLstring()
{
word = "";
}
void setWord(string w);
string getWord();
};
class EncryptString:public STLstring
{
private:
void encrypt();
void decrypt();
};
/*****************IMPLEMENTATION*******************/
void STLstring::setWord(string w)
{
void encrypt();
word = w;
cout << word;
}
string STLstring::getWord()
{
void decrypt();
return word;
}
void EncryptString::encrypt()
{
string temp = getWord();
temp = (temp - 5) %26;
setWord(temp);
}
void EncryptString::decrypt()
{
string temp = getWord();
setWord(temp);
}
int main()
{
string word = "";
EncryptString EncrptStr;
cout << "Enter a word and I will encrypt it so that you cannot read it any longer." << endl;
getline(cin, word);
cout << "\nHere is the encrypted word..." << endl;
EncrptStr.setWord(word);
cout << "\nHere is the decrypted word..." << endl;
cout << EncrptStr.getWord() << endl;
}
temp = (temp - 5) %26;
錯誤錯誤說:在沒有比賽的「操作符 - '溫度 - 5' 我我試圖做的是一個ceasar密碼,我知道我還沒有完成密碼,但我認爲即使完成它,錯誤仍然會出現,我應該在課堂上做一個重載操作符?如果是這樣如何?我認爲重載只在兩個類之間。
不要爲字符串實現'operator-'。 –
@LightnessRacesinOrbit:我澄清了我的答案的一部分。謹慎解釋你的評論? – wallyk
這樣的操作符會令人驚訝,它的語義不清。避免創造一個誘惑;這將是一個運營商濫用的完美例子。看到urzeit的答案。 –