Tutorial: 03

 ----------------- Rules.cpp

#include <iostream>


class MyClass {

public:

    int value;


public:

    MyClass(int v) : value(v) {

        std::cout << "Constructor called, value: " << value << std::endl;

    }


    // Destructor for resource cleanup (if your class manages resources)

    ~MyClass() {

        // Resource cleanup logic here

        std::cout << "Destructor called for value: " << value << std::endl;

    }


    // Copy constructor creates a deep copy

    MyClass(const MyClass& other) : value(other.value) {

        std::cout << "Copy constructor called, copied value: " << value << std::endl;

    }


    // Copy assignment operator performs deep copy

    MyClass& operator=(const MyClass& other) {

        if (this != &other) {

            value = other.value;

            std::cout << "Copy assignment operator called, assigned value: " << value << std::endl;

        }

        return *this;

    }


    // Move constructor efficiently transfers ownership (no copying)

    MyClass(MyClass&& other) noexcept : value(std::move(other.value)) {

        other.value = -1; // Mark moved-from object

        std::cout << "Move constructor called, moved value: " << value << std::endl;

    }


    // Move assignment operator efficiently transfers ownership (no copying)

    MyClass& operator=(MyClass&& other) noexcept {

        if (this != &other) {

            value = std::move(other.value);

            other.value = -1;

            std::cout << "Move assignment operator called, moved value: " << value << std::endl;

        }

        return *this;

    }


    // ... other member functions

};


int main() {

    MyClass obj1(42);


    // Copy operations (demonstrate deep copy)

    MyClass obj2(obj1);

    obj2.value = 100;


    MyClass obj3 = obj1;

    obj3.value = 200;


    std::cout << "obj1: " << obj1.value << std::endl;

    std::cout << "obj2: " << obj2.value << std::endl;

    std::cout << "obj3: " << obj3.value << std::endl;


    // Move operations (demonstrate ownership transfer)

    MyClass obj4(std::move(obj1));


    // obj1 becomes invalid after being moved

    obj1.value = 500; // This has no effect


    std::cout << "obj1: " << obj1.value << std::endl; // Potentially undefined behavior

    std::cout << "obj4: " << obj4.value << std::endl;


    return 0;

}


 ----------------- Rules5.cpp

#include <iostream>


class MyClass {

public:

    int value;


public:

    MyClass(int v) : value(v) {

        std::cout << "Constructor called, value: " << value << std::endl;

    }


    // Destructor for resource cleanup (if your class manages resources)

    ~MyClass() {

        // Resource cleanup logic here

        std::cout << "Destructor called for value: " << value << std::endl;

    }


    // Copy constructor creates a deep copy

    MyClass(const MyClass& other) : value(other.value) {

        std::cout << "Copy constructor called, copied value: " << value << std::endl;

    }


    // Copy assignment operator performs deep copy

    MyClass& operator=(const MyClass& other) {

        if (this != &other) {

            value = other.value;

            std::cout << "Copy assignment operator called, assigned value: " << value << std::endl;

        }

        return *this;

    }


    // Move constructor efficiently transfers ownership (no copying)

    MyClass(MyClass&& other) noexcept : value(std::move(other.value)) {

        other.value = -1; // Mark moved-from object

        std::cout << "Move constructor called, moved value: " << value << std::endl;

    }


    // Move assignment operator efficiently transfers ownership (no copying)

    MyClass& operator=(MyClass&& other) noexcept {

        if (this != &other) {

            value = std::move(other.value);

            other.value = -1;

            std::cout << "Move assignment operator called, moved value: " << value << std::endl;

        }

        return *this;

    }


    // ... other member functions

};


int main() {

    MyClass obj1(42);


    // Copy operations (demonstrate deep copy)

    MyClass obj2(obj1);

    obj2.value = 100;


    MyClass obj3 = obj1;

    obj3.value = 200;


    std::cout << "obj1: " << obj1.value << std::endl;

    std::cout << "obj2: " << obj2.value << std::endl;

    std::cout << "obj3: " << obj3.value << std::endl;


    // Move operations (demonstrate ownership transfer)

    MyClass obj4(std::move(obj1));


    // obj1 becomes invalid after being moved

    obj1.value = 500; // This has no effect


    std::cout << "obj1: " << obj1.value << std::endl; // Potentially undefined behavior

    std::cout << "obj4: " << obj4.value << std::endl;


    return 0;

}


 ----------------- Rules0.cpp

#include <iostream>

#include <string>


// A simple class that doesn't manage any resources directly

class Person {

private:

    std::string name;

    int age;


public:

    // Constructor

    Person(const std::string& name, int age) : name(name), age(age) {}


    // Method to display person's information

    void Display() const {

        std::cout << "Name: " << name << ", Age: " << age << std::endl;

    }

};


int main() {

    // Creating objects of class Person

    Person person1("Alice", 30);

    Person person2("Bob", 25);


    // Displaying person information

    person1.Display();

    person2.Display();


    return 0;

}


No comments:

Post a Comment

Fell free to write your query in comment. Your Comments will be fully encouraged.