Constructor And Destructor in C++


Constructor And Destructor in C++


  Constructor And Destructor in C++


Constructor(declaration and definition)

A constructor is a member function of a class; it automatically called every time when the object is created.

The main use of constructor is to initialize the object. The constructor has the same name as that of class; it does not have any return type.

Syntax
<class name>{arguments};

§  Define it in a public.
§  There are several form of constructors

 1. Default constructor
2. Parameterized constructor
3. Copy constructor


1. Default constructor

Default constructor is also called as no argument constructor. x:x() is a default constructor for class x since it takes no argument.


2. Parameterized constructor

A constructor that can take arguments are called parameterized constructor.


3. Copy Constructor

Copy constructor is used to declare and initialize an object from another object.
For ex. abc c2(c1);

Would define an object c2 and at the same time initialize it to the value of c1.

Ex. class abc{
                            int a,b;
                            public:
                            abc(int x, int y)
                             {
                                  a=x; 
                                  b=y;
                             }
                          abc( abc &p)
                           {
                              a=p.a;
                              b=p.b; 
                              cout<<”\n Copy constructor working”;
                           }
                         void show()
                         {
                            cout<<a<<” “<<b<<endl;
                         }
                  };
      void main()
          {
               abc c1(10,20);         
               abc c2(c1);
         abc c3=c1;
       }

Copy constructor is called in two situations.

1.      When an object is passed by value
2.    When a function return object


·      Overloaded constructor

1.      One constructor overload another constructor is called constructor overloading.

2.    It has the same name of class.

3.    It must be public member and no return type.

4.    Default constructors are called when constructors are not defined for the class.


Ex.
#include<iostream.h>
 class Example {
                                  int a,b;
                                  public:
                                     Example( )
                                          {
                                              a=50;
                                              b=100;
                                              cout<<”\n Im Constructor;”
                                           }
                                    Example (int x,int y)
                                         {
                                            a=x;
                                            b=y;
                                            cout<<”\n Im constructor”;    
                                          }
                                   void Display()
                                        {
                                           cout<<  “\n Values:”<<a<<”\t”<<b;
                                         }
                           };
                  void main()
                     {
                         Example obj(10,20);
                         Example obj2;
                         obj.Display();
                          obj2.Display ();
                     }

Output:
        Im Constructor
        Im Constructor
Values: 10      20
Values: 50      100   



·        Constructor with default argument

It is possible to define constructor with default arguments.
Default argument is an argument to a function that a programmer is not required to specify.



·        Destructor

Just as object are created, so are they destroyed .Destructor is used to destroy the object that has been created by constructor and release the memory.

Syntax
~classname ();
The name of destructor is same as class name and protected by (~) tilde.

Ex. class creat
         {
             private:
             int yearOfBirth;
             public:
                 creat()
                    {
                         yearOfBirth=1970;
                          cout<<”Constructor called”<<endl;
                     }
           ~creat()
                 {
                    cout<<”Destructor called”;
                 }
             };
 void main()
    {
        creat obj;
         cout<<”main start”<<endl;
    }


·        Characteristics of destructor

·        Take the same name as class name.
·        Defined in the public.
·        Destructors cannot be overloaded.
·        No return type is specified.




This post is about Constructor and Destructor in C++. In the next post we will discuss Inheritance:Extending Classes. 

Post a Comment

0 Comments