1. Default Construtor

#include <iostream>

class code{
    public:
        code(){//constructor
            std::cout<<"hello"<<std::endl;
        }
        ~code(){//distructor
            std::cout<<"bye"<<std::endl;
        }
};

int main() {
    code a;//if declared then both cons and dist will be called
}

out:
hello
bye

2. perameterized constructor

#include <iostream>

class code{
    private:
        int side;
    public:
        code(int x){
            side = x;
        }
        int getdata(){
            return side;
        }

        ~code(){
            static int a = 1;
            std::cout<<"distructor: "<< a <<std::endl;
            a++;
        }
};

int main(){
    code a(1);
    code b(2);
    code c(3);

    std::cout<<a.getdata()<<std::endl;
    std::cout<<b.getdata()<<std::endl;
    std::cout<<c.getdata()<<std::endl;
    return 0;
}

out:
1
2
3
distructor: 1
distructor: 2
distructor: 3

3. Copy Constuctor

#include <iostream>

class MyClass {
private:
    int data;

public:
    MyClass(int value) : data(value) {
        std::cout << "Constructor called with value = " << data << std::endl;
    }

    // Copy Constructor
    MyClass(const MyClass& other) : data(other.data) {
        std::cout << "Copy Constructor called with value = " << data << std::endl;
    }

    ~MyClass() {
        std::cout << "Destructor called with value = " << data << std::endl;
    }
};

int main() {
    MyClass obj1(42);        // Calls Constructor
    MyClass obj2(obj1);      // Calls Copy Constructor
    MyClass obj3(100);       // Calls Constructor

    return 0;                // Calls Destructor for obj3, obj2, and obj1
}


mOVE cONSTRUCTOR

#include <iostream>

class MyInt {
private:
    int* data;

public:
    // Constructor
    MyInt(int value) : data(new int(value)) {
        std::cout << "Constructor called" << std::endl;
    }

    // Destructor
    ~MyInt() {
        delete data;
        std::cout << "Destructor called" << std::endl;
    }

    // Move Constructor
    MyInt(MyInt&& other) noexcept : data(other.data) {
        other.data = nullptr;  // Set the source's pointer to nullptr
        std::cout << "Move constructor called" << std::endl;
    }

    // Function to print the value
    void print() const {
        if (data) {
            std::cout << "Value: " << *data << std::endl;
        } else {
            std::cout << "No data" << std::endl;
        }
    }
};

int main() {
    MyInt a(10);            // Constructor called
    MyInt b = std::move(a); // Move constructor called

    std::cout << "a: ";
    a.print();              // Should print "No data"
    std::cout << "b: ";
    b.print();              // Should print "Value: 10"

    return 0;
}

Explicit Constructor

#include <iostream>

class MyClass {
private:
    int value;

public:
    // Explicit Constructor
    explicit MyClass(int v) : value(v) {
        std::cout << "Explicit constructor called" << std::endl;
    }

    void print() const {
        std::cout << "Value: " << value << std::endl;
    }
};

void display(const MyClass& obj) {
    obj.print();
}

int main() {
    MyClass obj1(10);  // Explicit constructor called
    obj1.print();

    // MyClass obj2 = 20; // Error: no implicit conversion because of explicit constructor

    MyClass obj3 = MyClass(30); // Works fine with explicit constructor
    obj3.print();

    display(obj1);   // Works fine, explicit constructor is not an issue here

    // display(40);  // Error: No implicit conversion, cannot pass int directly

    return 0;
}



Specialized constructors in C++ are constructors that have specific purposes beyond the general object creation and initialization.
    These include constructors like conversion constructors, delegating constructors, and copy constructors. Let's look at examples 
for each type of specialized constructor.

1. Conversion Constructor

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: