Saturday, February 2, 2013

Constructor & Destructor in C++


Constructor:
A constructor is a special member function whose task is to initialize the object of its class. Constructor name is the same as the class name. The constructor is called whenever an object of its associated class is created.
For Ex:
            class integer ()
            {
                        private:
                                    int n, m;
                        public:
                                    integer ()
                                    {
                                                n=0;
                                                m=0;
                                    }
            };

Characteristics of Constructor:
                                i.            They should be declared in public section.
                              ii.            They are called automatically when class objects are declared.
                            iii.            They do not have return type so they cannot return any value.
                           iv.            They cannot be inherited, derived class can call base call constructor.

Types of constructor:
1.     Default constructor
2.     Parameterized constructor
3.     Copy constructor


1.     Default constructor:
When a constructor is called without any argument and it is called automatically when object is created, this is called default constructor.

2.     Parameterized constructor:
When a constructor is called with parameters while creating an object; such type of constructor is called parameterized constructor.
            class integer ()
            {
                        private:
                                    int n, m;
                        public:
                                    integer () //Default
                                    {
                                                n=0;
                                                m=0;
                                    }
                                    integer (int x, int y);
            };
integer::integer(int x, int y) //Parameterized
{
            n=x;
            m=y;
}

3.     Copy constructor:
A constructor takes a reference of an object of the same class, itself as an argument.
A reference variable is used as argument in constructor such a constructor is called copy constructor.




class emp
{
            private:
                        int id;
            public:
                        emp(int a)
                        {
                                    id=a;
                        }
                        emp(emp &obj)
                        {
                                    id=obj.id;
                        }
                        Void display()
                        {
                                    cout<<”id”<<id;
                        }
};
void main()
{
            emp x(100);
            emp y(x);
            cout<<”x”;
            x.display();
            cout<<”y”;
            y.display();
}

Destructor:
A member function that names same as class name preceded by a tilde (~) sign. Such types of function are called destructor.








class ex
{
            static int count;
            public:
                        ex()
                        {
                                    count++;
                                    cout<<”No. of object created”<<count;
                        }
                        ~ex()
                        {
                                    count--;
                                    cout<<”No. of object destroyed”<<count;
                        }

};
void main()
{
            ex e1,e2,e3;
            {
                        ex e4;
            }
}

0 comments:

Post a Comment

Powered by Blogger.