the class tag but prefixed with a ~(tilde) . Destructor function also return nothing and it does not associated with anydata type. . .
Need of Destructors The purpose of the destructor is to free the resources that the object may have acquired during its lifetime. A destructor function removes the memory of an object which was allocated by the constructor at the time of creating a object. . .
Declaration and Definition A destructor is a special member function that is called when the lifetime of an object ends and destroys the object constructed by the constructor. Normally declared under public. #include<iostream> using namespace std; class simple private: int a, b; public: simple() a= ; b= ; cout<< "\n Constructor of class-simple "; void getdata() cout<<"\n Enter values for a and b "; cin>>a>>b; void putdata() cout<<"\nThe two integers are .. "; cout<<<<a<<'\t'<< b<<endl; cout<<"\n The sum = "<<a+b; ~simple() { cout<<”\n Destructor is executed ”;} }; int main() simple s; s.getdata(); s.putdata(); return ; Output: Constructor of class-simple Enter values for a and b The two integers are ..
The sum = Destructor is executed Illustration14. To illustrate destructor function in a class . Characteristics of Destructors • The destructor has the same name as that class prefixed by the tilde character ‘~’. • The destructor cannot have arguments • It has no return type • Destructors cannot be overloaded • In the absence of user defined destructor, it is generated by the compiler • The destructor is executed automatically when the control reaches the end of class scope to destroy the object • They cannot be inherited Chapter Page - - • A class binds data and associated functions together.
• A class in C++ makes a user defined data type using which objects of this type can be created. • While declaring a class data members , member functions ,access specifiers and class tag name are given. • The member functions of a class can either be defined within the class (inline) definition or outside the class definition.