Explain the characteristics of a constructor How does it dif
Explain the characteristics of a constructor. How does it differ from a destructor? Give a unique example of an object-oriented language in which destructor is used and how it is used.
Solution
Solution:
Characteristics of constructor
A constructor is a special member function. The work of the constructor is to initialize the objects of its class. So the constructor constructs the value of the data members of the class.
1. The constructor have the same that of the class where they belongs to.
2. The constructor is used to construct the value of the data members of the class..
3. Constructor can have been default values or overloaded.
4. Constructor does not have any return type nor the void.
5. The constructor without arguments is called default constructor.
Characteristics of Destructor
1. The destructor has the same that of the class where they belongs to preceded by tilde (~) symbol.
2. The destructor is used to destroy the objects that have been created by a constructor.
3. Destructor cannot be default values or overloaded.
4. Destructor does not have any return type nor the void
5. The destructor does not have any arguments.
Example for Destructor
#include <iostream>
#include <iostream>
using namespace std;
class myclass
{
int a,b;
public:
myclass();
~myclass();
void show();
};
myclass::myclass()
{
cout << \"In constructor\ \";
a = 30;
b = 20;
}
myclass::~myclass()
{
cout << \"Destructing...\ \";
}
void myclass::show()
{
cout << \"A =\" << a << endl << \"B =\" << b << endl;
cout << \"product is \" << a*b << endl;
}
int main()
{
myclass ob;
ob.show();
return 0;
}

