Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ZADANIE 1 i
- #include <iostream>
- using namespace std;
- template<class T>
- class SmartPointer{
- public:
- T *id;
- SmartPointer(T *cos) {
- id = cos;
- 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;
- float oblicz(unique_ptr<Triangle> &ptr) {
- return ptr->x + ptr->y + ptr->z;
- }
- Triangle(float x1, float y1, float z1) {
- cout<<"Nowy obiekt triangle"<<endl;
- x = x1;
- y = y1;
- z = z1;
- }
- };
- int main()
- {
- /* unique_ptr<Triangle> ptr(new Triangle);
- ptr->x = 4;
- ptr->y = 2;
- ptr->z = 3;
- cout<<ptr->oblicz(ptr)<<endl;*/
- SmartPointer<Triangle> sp (new Triangle(3,2,2));
- cout<<sp->x<<endl;
- return 0;
- }
- //zadanie 2
- #include <iostream>
- using namespace std;
- class Triangle {
- public:
- float x,y,z;
- float oblicz(unique_ptr<Triangle> &ptr) {
- return ptr->x + ptr->y + ptr->z;
- }
- Triangle(float x1, float y1, float z1) {
- cout<<"Nowy obiekt triangle"<<endl;
- x = x1;
- y = y1;
- z = z1;
- }
- };
- int main()
- {
- unique_ptr<Triangle> ptr (new Triangle(2,3,4));
- cout<<ptr->x<<endl;
- return 0;
- }
- //zad 3
- #include <iostream>
- #include <vector>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- class WordBag {
- private:
- vector<unique_ptr<string>> vc;
- public:
- void add(unique_ptr<string> cos) {
- vc.push_back(move(cos));
- }
- unique_ptr<string> &take() {
- return vc.at(rand()%vc.size());
- }
- };
- int main()
- {
- srand(time(0));
- unique_ptr<string> ptr (new string("cos"));
- unique_ptr<string> ptr2 (new string("cosik"));
- unique_ptr<string> ptr3 (new string("cos2"));
- WordBag wb = WordBag();
- wb.add(move(ptr));
- wb.add(move(ptr2));
- wb.add(move(ptr3));
- cout<<*wb.take()<<endl;
- return 0;
- }
- //ZADANIE 4
- #include <iostream>
- using namespace std;
- class Triangle {
- public:
- float x,y,z;
- float oblicz(shared_ptr<Triangle> &ptr) {
- return ptr->x + ptr->y + ptr->z;
- }
- Triangle(float x1, float y1, float z1) {
- cout<<"Nowy obiekt triangle"<<endl;
- x = x1;
- y = y1;
- z = z1;
- }
- };
- int main()
- {
- shared_ptr<Triangle> ptr1 (new Triangle(2,3,4));
- shared_ptr<Triangle> ptr2 (ptr1);
- shared_ptr<Triangle> ptr3 (ptr2);
- cout<<ptr3->x<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement