Friday 31 May 2013

Constructors and destructors in C++


Constructor
l  A constructor is a member function of a class.
l  It is a special member function of a class whose name is same as the name of class.
l  It has no return type.
l  Constructors do not call explicitly.
l  It is invoked implicitly when an object of class is created.
l  An implicit constructor is generated by compiler whenever we do not write any constructor in class definition. This implicit constructor would be called during object creation.
l  If we define a constructor in the class then there will be no implicit constructor created by class.
l  Constructor should always be defined in public as we create objects outside the class definition. Although there are scenarios where we would like to define constructor in private.
l  Constructor solves the problem of initialization. We can initialize variables of primitive data type during creation of variable but this feature is optional.

For example: int x=3; //creation of variable x and initialize it with a value 3
Let us assume complex is a user defined data type.
complex c1=4; //error this is called problem of initialization during creation of object


l  This problem can be solved with constructor. Following example illustrates this in detail:
#include<iostream.h>
class complex
{
     private:
     int a,b;
     public:
     complex(int x) { a=x; b=x; }
     void getdata();
     void showdata();  
};
void complex:: getdata()
{
     cout<<”Enter two numbers”;
     cin>>a>>b;
}
void complex:: showdata()
{
     cout<<”a=”<<a<<”b=”<<b;
}
void main()
{
  complex c1=3;  //constructor is called
  c1.show();
}

l  Here, constructor is called when object c1 is created in main() function
l  Notice that a value 3 is assigned to c1. This notation is resolved as; during creation of object c1 constructor is called and 3 is passed as an argument, which is then received by variable x in constructor. Value of x is copied into a and b. Hence, member variables of object c1 are initialized during object creation.
l  Please note the following declarations
complex c1(3);
complex c1=complex(3);

l  These two notations and the one which is used in function main()(complex c1=3;} are all same. You can choose the one that you like the most.
l  What happens if we remain class specification same and change the code of function main().
New definition of main(). For class definition please refer above example:
void main()
{
  complex c1=3;
  complex c2;  //error: no proper match for the constructor in class complex
 …
}


l  Why this error occurs? Answer is simple there is only one constructor in class complex which takes one argument of type int. So the first line is valid. In the second line we are creating an object with no arguments for constructor. Also note that there is no implicit constructor created by compiler as compiler do such only when we do not define any constructor in the class.
l  To remove this error we should define another constructor in the class which takes no argument.
l  Now consider another example which is remarkable modification in the previous example
#include<iostream.h>
class complex
{
     private:
     int a,b;
     public:
     complex() { } //default constructor, constructor with no argument
            complex(int x)
{ a=x; b=x; } //parameterized constructor, one argument
     complex (int x, int y)
{ a=x; b=y; } //parameterize constructor for two arguments
            void getdata();
     void showdata();  
};
void complex:: getdata()
{
     cout<<”Enter two numbers”;
     cin>>a>>b;
}
void complex:: showdata()
{
     cout<<”a=”<<a<<”b=”<<b;
}
void main()
{
  complex c1=3;  //constructor with one argument is called
     complex c2(3,4); //constructor with two arguments is called
     complex c3; //default constructor is called
     c1.show();
  c2.show();
  c3.show();
}

l  Here, three versions of constructors are defined in the class complex.
l  Compiler will resolve the version of constructor by examine argument list. This mechanism is called constructor overloading. Since this happens during compilation it is known as compile time polymorphism.

Copy Constructor

l  Whenever an object is created and initialize by the object of same class, no need to define another constructor as bitwise copy of existing object is assigned to newly created object. But whenever you desire to change this traditional mechanism of bitwise copying, you can create a copy constructor.
l  Following example illustrates copy constructor:

#include<iostream.h>
class complex
{
     private:
     int a,b;
     public:
     complex() { } //default constructor, constructor with no argument
            complex(int x)
{ a=x; b=x; } //parameterized constructor for one argument
            complex (int x, int y)
{ a=x; b=y; } //parameterize constructor for two arguments
            complex (complex &c) //copy constructor
{   
   a= c.b;   
   b= c.a;
}
void getdata();
     void showdata();  
};
void complex:: getdata()
{
     cout<<”Enter two numbers”;
     cin>>a>>b;
}
void complex:: showdata()
{
     cout<<”a=”<<a<<”b=”<<b;
}
void main()
{
  complex c1=3;  //constructor with one argument is called
     complex c2(3,4); //constructor with two arguments is called
     complex c3; //default constructor is called
     complex c4=c2;  //copy constructor is called
     c1.show();
  c2.show();
  c3.show();
  c4.show();
}

l  Here, copy constructor is called during creation of object c4. We are passing reference of c2. Thus c is reference object in copy constructor.
l  Remember always to pass reference in copy constructor and not pass value of object otherwise it yields an error message. It is due to avoid recursive call of copy constructor.

Destructor

l  Destructor is also a member function of class.
l  The name of destructor is same as the name of class but preceded with a tilde (~) sign.
l  It has no return type.
l  It never takes any argument and hence can not be overloaded.
l  Destructor invokes automatically for an object, when object goes out of scope.
l  Destructor is called when object is destroyed. Whatever we code in destructor will run during object destruction.
l  Sometimes it is needed to perform some actions when an object is destroyed. For example, an object that allocates memory when it is created will want to free that memory when it is destroyed.
l  The following example illustrates the use of destructor.
#include<iostream.h>
class complex
{
     private:
            int a,b;
     public:
            complex() { } //default constructor, constructor with no argument
            complex(int x) { a=x; b=x; } //parameterized constructor for one argument
            complex (int x, int y) { a=x; b=y; } //parameterize constructor for two arguments
            complex (complex &c) //copy constructor
{          
   a= c.b;   
   b= c.a;
}
~complex()
{ cout<<”You are in destructor”; }
void getdata();
            void showdata();        
};
void complex:: getdata()
{
            cout<<”Enter two numbers”;
            cin>>a>>b;
}
void complex:: showdata()
{
            cout<<”a=”<<a<<”b=”<<b;
}
void main()
{
  complex c1=3;  //constructor with one argument is called
  complex c2(3,4); //constructor with two arguments is called
  complex c3; //default constructor is called
  complex c4=c2;  //copy constructor is called
  c1.show();
  c2.show();
  c3.show();
  c4.show();
}

