~Time()
2. It doesn't return any type.
3. It is called when objects are destroyed to release all allocated resources.
4. There is just one destructor in a class, it can not be overloaded.
// Definition of class CreateAndDestroy. class CreateAndDestroy { public: CreateAndDestroy( int value) // constructor { data = value; cout << "Object " << data << " constructor"; } ~CreateAndDestroy() // destructor { cout << "Object " << data << " destructor " << endl; } } private: int data; }; void create( void ); // prototype CreateAndDestroy first( 1 ); // global object int main() { cout << " (global created before main)" << endl; CreateAndDestroy second( 2 ); // local object cout << " (local in main)" << endl; create(); // call function to create objects CreateAndDestroy fourth( 4 ); // local object cout << " (local in main)" << endl; return 0; } // Function to create objects void create( void ) { CreateAndDestroy fifth( 5 ); cout << " (local in create)" << endl; CreateAndDestroy seventh( 7 ); cout << " (local in create)" << endl; }
Object 1 constructor (global created before main) Object 2 constructor (local in main) Object 5 constructor (local in create) Object 7 constructor (local in create) Object 7 destructor Object 5 destructor Object 4 constructor (local in main) Object 4 destructor Object 2 destructor Object 1 destructorNote: You can find the full source code of this example in code.zip file.