Tuesday, October 16, 2012

Copy Constructor & this pointer


Copy Constructor: Firstly, what is constructor? A constructor is a method define in a class by same name as of class. The work of constructor is to initialize the class members.Constructor is defined publicaly or privately. A Constructor never return any value,so it will never have any return type. A constructor without an argument is called default constructor. A constructor with an argument is known as parameterised constructor

 example:      class example
                       {
                          int a,b;            //data member 

                      public:
                                  
                            example()           //Default Constructor 
                           {
                              a=10;
                               b=10;
                           }
                           example(int x, int y)            //parameterised Constructor
                          {
                                a=x;   
                                b=y;
                           }
                       };

this pointer: Member function of every class object have a special kind of pointer known as this pointer, which points to the object itself. When we cal a member function it comes into existence with the value of this set to the address of object it is pointing. Using a this pointer any member function can find out the the address of the object which it is pointing. Using this pointer a member function can also access the data of the object.
  example:


                       class example
                       {
                          int a,b;            

                      public:
                                  
                            example()         //constructor is also a member function of a class 
                           {
                              this->a=10;          //data member are access with the help of this pointer
                               this->b=10;
                           }
                        };


Before getting to copy constructor, first you should understand about reference. When a reference is used as a function argument,any modification to the reference inside the function
will cause change to the argument outside the function, this is something same as passing pointer as argument. But a reference has a cleaner syntax.
                    Constructor is call it initializes data members of a class. In above example you have seen that when a parameterised constructor is called data members are initialized with the parameter value passed. A Copy constructor is a constructor defined within a class that takes class object as a parameter and initialize the class data member with the object received as a argument. So the work of a copy constructor is to create a copy of the object it received as agrument from a constructor call. Given example will make Copy constructor concepts more clear to you.


/* Copy Constructor*/


#include<iostream.h>
#include<conio.h>
class Test
{
int hr;
int min;
public:

//default constructor.
Test()
{
hr=0;
min=0;
}
//Parameterised constructor.
Test(int a,int b)
{
this->hr=a;      //this pointer is used to initialised data member
this->min=b;
}

//This is copy constructor.
/*It will copy the object pass as parameter to the
  object by which the construtor is call*/

Test(Test &t)
{
hr=t.hr;
min=t.min;
}
void disp();
};
void Test::disp()
{
cout<<hr<<"\t"<<min<<"\n";
}
void main()
{
Test c;                       //default constructor call.
Test c1(5,10);              //parameterised constructor call.
Test c2(c1);               //copy constructor call.
c.disp();
c1.disp();
c2.disp();
getch();
}

     

No comments: