Skip to content

Latest commit

 

History

History
78 lines (63 loc) · 2.74 KB

14.48.md

File metadata and controls

78 lines (63 loc) · 2.74 KB

(a) Book

No arithmetic operator should be defined for class Book, since no operation has logical mapping from built-in arithmetic operators. No relational operator should be defined for class Book, since there is not a single logical order for this class. The Book class does not need a copy- and move-assignment operator, because all data members can be copy- or move-assign correctly by the synthesized ones. No other assignment operator is needed since there are serveral members that all std::string, thus the assignment operator would be ambiguious. The Book class should not define conversion operator to bool, because it does not have meaningful bool value for a Book object.

class Book {
  friend bool operator==(const Book &, const Book &);
  friend bool operator!=(const Book &, const Book &);
  friend std::ostream &operator<<(std::ostream &, const Book &);
  friend std::istream &operator>>(std::istream &, Book &);

public:
  Book() : isbn(""), name(""), author(),
      publish_year(0), publisher(""), version(0) {}
  Book(const std::string &i, const std::string &n,
       const std::vector<std::string> &au,
       unsigned y, const std::string &p = "", unsigned v = 1)
      : isbn(i), name(n), author(au),
        publish_year(y), publisher(p), version(v) {}
  Book(std::istream &is) {
    is << *this;
  }

private:
  std::string isbn;
  std::string name;
  std::vector<std::string> author;
  unsigned publish_year;
  std::string publisher;
  unsigned version;
};

bool operator==(const Book &, const Book &);
bool operator!=(const Book &, const Book &);
std::ostream &operator<<(std::ostream &, const Book &);
std::istream &operator>>(std::istream &, Book &);

std::ostream &operator<<(std::ostream &os, const Book &rhs) {
  os << rhs.isbn << " " << rhs.name;
  for (const auto &a : rhs.author)
    os << " " << a;
  os << " " << rhs.publish_year << " " << rhs.version << " " << publisher;
  return os;
}

std::istream &operator>>(std::istream &is, Book &b) {
  is >> isbn >> name;
  std::string s;
  is >> s;
  author.push_back(s);
  is >> publish_year >> publisher >> version;
  if (!is)
    b = Book();
  return is;
}

bool operator==(const Book &lhs, const Book &rhs) {
  return lhs.isbn == rhs.isbn &&
         lhs.name == rhs.name &&
         lhs.author == rhs.author &&
         lhs.publish_year == rhs.publish_year &&
         lhs.publisher == rhs.publisher &&
         lhs.version == rhs.version;
}

bool operator!=(const Book &lhs, const Book &rhs) {
  return !(lhs == rhs);
}

(b) Date

(c) Employee

(d) Vehicle

(e) Object

(f) Tree