#include

#include // runtime_error

#include

#include

// Представляет ветеринара или дрессировщика

class Contact {

public:

 Contact() {}

 Contact(const std::string& name, const std::string& phone) :

  name_(name) {

  setPhone(phone);

 }

 std::string name() const { return name_; }

 std::string phone() const { return phone_; }

 void setName(const std::string& name) { name_ = name; }

 void setPhone(const std::string& phone) {

  using namespace std;

  using namespace boost;

  // Используйте Boost.Regex, чтобы убедиться, что телефон

  // задач в форме (ddd)ddd-dddd

  static regex pattern("\\([0-9]{3}\\)[0-9]{3}-[0-9]{4}");

  if (!regex_match(phone, pattern)) {

   throw runtime_error(string("bad phone number:") + phone);

  }

  phone_ = phone;

 }

private:

 std::string name_;

 std::string phone_;

};

// Сравнить на равенство два объекта класса Contact; используется в рецепте

// 14.9 (для полноты следует также определить operator!=)

bool operator--(const Contact& lhs, const Contact& rhs) {

 return lhs.name() == rhs.name() && lhs.phone() == rhs.phone();

}

// Записывает объект класса Contact в поток ostream

std::ostream& operator(std::ostream& out, const Contact& contact) {

 out << contact.name() << " " << contact.phone(); return out;

}

// Класс Animal представляет животное

class Animal {

public:

 // Конструктор по умолчанию класса Animal; этот конструктор будет вами

 // использоваться чаще всего Animal() {}

 // Конструирование объекта Animal с указанием свойств животного;

 // этот конструктор будет использован в рецепте 14.9

 Animal(const std::string& name,

  const std::string& species, const std::string& dob,

  const Contact& vet, const Contact& trainer) :

  name_(name), species_(species), vet_(vet), trainer_(trainer) {

   setDateOfBirth(dob)

  }

 // Функции доступа к свойствам животного

 std::string name() const { return name_; }

 std::string species() const { return species_; }

 boost::gregorian::date dateOfBirth() const { return dob_; )

 Contact veterinarian() const { return vet_; }

 Contact trainer() const { return trainer_; }

 // Функции задания свойств животного

 void setName(const std::string& name) { name_ = name; }

 void setSpecies(const std::string& species) { species_ = species; }

 void setDateOfBirth(const std::string& dob) {

  dob_ = boost::gregorian::from_string(dob);

 }

 void setVeterinarian(const Contact& vet) { vet_ = vet; }

 void setTrainer(const Contact& trainer) { trainer_ = trainer; }

private:

 std::string name_;

 std::string species_;

 boost::gregorian::date dob_;

 Contact vet_;

 Contact trainer_;

};

// Сравнение на равенство двух объектов Animal; используется в рецепте 14.9

Перейти на страницу:

Похожие книги