Advertisement
Garey

Danny_OOP_Izpit

Jan 18th, 2019
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. // ConsoleApplication9.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7. #include <string>
  8. #include <iterator>
  9. #include <algorithm>
  10.  
  11. using namespace std;
  12.  
  13. class Book {
  14. private:
  15.     string name;
  16.     string description;
  17. public:
  18.     /*! Constructors */
  19.     Book() : name(string()), description(string()) {};
  20.     Book(string name, string description) : name(name), description(description) {};
  21.  
  22.     /*! Destructor */
  23.     virtual ~Book() {};
  24.  
  25.     /*! Get */
  26.     auto getName() {
  27.         return this->name;
  28.     }
  29.  
  30.     auto getDescription() {
  31.         return this->description;
  32.     }
  33.  
  34.     /*! Set */
  35.     auto setName(string name) {
  36.         this->name = name;
  37.     }
  38.  
  39.     auto setDescription(string description) {
  40.         this->description = description;
  41.     }
  42.  
  43.     /*! Operators */
  44.     friend istream& operator>>(istream& input, Book& obj) {
  45.        
  46.         input >> obj.name >> obj.description;
  47.  
  48.         return input;
  49.     }
  50.  
  51.     friend ostream& operator<<(ostream& output, const Book& obj) {
  52.        
  53.         output << "Name: " << obj.name << endl;
  54.         output << "Description: " << obj.description << endl << endl;
  55.  
  56.         return output;
  57.     }
  58. };
  59.  
  60. class Library {
  61. private:
  62.     string title;
  63.     vector<Book> books;
  64.  
  65. public:
  66.     /*! Constructors */
  67.     Library() : books(vector<Book>()) {};
  68.     Library(const string path) {
  69.         fstream file(path);
  70.  
  71.         file >> this->title;
  72.         copy(istream_iterator<Book>(file), istream_iterator<Book>(), back_inserter(this->books));
  73.        
  74.     }
  75.     Library(const Library& obj) : title(obj.title), books(obj.books) {};
  76.  
  77.     /*! Destructor */
  78.     virtual ~Library() {};
  79.  
  80.     /*! Get */
  81.     auto getBooks() {
  82.         return this->books;
  83.     }
  84.  
  85.     auto getTitle() {
  86.         return this->title;
  87.     }
  88.  
  89.     /*! Operators */
  90.     friend istream& operator>>(istream& input, Library& obj) {
  91.         input >> obj.title;
  92.  
  93.         copy(istream_iterator<Book>(input), istream_iterator<Book>(), back_inserter(obj.books));
  94.  
  95.         return input;
  96.     }
  97.  
  98.     friend ostream& operator<<(ostream& output, const Library& obj) {
  99.         output << "Title: " << obj.title << endl << endl;
  100.  
  101.         for (auto book : obj.books) {
  102.             output << book;
  103.         }
  104.  
  105.         return output;
  106.     }
  107. };
  108.  
  109. /*! Program Entry Point */
  110. int main()
  111. {
  112.     /*!
  113.     *
  114.     * File contents
  115.     *
  116.     * Example
  117.     * Book1 Description
  118.     * Book2 Test
  119.     *
  120.     */
  121.     Library test("test.txt");
  122.  
  123.     cout << test;
  124.  
  125.     return 0;
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement