Advertisement
NickAndNick

Полиморфизм

Aug 20th, 2020 (edited)
1,644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. class Edition {
  6. public:
  7.     void name(const string& value) { name_ = value; }
  8.     void author(const string& value) { author_ = value; }
  9.     string name()const { return name_; }
  10.     string author()const { return author_; }
  11.     virtual void show()const = 0;
  12. private:
  13.     string name_;
  14.     string author_;
  15. };
  16. class Book : virtual public Edition {
  17. public:
  18.     void publishing(unsigned short value) { publishing_ = value; }
  19.     void publishing_house(const string& value) { publishing_house_ = value; }
  20.     unsigned short publishing()const { return publishing_; }
  21.     string publishing_house()const { return publishing_house_; }
  22.     void show()const override {
  23.         cout
  24.             << "Автор: " << author() << '\n'
  25.             << "Название книги: " << name() << '\n'
  26.             << "Издательство: " << publishing_house() << '\n'
  27.             << "Год издания: " << publishing() << '\n';
  28.     }
  29. private:
  30.     unsigned short publishing_;
  31.     string publishing_house_;
  32. };
  33. class Article : virtual public Edition {
  34. public:
  35.     void number(int value) { number_ = value; }
  36.     void year(int value) { year_ = value; }
  37.     void title(const string& value) { title_ = value; }
  38.     int number()const { return number_; }
  39.     int year()const { return year_; }
  40.     string title()const { return title_; }
  41.     void show()const override {
  42.         cout
  43.             << "Автор: " << author() << '\n'
  44.             << "Название статьи: " << name() << '\n'
  45.             << "Название журнала: " << title() << '\n'
  46.             << "Номер журнала: " << number() << '\n'
  47.             << "Год публикации: " << year() << '\n';
  48.     }
  49. private:
  50.     int number_;
  51.     int year_;
  52.     string title_;
  53. };
  54. class Catalog {
  55. public:
  56.     explicit Catalog(const size_t limit) : limit_(limit) {}
  57.     bool add(Edition* ptr) {
  58.         if (box_.size() < limit_) box_.push_back(ptr);
  59.         return box_.size() < limit_;
  60.     }
  61.     vector<Edition*> find_author(const string& author)const {
  62.         vector<Edition*> box;
  63.         for (auto item : box_) if (item->author() == author) box.push_back(item);
  64.         return box;
  65.     }
  66.     void show()const {
  67.         for (auto item : box_) {
  68.             item->show();
  69.             cout.put('\n');
  70.         }
  71.     }
  72. private:
  73.     size_t limit_;
  74.     vector<Edition*> box_;
  75. };
  76. Book input_book() {
  77.     Book book;
  78.     cout << "Имя автора книги: ";
  79.     string author;
  80.     getline(cin, author);
  81.     book.author(author);
  82.     cout << "Название книги: ";
  83.     string name;
  84.     getline(cin, name);
  85.     book.name(name);
  86.     cout << "Издательство: ";
  87.     string publishing_house;
  88.     getline(cin, publishing_house);
  89.     book.publishing_house(publishing_house);
  90.     cout << "Год издания: ";
  91.     unsigned short publishing;
  92.     cin >> publishing;
  93.     book.publishing(publishing);
  94.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  95.     system("cls");
  96.     return book;
  97. }
  98. Article input_article() {
  99.     Article article;
  100.     cout << "Имя автора статьи: ";
  101.     string author;
  102.     getline(cin, author);
  103.     article.author(author);
  104.     cout << "Название статьи: ";
  105.     string name;
  106.     getline(cin, name);
  107.     article.name(name);
  108.     cout << "Название журнала: ";
  109.     string title;
  110.     getline(cin, title);
  111.     article.title(title);
  112.     cout << "Номер журнала: ";
  113.     int number;
  114.     cin >> number;
  115.     article.number(number);
  116.     cout << "Год издания: ";
  117.     int year;
  118.     cin >> year;
  119.     article.number(year);
  120.     article.year(year);
  121.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  122.     system("cls");
  123.     return article;
  124. }
  125. int main() {
  126.     system("chcp 1251 > nul");
  127.     const auto limit = 5U;
  128.     Catalog catalog(limit);
  129.     auto b1 = input_book();
  130.     catalog.add(&b1);
  131.     auto a1 = input_article();
  132.     catalog.add(&a1);
  133.     auto a2 = input_article();
  134.     catalog.add(&a2);
  135.     auto b2 = input_book();
  136.     catalog.add(&b2);
  137.     auto a3 = input_article();
  138.     catalog.add(&a3);
  139.     catalog.show();
  140.     cout << "Введите имя автора для поиска: ";
  141.     string author;
  142.     getline(cin, author);
  143.     cout.put('\n');
  144.     auto box = catalog.find_author(author);
  145.     if (box.size() > 0) {
  146.         for (auto item : box) {
  147.             item->show();
  148.             cout.put('\n');
  149.         }
  150.     }
  151.     else cout << "Не найден!\n";
  152.     system("pause > nul");
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement