Ad Code

Responsive Advertisement

What is Constructor and Destructor ? How Constructor and Destructor used for OOPs ?


Important aspects of OOP in C++ is constructors and destructors and here you will get all of this in detail.

Constructors in C++:


Constructors are special member functions in C++ that are called automatically when an object of a class is created. They are used to initialise the object's data members and allocate any resources that the object requires. A constructor has the same name as the class, and it can be overloaded to take different arguments. A class can have multiple constructors with different signatures, and each constructor can have its own implementation.


Here is an example of a simple class with a constructor:


class Person {
    public:
    Person() {
        std::cout << "Person object created" << std::endl;
    }
};
int main() {
    Person person;  // create a Person object
    return 0;
}


In the above example, we have defined a class called Person with a default constructor. The constructor prints a message when the object is created. In the main() function, we create an object of the Person class, which calls the constructor and prints the message "Person object created" to the console.


Destructors in C++:


Destructors are special member functions in C++ that are called automatically when an object of a class is destroyed. They are used to clean up any resources that the object has acquired, such as memory, file handles, or network connections. A destructor has the same name as the class, preceded by a tilde (~), and it does not take any arguments. A class can have only one destructor, and it cannot be overloaded.


Here is an example of a class with a destructor:


class Person {
    public:

    Person() {
        std::cout << "Person object created" << std::endl;
    }

    ~Person() {
        std::cout << "Person object destroyed" << std::endl;
    }
};

int main() {
    {
        Person person;  // create a Person object
    }  // the Person object is destroyed when it goes out of scope
    return 0;
}



In the above example, we have defined a class called Person with a constructor and a destructor. The constructor prints a message when the object is created, and the destructor prints a message when the object is destroyed. In the main() function, we create an object of the Person class inside a code block. When the code block ends, the object goes out of scope and the destructor is called, which prints the message "Person object destroyed" to the console.


Constructors and Initialisation:


Constructors are also used to initialise the object's data members. In C++, data members of a class are not initialised by default, and their initial values are undefined. This can cause problems if the object is used before its data members are initialised. Constructors can be used to ensure that the data members are initialised to a specific value when the object is created.


Here is an example of a class with a constructor that initialises the data members:


#include <iostream>
class Person {
    public:
    // Constructor that initializes the data members
    Person(std::string name, int age) {
        this->name = name;
        this->age = age;
        std::cout << "Person " << name << " created." << std::endl;
    }
    // Member function to print the data members
    void print() {
        std::cout << "Name: " << name << std::endl;
        std::cout << "Age: " << age << std::endl;
    }
    private:
    std::string name;
    int age;
};

int main() {
  // Create a Person object using the constructor
  Person p1("John", 30);
  // Call the print function to display the data members
  p1.print();
  return 0;
}


In this example, the Person class has a constructor that takes two parameters: name and age. The constructor initializes the private data members name and age using the this pointer, which points to the object being created. It also prints a message to the console to indicate that a Person object has been created.


In the main function, we create a Person object named p1 using the constructor and passing the values "John" and 30 as arguments. We then call the print member function to display the name and age of the object.


When we run this program, we get the following output:


Person John created.
Name: John
Age: 30

Here, we can see that the constructor was called when the Person object was created, and it initialized the name and age data members. The print function was then called to display the data members.


Constructors can also be overloaded to provide different ways of initializing objects. In the example above, we provided a constructor that takes two parameters. We could also provide a constructor that takes only one parameter, such as the name of the person, and sets the age to a default value. This would allow us to create Person objects with different initializations depending on the number and type of parameters passed to the constructor.


In summary, constructors and destructors are important features of object-oriented programming in C++. Constructors are used to initialise object data members, while destructors are used to clean up any resources that an object has acquired during its lifetime. Proper use of constructors and destructors can help to prevent memory leaks and other issues caused by improperly managed resources.







Post a Comment

0 Comments

Close Menu