l  We have added one more function in the previous program that is destructor.
l  You can see name of destructor is same as the name of the class preceded by a symbol called tilde (~).
l  This function is called every time when object goes out of scope.

Static Member variable and static member function
Members of a class can be qualified as static by using keyword static. Members those are static are different in behavior from other non static members of the class.
Life of the static members extends the entire run of the program.

Static member variable

l  When you declare a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class.
l  Static member variables need to be defined outside the class.
l  Static member variables are not associated with any object and thus also known as class variable.
l  Although object can access public static member variables of the class using dot (.) operator but that doesn’t mean that static member variable is associated with that object.
l  Static member variable can also be accessed with class name and scope resolution operator.
l  Memory allocation of static member variable doesn’t depend on creation of object of class.
l  Static member variables can be accessed through any of class member function, no matter if they are static or not.
l  Life time of static members extends the entire program.

Example of static member variables

#include<iostream.h>

class  marksheet
{
  private:
          int sub1,sub2,sub3,sub4,sub5;
          float per;
          int result; 
  public:
          static int displaycount;
            void getmarks()
            {
                        cout<<”Enter marks of five subjects:”;
                        cin>>sub1>>sub2>>sub3>>sub4>>sub5;
                        result=result_generation();
                        per=percentage();
            }
            int result_generation()
            {
                        if(sub1>=35&&sub2>=35&&sub3>=35&&sub4>=35&&sub5>=35)
                                    return(1);
                        return(0);
            }
            float percentage()
{
            int sum=sub1+sub2+sub3+sub4+sub5;
                        return(sum/5.0);
            }
            void show_result()
{
            cout<<”Subject 1: “<<sub1<<endl;
            cout<<”Subject 2: “<<sub2<<endl;
            cout<<”Subject 3: “<<sub3<<endl;
            cout<<”Subject 4: “<<sub4<<endl;
            cout<<”Subject 5: “<<sub5<<endl;
            cout<<”Result: ”;
            result==1? cout<<”PASS” : cout<<”FAIL”;
            cout<<”\nPercentage: “;
            result==1?cout<<per:;
            displaycount++;
            cout<<”Display No: “<<displaycount;
      }
};
int marksheet:: displaycount;
void main()
{
            marksheet m1,m2,m3;
            m1.getmarks();
            m1.showresult();
            m2.getmarks();
            m2.showresult();
            m3.getmarks();
            m3.showresult();
}

Try above program and see result
You will observe every time showresult() increments displaycount. No matter who calls showresult, there is only one copy of displaycount used by showresult().

In the above program, displaycount is a static member variable. Please go the points written above this program.


Static member function

l  Static member functions can access only static members of the same class.
l  Static member function can be called by class name and scope resolution operator.
l  Static member functions can also be accessed through object name and dot (.) operator, but that doesn’t mean that the static member function could access non static member variables of object.
l  You might have a surprised, why we use static member function while we can access static member variables from non static member function. Answer is simple, we can not call non static member functions from out side the class without using object and dot (.) operator. But we know that existence of static member variable does not depend on creation of object. So how we can access static member variables (private) from outside the class when none of the object is created? It can be accessed through static member functions.
l  Since static member functions are not associated with any particular object they can never access object’s non static variables. Although we can create an object of same class in static member function and can access object’s member from there.
l   

Example
#include<iostream.h>

class  marksheet
{
  private:
          int sub1,sub2,sub3,sub4,sub5;
          float per;
          int result; 
          static int displaycount;
  public:
            void getmarks()
            {
                        cout<<”Enter marks of five subjects:”;
                        cin>>sub1>>sub2>>sub3>>sub4>>sub5;
                        result=result_generation();
                        per=percentage();
            }
            int result_generation()
            {
                        if(sub1>=35&&sub2>=35&&sub3>=35&&sub4>=35&&sub5>=35)
                                    return(1);
                        return(0);
            }
            float percentage()
{
            int sum=sub1+sub2+sub3+sub4+sub5;
                        return(sum/5.0);
            }
            static int counter()
            {
                        displaycount++;
                        return(displaycount);
            }
            void show_result()
{
            cout<<”Subject 1: “<<sub1<<endl;
            cout<<”Subject 2: “<<sub2<<endl;
            cout<<”Subject 3: “<<sub3<<endl;
            cout<<”Subject 4: “<<sub4<<endl;
            cout<<”Subject 5: “<<sub5<<endl;
            cout<<”Result: ”;
            result==1? cout<<”PASS” : cout<<”FAIL”;
            cout<<”\nPercentage: “;
            result==1?cout<<per:;
      }
};
int marksheet:: displaycount;
void main()
{
            marksheet::counter();
            marksheet m1,m2,m3;
            m1.getmarks();
            m1.showresult();
            m1.counter();
            m2.getmarks();
            m2.showresult();
            m2.counter();
            m3.getmarks();
            m3.showresult();
            m3.counter();
}

No comments:

Post a Comment