Wednesday, 12 October 2011

a program for constructor and destructor

#include<iostream.h>
class abc
{
int x;
public:
abc()  //default constructor
{
  x=10;
}
abc(int a)// parameterised constructor
{
x=a;
}
abc (abc &A)  // copy constructor
{
x=A.x;
}
~abc()    // destructor
{
cout<<"destructor called"<<endl;
}
void accept()
{
cout<<"Enter a number"<<endl;
cin>>x;
}
void disp()
{
cout<<"the value of x is "<<x<<endl;
}
};
void main()
{
 abc a;
 a.disp();                // value is 10
 abc a1(1000);        // implicit call
       // explicit call abc a1=abc(1000);
 a1.disp();
 // when we r declaring 'a' as object the default constructor is called and when
 // we r declarin a1 as object parameteried constructor is called
 abc a2(a1);         // implicit call
       // explicit call abc a2=abc(a1);
a2.disp();
 }
i will surely explain u this program later

No comments:

Post a Comment