Advertisement
vallec

Читател2

Dec 15th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // Клас Читател
  6. class Reader {
  7. protected:
  8.     string name;   // Име на читателя
  9.     string number; // Номер на читателя
  10.  
  11. public:
  12.     // Конструктор с параметри
  13.     Reader(string readerName, string readerNumber)
  14.         : name(readerName), number(readerNumber) {}
  15.  
  16.     // Метод за извеждане на информация за читателя
  17.     void printReaderInfo() const {
  18.         cout << "Reader Name: " << name << ", Reader Number: " << number << endl;
  19.     }
  20. };
  21.  
  22. // Клас Библиотека (наследява Reader)
  23. class Library : public Reader {
  24. private:
  25.     string bookName; // Име на книгата
  26.     bool isBorrowed; // Статус на книгата: заета или свободна
  27.  
  28. public:
  29.     // Конструктор с параметри
  30.     Library(string readerName, string readerNumber, string book, bool status)
  31.         : Reader(readerName, readerNumber), bookName(book), isBorrowed(status) {}
  32.  
  33.     // Функция за заемане на книга
  34.     void borrowBook() {
  35.         if (!isBorrowed) {
  36.             isBorrowed = true;
  37.             cout << "The book \"" << bookName << "\" has been borrowed by " << name << "." << endl;
  38.         } else {
  39.             cout << "The book \"" << bookName << "\" is already borrowed." << endl;
  40.         }
  41.     }
  42.  
  43.     // Функция за проверка дали дадена книга е заета
  44.     bool checkBookStatus() const {
  45.         return isBorrowed;
  46.     }
  47.  
  48.     // Функция за извеждане на информация за книгата
  49.     void printBookInfo() const {
  50.         cout << "Book Name: " << bookName
  51.              << ", Status: " << (isBorrowed ? "Borrowed" : "Available") << endl;
  52.     }
  53. };
  54.  
  55. // Главна функция
  56. int main() {
  57.     // Масив от обекти Library
  58.     Library books[] = {
  59.         Library("Ivan Ivanov", "001", "C++ Programming", false),
  60.         Library("Maria Petrova", "002", "Data Structures", false),
  61.         Library("Georgi Georgiev", "003", "Algorithms", true),
  62.         Library("Anna Dimitrova", "004", "Operating Systems", false),
  63.         Library("Petar Petrov", "005", "Computer Networks", false),
  64.     };
  65.  
  66.     // Извеждане на информация за книгите преди заемането
  67.     cout << "Books before borrowing:" << endl;
  68.     for (const Library& book : books) {
  69.         book.printBookInfo();
  70.     }
  71.  
  72.     cout << endl;
  73.  
  74.     // Заемане на първите две книги със статус "свободна"
  75.     cout << "Borrowing available books:" << endl;
  76.     int borrowedCount = 0;
  77.     for (Library& book : books) {
  78.         if (!book.checkBookStatus() && borrowedCount < 2) {
  79.             book.borrowBook();
  80.             borrowedCount++;
  81.         }
  82.     }
  83.  
  84.     cout << endl;
  85.  
  86.     // Извеждане на информация за книгите след заемането
  87.     cout << "Books after borrowing:" << endl;
  88.     for (const Library& book : books) {
  89.         book.printBookInfo();
  90.     }
  91.  
  92.     return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement