Thursday, 19 January 2012

Program Of Constructor & Destructor In C++

              #include<iostream.h>
#include<conio.h>
class add
{
public:
add()            //Constructor
{
cout<<"\n Constructor Fired";
}
~add()        //Destructor
{
cout<<"\n Destructor Fired";
}


};
main()
{
clrscr();
int i;
add a1;
getch();
}

-----------------------------------------------------------------------------------

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

A destructor is a member function with the same name as its class prefixed by a ~ (tilde). For example:class X { public: // Constructor for class X X(); // Destructor for class X ~X(); };


A destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared const, volatile, const volatile orstatic. A destructor can be declared virtual or pure virtual.

If no user-defined destructor exists for a class and one is needed, the compiler implicitly declares a destructor. This implicitly declared destructor is an inline public member of its class.

The compiler will implicitly define an implicitly declared destructor when the compiler uses the destructor to destroy an object of the destructor's class type. Suppose a class A has an implicitly declared destructor. The following is equivalent to the function the compiler would implicitly define for A: A::~A() { }


The compiler first implicitly defines the implicitly declared destructors of the base classes and nonstatic data members of a class A before defining the implicitly declared destructor of A

A destructor of a class A is trivial if all the following are true:
It is implicitly defined
All the direct base classes of A have trivial destructors
The classes of all the nonstatic data members of A have trivial destructors

If any of the above are false, then the destructor is nontrivial.

A union member cannot be of a class type that has a nontrivial destructor.

Class members that are class types can have their own destructors. Both base and derived classes can have destructors, although destructors are not inherited. If a base class A or a member of A has a destructor, and a class derived from A does not declare a destructor, a default destructor is generated.

The default destructor calls the destructors of the base class and members of the derived class.

The destructors of base classes and members are called in the reverse order of the completion of their constructor:
The destructor for a class object is called before destructors for members and bases are called.
Destructors for nonstatic members are called before destructors for base classes are called.
Destructors for nonvirtual base classes are called before destructors for virtual base classes are called.



view source::-- http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr380.htm

No comments:

Post a Comment