什么是正确的封装语法?

What is the proper encapsulation syntax?

我有出版物和图书馆两个 classes 和在出版物中 class。如何操纵(作为封装)流派、媒体和 target_age,如果我希望它们分开 classes。它不在另一个 class 中 class。 该类型有更多类型(小说、非小说、自助、表演)以及媒体和年龄。我已经完成了我的研究,我正在寻找它的正确语法。

class Publication {    
  private:
    string title;
    string authore;
    string copyright;
    Genre genre;
    Media media;
    Age target_age;
    string isbn;
    bool checked_out;
    string patron_name;
    string patron_phone;

  public:
    void check_out(string patron_name, string patron_phone ){}
    void check_in(){}
    bool is_checked_out(){}
    string to_string(){}
};

最好的封装方式是将所有内容都保密。为可能从外部读取的内容创建常量 getter,并在构造函数中初始化所有内容。毕竟author/title/etc这样的事情。不应该为一本真正的书的实例而改变吗?看看下面的片段:

class Publication {    
  private:
    string _title;
    string _author;
    Genre _genre;

  public:
    void check_out(string patron_name, string patron_phone );
    void check_in();
    bool is_checked_out() const;

    string to_string() const;
    string get_title() const { return _title; }
    string get_author() const { return _author; }
    const Genre& get_genre() const { return _genre; }


    Publication(string author, string title, Genre genre) : _author(auth), _title(title), _genre(genre)
    { }
};