#include #include #include // Lösningen ska visa på att datamedlemmar kan garanteras uppfylla // angivna kriterier så snart ett objekt skapats och under hela // objektets livstid. Publik/privat gränssnitt, konstruktor och // felhantering är viktigt. class Four_Letter_Word { public: Four_Letter_Word(std::string const& w) : word{w} { check(w); } std::string get() const { return word; } void set(std::string const& w) { check(w); word = w; } private: void check(std::string const& w) const { if ( w.length() != 4 ) { throw std::logic_error{"Not a four letter word"}; } } std::string word; }; using namespace std; int main() { Four_Letter_Word const w{"Frak"}; cout << w.get() << endl; Four_Letter_Word f{"f*n"}; }