Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "MyString.h"
- class Example {
- int a;
- public:
- Example(int a) : a(a) {};
- int getA() {
- return a;
- }
- };
- class A {
- public:
- int x;
- };
- class B : /*public*/ A {
- public:
- int y;
- };
- void acceptCopyOfA(A obj) {}
- void acceptCopyOfB(B obj) {}
- void acceptRefOfA(A& ref) {}
- void acceptRefOfB(B& ref) {}
- void acceptPtrOfA(A* ptr) {}
- void acceptPtrOfB(B* ptr) {}
- void acceptRvalueRefOfA(A&& ptr) {}
- void acceptRvalueRefOfB(B&& ptr) {}
- class User {
- public:
- User() {
- std::cout << "User()" << std::endl;
- }
- User(const User& other) {
- std::cout << "CC of User()" << std::endl;
- }
- User(User&& other) noexcept {
- std::cout << "MC of User()" << std::endl;
- }
- User& operator=(const User& other) {
- std::cout << "OP= of User()" << std::endl;
- return *this;
- }
- User& operator=(User&& other) noexcept {
- std::cout << "MOP= of User()" << std::endl;
- return *this;
- }
- ~User() {
- std::cout << "~User()" << std::endl;
- }
- };
- class PremiumUser : public User {
- public:
- PremiumUser() {
- std::cout << "PremiumUser()" << std::endl;
- }
- PremiumUser(const PremiumUser& other) : User(other) {
- std::cout << "CC of PremiumUser()" << std::endl;
- }
- PremiumUser(PremiumUser&& other) noexcept : User(std::move(other)) {
- std::cout << "MC of PremiumUser()" << std::endl;
- }
- PremiumUser& operator=(const PremiumUser& other) {
- User::operator=(other);
- std::cout << "OP= of PremiumUser()" << std::endl;
- return *this;
- }
- PremiumUser& operator=(PremiumUser&& other) noexcept {
- User::operator=(std::move(other));
- std::cout << "MOP= of PremiumUser()" << std::endl;
- return *this;
- }
- ~PremiumUser() {
- std::cout << "~PremiumUser()" << std::endl;
- }
- };
- //???
- class Document {
- protected:
- MyString content;
- public:
- Document(const MyString& content) : content(content) {}
- void view() {
- std::cout << content << std::endl;
- }
- };
- class CensoredDocument : public Document {
- bool (*pred) (char);
- public:
- CensoredDocument(bool (*pred) (char), const MyString& content) : Document(content) , pred(pred) {}
- void view() {
- size_t size = content.getSize();
- for (int i = 0; i < size; i++)
- {
- if (pred(content[i]))
- std::cout << '*';
- else
- std::cout << content[i];
- }
- std::cout << std::endl;
- }
- };
- int main()
- {
- /*Placement new*/
- {
- //allocates memory for 3 Example objects
- Example* ptr = (Example*) new char[3 * sizeof(Example)];
- //allocates ex1 at the address of ptr[0]
- Example* ex1 = new (&ptr[0]) Example(1);
- //allocates ex2 at the address of ptr[1]
- Example* ex2 = new (&ptr[1]) Example(2);
- std::cout << ex1->getA() << std::endl;
- std::cout << ex2->getA() << std::endl;
- //we should always call the destructors explicitly!!!
- ex1->~Example();
- ex2->~Example();
- delete[]((char*)ptr);
- }
- //{
- // /*Example 1*/
- // A a;
- // B b;
- // acceptCopyOfA(a);
- // acceptCopyOfA(b); //only if b has inherited a as public
- // acceptCopyOfB(a); //not allowed
- // acceptCopyOfB(b);
- // acceptRefOfA(a);
- // acceptRefOfA(b); //only if b has inherited a as public
- // acceptRefOfB(a); //not allowed
- // acceptRefOfB(b);
- // acceptRvalueRefOfA(std::move(a));
- // acceptRvalueRefOfA(std::move(b)); //only if b has inherited a as public
- // acceptRvalueRefOfB(std::move(a)); //not allowed
- // acceptRvalueRefOfB(std::move(b));
- // acceptPtrOfA(&a);
- // acceptPtrOfA(&b); //only if b has inherited a as public
- // acceptPtrOfB(&a); //not allowed
- // acceptPtrOfB(&b);
- //}
- //{
- // /*Example 2*/
- // PremiumUser p1;
- // PremiumUser p2 = p1;
- // PremiumUser p3;
- // p3 = p2;
- // p3 = std::move(p1);
- //}
- //{
- // /*Example 3*/
- // CensoredDocument cd([](char ch) {return ch >= '0' && ch <= '9'; }, "Test content123");
- // Document d = cd;
- // cd.view();
- // d.view();
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement