Tuesday 30 December 2014

Copy Constructor in C++ to reduce code complexity

Copy constructor: 

 Extract the source into destination element by element.
 Here the size of class name has been passed.
 Here both objects have different sizes.
 Here one element will be taken as source and put it onto destination.
 Like this entire object data is copied into destination. Hence it called “copy constructor’.

class Vector
{
int size;
int *v;
Vector(int s)
{
size=s;
v=new int[s];
}
void get()
{
for(int i=0;i>v[i];
}
};
Vector(Vector pv)
{
size=pv.size;		//sizeof(v1)
v=new int[size];
for(int i=0;i 
Here v1-> actual parameter Pv-> formal parameter Here compiler will not copy the entire data from one object to other object directly. 
It has to be done by user element by element. V1 values is to be copied into v2 by using constructor. 
The compiler will not take care while copying  ‘v1’data into ‘v2’. 
Hence user defined constructor is used while copying the data of ‘v1’ into  ‘v2’.
We will take the help of ‘copy constructor’ when the object is copied into another object.

A constructor can take anything as an argument. except same class object.

Note: same class object reference can be the parameter for a constructor . a constructor which is having same class object reference as a parameter, it is called “copy constructor”.
Constructor gets invoked when object creation occurs.
Vector v2;
Vector v1;	default constructor

Vector v2=v1;
Vector v2(v1);		Copy Constructor.

Example Program to explain the concept of constructors:
#include<stdio.h>
using namespace std;
class test
{
public: test()
{
cout<<”default constructor invoked”<
Output: default constructor invoked. Program to print sum of vector by using copy constructor
#include<stdio.h>
class Vector
{
private:
int size;
int *v;
public:
Vector(int s)
{
size=s;
v=new int[s];
}
Vector(vector &vr)
{
size=vr.size;
v=new int[size];
for(int i=0;i>v[i];
}
}

void show()
{
cout<<”elements of vector are”<
Destructors: Complements the job of constructor is called as “destructor”. 

Let us consider a simple example for gmail. When we try to open gmail account, when user name and password are matched with database. Then only object created. Constructors executes when object is created. Destructor executes when object goes to die.

Syntax:
~Bag()
{
………..
……….
}
Before object goes to die, if any operations we want to perform are going to be written within this block only. Object creation process construct the constructor. Object destroy process destroy the destructor.

If the scope of the variable exist within the block, then only the object is said to be alive.

main()
{
test1()
{
test2()
{
………
………..
}		//test2 alive up to here only
……..
……….
}
Once the object is created, it needs to destroy after using. The complement function need to be made as public to destroy the object of main function. We don’t need additional parameters to destroy the object.

Example to manually destroy the created constructor.

Vector(int s)
{
size=s;
V=new int[s];
}
~vector()
{
delete []v;
}
Destructor is a special member, which gets invoked at the time of destroying an object. Destructor will have the same name as class name preceding with tilt(~) operator.

Syntax: 
	
~class-name()
{
………
…………
}
Destructors will not have any parameters. Destructors must be in the public section itself. Destructor means, complement the job of constructor. Example:
void fun()
{
Test fo;
}
Class Test
{
Public:
Test()
{
Cout<<”constructor”<