Advertisement
desislava_shunina

examples pract

Apr 25th, 2024
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include "MyString.h"
  3.  
  4. class Example {
  5.     int a;
  6.  
  7. public:
  8.     Example(int a) : a(a) {};
  9.     int getA() {
  10.         return a;
  11.     }
  12. };
  13.  
  14. class A {
  15. public:
  16.     int x;
  17. };
  18. class B : /*public*/ A {
  19. public:
  20.     int y;
  21. };
  22.  
  23. void acceptCopyOfA(A obj) {}
  24. void acceptCopyOfB(B obj) {}
  25. void acceptRefOfA(A& ref) {}
  26. void acceptRefOfB(B& ref) {}
  27. void acceptPtrOfA(A* ptr) {}
  28. void acceptPtrOfB(B* ptr) {}
  29. void acceptRvalueRefOfA(A&& ptr) {}
  30. void acceptRvalueRefOfB(B&& ptr) {}
  31.  
  32.  
  33. class User {
  34. public:
  35.     User() {
  36.         std::cout << "User()" << std::endl;
  37.     }
  38.     User(const User& other) {
  39.         std::cout << "CC of User()" << std::endl;
  40.     }
  41.     User(User&& other) noexcept {
  42.         std::cout << "MC of User()" << std::endl;
  43.     }
  44.     User& operator=(const User& other) {
  45.         std::cout << "OP= of User()" << std::endl;
  46.         return *this;
  47.     }
  48.     User&  operator=(User&& other) noexcept {
  49.         std::cout << "MOP= of User()" << std::endl;
  50.         return *this;
  51.     }
  52.     ~User() {
  53.         std::cout << "~User()" << std::endl;
  54.     }
  55. };
  56.  
  57. class PremiumUser : public User {
  58. public:
  59.     PremiumUser() {
  60.         std::cout << "PremiumUser()" << std::endl;
  61.     }
  62.     PremiumUser(const PremiumUser& other) : User(other) {
  63.         std::cout << "CC of PremiumUser()" << std::endl;
  64.     }
  65.     PremiumUser(PremiumUser&& other) noexcept : User(std::move(other)) {
  66.         std::cout << "MC of PremiumUser()" << std::endl;
  67.     }
  68.     PremiumUser& operator=(const PremiumUser& other) {
  69.         User::operator=(other);
  70.         std::cout << "OP= of PremiumUser()" << std::endl;
  71.         return *this;
  72.     }
  73.     PremiumUser& operator=(PremiumUser&& other) noexcept {
  74.         User::operator=(std::move(other));
  75.         std::cout << "MOP= of PremiumUser()" << std::endl;
  76.         return *this;
  77.     }
  78.     ~PremiumUser() {
  79.         std::cout << "~PremiumUser()" << std::endl;
  80.     }
  81. };
  82.  
  83. //???
  84. class Document {
  85. protected:
  86.     MyString content;
  87.  
  88. public:
  89.     Document(const MyString& content) : content(content) {}
  90.  
  91.     void view() {
  92.         std::cout << content << std::endl;
  93.     }
  94. };
  95.  
  96. class CensoredDocument : public Document {
  97.     bool (*pred) (char);
  98. public:
  99.     CensoredDocument(bool (*pred) (char), const MyString& content) : Document(content) , pred(pred) {}
  100.  
  101.     void view() {
  102.         size_t size = content.getSize();
  103.         for (int i = 0; i < size; i++)
  104.         {
  105.             if (pred(content[i]))
  106.                 std::cout << '*';
  107.             else
  108.                 std::cout << content[i];
  109.         }
  110.         std::cout << std::endl;
  111.     }
  112.  
  113. };
  114. int main()
  115. {
  116.     /*Placement new*/
  117.     {
  118.         //allocates memory for 3 Example objects
  119.         Example* ptr = (Example*) new char[3 * sizeof(Example)];
  120.  
  121.         //allocates ex1 at the address of ptr[0]
  122.         Example* ex1 = new (&ptr[0]) Example(1);
  123.  
  124.         //allocates ex2 at the address of ptr[1]
  125.         Example* ex2 = new (&ptr[1]) Example(2);       
  126.  
  127.         std::cout << ex1->getA() << std::endl;
  128.         std::cout << ex2->getA() << std::endl;
  129.  
  130.         //we should always call the destructors explicitly!!!
  131.         ex1->~Example();
  132.         ex2->~Example();
  133.  
  134.         delete[]((char*)ptr);
  135.     }
  136.     //{
  137.     //  /*Example 1*/
  138.     //  A a;
  139.     //  B b;
  140.     //  acceptCopyOfA(a);
  141.     //  acceptCopyOfA(b); //only if b has inherited a as public
  142.     //  acceptCopyOfB(a); //not allowed
  143.     //  acceptCopyOfB(b);
  144.     //  acceptRefOfA(a);
  145.     //  acceptRefOfA(b); //only if b has inherited a as public
  146.     //  acceptRefOfB(a); //not allowed
  147.     //  acceptRefOfB(b);
  148.     //  acceptRvalueRefOfA(std::move(a));
  149.     //  acceptRvalueRefOfA(std::move(b)); //only if b has inherited a as public
  150.     //  acceptRvalueRefOfB(std::move(a)); //not allowed
  151.     //  acceptRvalueRefOfB(std::move(b));
  152.     //  acceptPtrOfA(&a);
  153.     //  acceptPtrOfA(&b); //only if b has inherited a as public
  154.     //  acceptPtrOfB(&a); //not allowed
  155.     //  acceptPtrOfB(&b);
  156.     //}
  157.  
  158.     //{
  159.     //  /*Example 2*/
  160.     //  PremiumUser p1;
  161.     //  PremiumUser p2 = p1;
  162.     //  PremiumUser p3;
  163.     //  p3 = p2;
  164.     //  p3 = std::move(p1);
  165.     //}
  166.  
  167.     //{
  168.     //  /*Example 3*/
  169.     //  CensoredDocument cd([](char ch) {return ch >= '0' && ch <= '9'; }, "Test content123");
  170.     //  Document d = cd;
  171.     //  cd.view();
  172.     //  d.view();
  173.     //}
  174. }
  175.  
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement