Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. //ZADANIE 1 i
  2.  
  3.  
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. template<class T>
  10. class SmartPointer{
  11. public:
  12. T *id;
  13. SmartPointer(T *cos) {
  14. id = cos;
  15. cout<<"Nowy obiekt o id "<<endl;
  16. }
  17. ~SmartPointer() {
  18. cout<<"Destruktor obiektu"<<endl;
  19. }
  20. T* operator -> () {
  21. return id;
  22. }
  23. T& operator & () {
  24. return *id;
  25. }
  26. };
  27.  
  28. class Triangle {
  29. public:
  30. float x,y,z;
  31. float oblicz(unique_ptr<Triangle> &ptr) {
  32. return ptr->x + ptr->y + ptr->z;
  33. }
  34. Triangle(float x1, float y1, float z1) {
  35. cout<<"Nowy obiekt triangle"<<endl;
  36. x = x1;
  37. y = y1;
  38. z = z1;
  39. }
  40. };
  41.  
  42.  
  43. int main()
  44. {
  45. /* unique_ptr<Triangle> ptr(new Triangle);
  46. ptr->x = 4;
  47. ptr->y = 2;
  48. ptr->z = 3;
  49. cout<<ptr->oblicz(ptr)<<endl;*/
  50.  
  51. SmartPointer<Triangle> sp (new Triangle(3,2,2));
  52.  
  53. cout<<sp->x<<endl;
  54. return 0;
  55. }
  56.  
  57. //zadanie 2
  58. #include <iostream>
  59.  
  60. using namespace std;
  61.  
  62.  
  63.  
  64. class Triangle {
  65. public:
  66. float x,y,z;
  67. float oblicz(unique_ptr<Triangle> &ptr) {
  68. return ptr->x + ptr->y + ptr->z;
  69. }
  70. Triangle(float x1, float y1, float z1) {
  71. cout<<"Nowy obiekt triangle"<<endl;
  72. x = x1;
  73. y = y1;
  74. z = z1;
  75. }
  76. };
  77.  
  78.  
  79. int main()
  80. {
  81.  
  82. unique_ptr<Triangle> ptr (new Triangle(2,3,4));
  83.  
  84. cout<<ptr->x<<endl;
  85. return 0;
  86. }
  87.  
  88.  
  89.  
  90. //zad 3
  91. #include <iostream>
  92. #include <vector>
  93. #include <stdlib.h>
  94. #include <time.h>
  95.  
  96. using namespace std;
  97.  
  98. class WordBag {
  99. private:
  100. vector<unique_ptr<string>> vc;
  101. public:
  102. void add(unique_ptr<string> cos) {
  103. vc.push_back(move(cos));
  104. }
  105. unique_ptr<string> &take() {
  106. return vc.at(rand()%vc.size());
  107. }
  108. };
  109.  
  110. int main()
  111. {
  112. srand(time(0));
  113. unique_ptr<string> ptr (new string("cos"));
  114. unique_ptr<string> ptr2 (new string("cosik"));
  115. unique_ptr<string> ptr3 (new string("cos2"));
  116.  
  117.  
  118. WordBag wb = WordBag();
  119. wb.add(move(ptr));
  120. wb.add(move(ptr2));
  121. wb.add(move(ptr3));
  122. cout<<*wb.take()<<endl;
  123.  
  124.  
  125.  
  126. return 0;
  127. }
  128. //ZADANIE 4
  129.  
  130. #include <iostream>
  131.  
  132. using namespace std;
  133.  
  134.  
  135.  
  136. class Triangle {
  137. public:
  138. float x,y,z;
  139. float oblicz(shared_ptr<Triangle> &ptr) {
  140. return ptr->x + ptr->y + ptr->z;
  141. }
  142. Triangle(float x1, float y1, float z1) {
  143. cout<<"Nowy obiekt triangle"<<endl;
  144. x = x1;
  145. y = y1;
  146. z = z1;
  147. }
  148. };
  149.  
  150.  
  151. int main()
  152. {
  153.  
  154. shared_ptr<Triangle> ptr1 (new Triangle(2,3,4));
  155. shared_ptr<Triangle> ptr2 (ptr1);
  156. shared_ptr<Triangle> ptr3 (ptr2);
  157.  
  158. cout<<ptr3->x<<endl;
  159. return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement