Constructor:-
A constructor is a special member function for automatic initialisation of an object. Has the same name as the class it belongs to. It can be declared and defined within the class, or can be declared within the class and defined outside the class. It has no return type is used for constructors.
Destructor:-
A destructor is a member function that is called automatically when an object is destroyed. It cannot be directly called from the class. The compiler itself generates a call to a destructor when an object expires. Same name as the class but with a tilde (~) before the class name. Destructors have no return type and they take no arguments.
Destructor:-
A destructor is a member function that is called automatically when an object is destroyed. It cannot be directly called from the class. The compiler itself generates a call to a destructor when an object expires. Same name as the class but with a tilde (~) before the class name. Destructors have no return type and they take no arguments.
Suitable example:-
class String
{
char *str;
public:
string(char *s)
{
int length = strlen(s);
str = new char[length+1]; // constructor
strcpy(str,s);
}
~ String( ) {delete[ ] str;} //destructor
};