Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <memory>
- #include <vector>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- template<class T>
- class SmartPointer
- {
- public:
- T *id;
- SmartPointer(T *test)
- {
- id=test;
- cout<<"Nowy obiekt o id "<<endl;
- }
- ~SmartPointer()
- {
- cout<<"Destruktor obiektu"<<endl;
- }
- T* operator ->()
- {
- return id;
- }
- T& operator &()
- {
- return *id;
- }
- };
- class Triangle
- {
- public:
- float x,y,z;
- Triangle(float x1,float y1,float z1)
- {
- cout<<"Nowy obiekt triangle"<<endl;
- this->x=x1;
- this->y=y1;
- this->z=z1;
- }
- float oblicz(unique_ptr<Triangle> &ptr)
- {
- return ptr->x+ptr->y+ptr->z;
- }
- };
- class Triangle2
- {
- public:
- float x,y,z;
- Triangle2(float x1,float y1,float z1)
- {
- cout<<"Nowy obiekt triangle2"<<endl;
- this->x=x1;
- this->y=y1;
- this->z=z1;
- }
- float oblicz(shared_ptr<Triangle2> &ptr)
- {
- return ptr->x+ptr->y+ptr->z;
- }
- };
- class WordBag
- {
- private:
- vector<unique_ptr<string>> vc;
- public:
- void add(unique_ptr<string> item)
- {
- vc.push_back(move(item));
- }
- unique_ptr<string> &take()
- {
- return vc.at(rand()%vc.size());
- }
- };
- int main()
- {
- //2
- unique_ptr<Triangle>ptr(new Triangle(4,5,6));
- cout<<ptr->oblicz(ptr)<<endl<<endl;
- //3
- srand(time(0));
- unique_ptr<string> wskaznik(new string("Test"));
- unique_ptr<string> wskaznik2(new string("Tescik"));
- unique_ptr<string> wskaznik3(new string("Test123"));
- WordBag wb=WordBag();
- wb.add(move(wskaznik));
- wb.add(move(wskaznik2));
- wb.add(move(wskaznik3));
- cout<<*wb.take()<<endl<<endl;
- //4
- shared_ptr<Triangle2>Xptr(new Triangle2(5,10,10));
- shared_ptr<Triangle2>Xptr1(Xptr);
- shared_ptr<Triangle2>Xptr2(Xptr1);
- cout<<Xptr2->oblicz(Xptr1)<<endl<<endl;
- return 0;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement