Advertisement
dariahinz

POB

Mar 13th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. main
  2.  
  3. #include <iostream>
  4. #include "book.h"
  5. #include <string>
  6. int main() {
  7.     book v4;
  8.     book v6;
  9.     book v5;
  10.     v6.SetTitle("Missing You");
  11.     v6.SetAuthor("Missin you so much my baby");
  12.     v5.SetTitle(move(v6.GetTitle()));
  13.     cout << v5 << endl;
  14.     cout << v6 << endl;
  15.     v4 = v6;
  16.     cout << v4 << endl;
  17.     system("pause");
  18.  
  19.     return 0;
  20. }
  21.  
  22.  
  23. .cpp
  24.  
  25.  
  26. #include "book.h"
  27. #include <iostream>
  28. #include <iomanip>
  29. #include <functional>
  30. using namespace std;
  31.  
  32.  
  33. book::book()
  34. {
  35.     cout << "Konstruktor bez parametru" <<  endl;
  36. }
  37.  
  38. book::book(string&& author, string&& title)
  39.     :author(author), title(title)
  40. {
  41.     cout << "Konstruktor z r-referencją" <<  endl;
  42. }
  43.  
  44. book::book(const string& author, const string& title)
  45.     : author(author), title(title)
  46. {
  47.     cout << "Konstruktor z l-referencją" << endl;
  48. }
  49.  
  50. string book::GetAuthor()const
  51. {
  52.     return this->author;
  53. }
  54.  
  55. string book::GetTitle()const
  56. {
  57.     return this->title;
  58. }
  59.  
  60. void book::SetAuthor(const string& author)
  61. {
  62.     this->author = author;
  63.  
  64. }
  65.  
  66. void book::SetAuthor(string&& author)
  67. {
  68.     this->author = author;
  69. }
  70.  
  71. void book::SetTitle(const string& title)
  72. {
  73.     this->title = title;
  74. }
  75.  
  76. void book::SetTitle(string&& title)
  77. {
  78.     this->title = title;
  79. }
  80.  
  81. book::book(const book& other)
  82.     :author(other.author), title(other.title)
  83. {
  84.     cout << "kopiowanie" << endl;
  85.     author = other.author;
  86.     title = other.title;
  87. }
  88.  
  89. book::book(book&& other)
  90.     : author(other.author), title(other.title)
  91. {
  92.     cout << "Przenoszenie konstruktora z r-referencją" <<  endl;
  93.  
  94.     author = other.author;
  95.     title = other.title;
  96.     other.author = nullptr;
  97.     other.title = nullptr;
  98.    
  99. }
  100.  
  101. book& book::operator=(const book& right) {
  102.  
  103.     cout << "Operator przypisania z l-referencją" << endl;
  104.  
  105.     book tmp = right;
  106.  
  107.     swap(author, tmp.author);
  108.     swap(title, tmp.title);
  109.  
  110.     return *this;
  111. }
  112.  
  113. book& book::operator=(book&& right) {
  114.     cout << "Prenoszący operator przypisania z r-referencją" <<  endl;
  115.  
  116.     swap(author, right.author);
  117.     swap(title, right.title);
  118.     cout << "Prenoszący operator przypisania z r-referencją" << endl;
  119.     return *this;
  120. }
  121.  
  122. ostream& operator<<(ostream& stream, const book& book)
  123. {
  124.     stream << "Author : ";
  125.    
  126.     stream.fill(' ');
  127.     stream << book.author;
  128.     stream << setw(15);
  129.     stream.fill(' ');
  130.     stream << "Title : ";
  131.  
  132.     stream.fill(' ');
  133.     stream << book.title;
  134.    
  135.     stream.fill(' ');
  136.     stream << endl;
  137.  
  138.     return stream;
  139. }
  140.  
  141. book::~book()
  142. {
  143.     cout << "Destruktor " << endl;
  144.    
  145. }
  146.  
  147.  
  148.  
  149. .h
  150.  
  151.  
  152. #pragma once
  153. #include <string>
  154. #include <iostream>
  155. using namespace  std;
  156.  
  157. class book
  158. {
  159. private:
  160.     string author;
  161.     string title;
  162. public:
  163.     book();
  164.     book(string&& author, string&& title);
  165.     book(const string& author, const string& title);
  166.     string GetAuthor() const;
  167.     string GetTitle() const;
  168.     void SetAuthor(const string& author);
  169.     void SetAuthor(string&& author);
  170.     void SetTitle(const string& title);
  171.     void SetTitle(string&& title);
  172.     book(const book& other);
  173.     book(book&& other);
  174.     book& operator=(const book& right);
  175.     book& operator=(book&& right);
  176.     friend std::ostream& operator<<(std::ostream &stream, const book& book);
  177.     ~book();
  178.  
  179.  
  180. };
  181.  
  182.  
  183. #pragma once
  184. #include "book.h"
  185. #include <initializer_list>
  186. class Library {
  187.     /* ...................... */
  188. public:
  189.     Library();
  190.     Library(std::initializer_list<book> list);
  191.     Library(const Library& orig);
  192.     Library(Library&& orig);
  193.     Library& operator=(const Library& ritght);
  194.     Library& operator=(Library&& ritght);
  195.     book& operator[](std::size_t index);
  196.     const book& operator[](std::size_t index) const;
  197.     std::size_t GetSize() const;
  198.     ~Library();
  199.  
  200.  
  201.  
  202.     Library(std::initializer_list<double> initList)
  203.         : size{ initList.size() }, array{new double[initList.size()]}
  204.     {
  205.         std::size_t i = 0;
  206.         for (double v : initList)
  207.         {
  208.             array[i] = v;
  209.             i++;
  210.         }
  211.     }
  212.  
  213.  
  214.     size_t GetSize() const {
  215.         return size;
  216.     }
  217.     ~Library()
  218.     {
  219.         if (array != nullptr)
  220.             delete[] array;
  221.     }
  222.  
  223. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement