c0d3r
Junior Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Код: #include <ctime> #include <iostream> #include <ostream> #include <string> #include <stdexcept> using std::cout; using std::endl; using std::ostream; using std::string; using std::out_of_range; using std::invalid_argument; typedef enum { BOOK_ARTISTIC, // художественная BOOK_METHODICAL,// методическая BOOK_HAND, // справочник BOOK_OTHER // другое } BookType; class Book { string name; string author; BookType type; size_t year; // needed for setAge() size_t curYear; static const size_t MIN_YEAR = 1900; static const size_t MAX_YEAR = 2054; public: Book(string name_, string author_, size_t year_, BookType type_) : name(name_), author(author_), type(type_), curYear(0) { setYear(year_); /// @todo check return values const time_t t = time(NULL); const struct tm *tm = localtime(&t); curYear = tm->tm_year + 1900; } // ------- // getters // ------- const string &getName() const { return name; } const string &getAuthor() const { return author; } BookType getType() const { return type; } size_t getYear() const { return year; } size_t getAge() const { return curYear - year; } // ------- // setters // ------- /// @exception std::invalid_argument for empty string void setName(string name_) { if (name_.empty()) { throw invalid_argument("Book's name shouldn't be empty!"); } name = name_; } /// @exception std::invalid_argument for empty string void setAuthor(string author_) { if (author_.empty()) { throw invalid_argument("Author's name shouldn't be empty!"); } author = author_; } void setType(BookType type_) { type = type_; } /// @exception std::out_of_range if date is wrong void setYear(size_t year_) { if (year_ < MIN_YEAR) { throw out_of_range("Wrong date! Should be greater than " + MIN_YEAR); } if (year_ > MAX_YEAR) { throw out_of_range("Wrong date! Should be less than " + MAX_YEAR); } year = year_; } }; inline ostream & operator<<(ostream &os, const Book &book) { os << "Name: " << book.getName() << endl << "Author: " << book.getAuthor() << endl << "Year: " << book.getYear() << endl << "Age: " << book.getAge() << endl; return os; } int main() { Book cpp_book("The C++ Programming Language", "Bjarne Stroustrup", 2000, BOOK_OTHER); cout << cpp_book; return 0; } | | Всего записей: 132 | Зарегистр. 24-08-2004 | Отправлено: 11:22 23-11-2008 | Исправлено: c0d3r, 11:33 23-11-2008 |
|