Results 1 to 5 of 5

Thread: What are the Constructors and Destructors?

  1. #1
    Join Date
    Feb 2009
    Posts
    10

    What are the Constructors and Destructors?

    Hi,

    I want to know the concept of Constructors and Destructors. Anyone can explain me this in simple words?

  2. #2
    Join Date
    May 2008
    Posts
    35

    Re: What are the Constructors and Destructors?

    Constructor and Destructor functions are Member Functions of a class having some special property.

    Constructor function gets invoked when an object of a class is constructed (declared) and destructor function gets invoked when the object is destructed (goes out of scope).

    Use of Constructor and Destructor function of a class

    • Constructor function is used to initialize member variables to pre-defined values as soon as an object of a class is declared.
    • Constructor function having parameters is used to initialize the data members to the values passed values, upon declaration.
    • Generally, the destructor function is needed only when constructor has allocated dynamic memory.


    Defining Constructor and Destructor functions

    The example below illustrates how constructor and destructor functions are defined:

    Code:
      class myclass
      {
       private:
       int number;
    
       public:
       myclass()//constructor
       {
        number=10;
       }
    
       ~myclass()//destructor
       {
      //nothing needed
       }
      };
    A few points to note:

    • Both of the functions have the same name as that of the class, destructor function having (~) before its name.
    • Both constructor and destructor functions should not be preceded by any data type (not even void).
    • These functions do not (and cannot) return any values.
    • We can have only the constructor function in a class without destructor function or vice-versa.
    • Constructor function can take arguments but destructors cannot.
    • Constructor function can be overloaded as usual functions.


    Example 1: Using constructor function to initialize data members to pre-defined values

    Code:
      //Example Program in C++
      #include<iostream.h>
    
      class myclass
      {
       private:
       int a;
       int b;
    
       public:
       myclass()
       {
       //here constructor function is used to
       //initialize data members to pre-def
       //values
       a=10;
       b=10;
       }
    
       int add(void)
       {
        return a+b;
       }
      };
    
      void main(void)
      {
       myclass a;
    
       cout<<a.add();
      }
    Example 2: Using constructor function to initialize data members to values passed as arguments

    Code:
      //Example Program in C++
      #include<iostream.h>
    
      class myclass
      {
       private:
       int a;
       int b;
    
       public:
       myclass(int i, int j)
       {
        a=i;
        b=j;
       }
    
       int add(void)
       {
        return a+b;
       }
      };
    
      void main(void)
      {
       //notice how the object of the class
       //has been declared
       //it can be thought as
       // myclass a;
       // a=myclass(10,20)
       myclass a(10,20);
    
       cout<<a.add();
      }
    Notice that there is no destructor function in both the examples, just because we don’t need them.

  3. #3
    Join Date
    May 2008
    Posts
    35

    Re: What are the Constructors and Destructors?

    When does the destructor function gets invoked

    The examples below illustrates when the destructor function gets invoked:

    Example 1:

    Code:
      //Example Program in C++
      #include<iostream.h>
    
      class myclass
      {
       public:
    
       ~myclass()
         {
          cout<<"destructed\n";
         }
      };
    
      void main(void)
      {
       myclass obj;
       cout<<"inside main\n";
      }
    OUTPUT:

    inside main
    destructed
    Press any key to continue

    As I said in the other article, destructors get invoked when the object of a class goes out of scope. In this case, the object goes out of scope as the program terminates. So the destructor gets invoked just before the program’s termination.

    Example 2:

    Code:
      //Example Program in C++
      #include<iostream.h>
    
      void myfunc(void);
    
      class myclass
      {
       public:
    
       ~myclass()
         {
          cout<<"destructed\n";
         }
      };
    
      void main(void)
      {
       cout<<"inside main\n";
       myfunc();
       cout<<"again inside main\n";
      }
    
      void myfunc(void)
      {
       cout<<"inside myfunc\n";
       myclass obj;
       cout<<"still inside myfunc\n";
      }
    OUTPUT:

    inside main
    inside myfunc
    still inside myfunc
    destructed
    again inside main
    Press any key to continue

    In this case, destructor function is invoked just as the program’s execution returns from the function, but before executing any further instruction from where it was called (main).

    Example 3: In the following example we are creating a dynamically allocated object of a class in the same way as we did with the variables.

    Code:
      //Example Program in C++
      #include<iostream.h>
    
      class myclass
      {
       public:
    
       ~myclass()
         {
          cout<<"destructed\n";
         }
      };
    
      void main(void)
      {
       myclass *obj;
       obj=new myclass;
       cout<<"inside main\n";
    
       delete obj;
    
       cout<<"still inside main\n";
      }
    OUTPUT:

    inside main
    destructed
    still inside main
    Press any key to continue

    Here the programmer is explicitly destroying the object, hence the destructor function is called.

    Now that you know some details about the destructor function, let me give you a practical example of the use of destructor function:

    Code:
      //Example Program in C++
      #include<iostream.h>
    
      class myclass
      {
       int *number;
    
       public:
       myclass(int num)
         {
          number=new int[num];
         }
    
       ~myclass()
         {
          delete []number;
         }
    
       void input_num(int index, int num)
         {
          number[index]=num;
         }
    
       int output_num(int index)
         {
          return number[index];
         }
      };
    
      void main(void)
      {
       int size, num;
       cout<<"enter number of elments: ";
       cin>>size;
    
       myclass obj(size);
    
       for(int i=0;i<size;i++)
         {
          cout<<"enter element "<<i+1<<":";
          cin>>num;
          obj.input_num(i,num);
         }
    
       cout<<"\nelements have the following values\n\n";
    
       for(i=0;i<size;i++)
         {
          cout<<"element "<<i+1<<":";
          cout<<obj.output_num(i);
          cout<<"\n";
         }
      }
    Good-Bye!

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What are the Constructors and Destructors?

    A constructor is a member function that has the same name as its class (and which has no return value). E.g., there are four constructors declared in Program. The purpose of a constructor is to initialize an object. In fact, C++ makes the following promise: If a class has a constructor, all objects of that class will be initialized.

    Consider the following sequence of variable declarations:

    Code:
    Complex c;       // calls Complex ()
    Complex d = 2.0; // calls Complex (double)
    Complex i(0, 1); // calls Complex (double, double)
    Complex j(i);    // calls Complex (Complex const&)
    Each variable declaration implicitly causes the appropriate constructor to be invoked. In particular, the equal sign in the declaration of d denotes initialization not assignment and, therefore, is accomplished by calling the constructor.

    details

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: What are the Constructors and Destructors?

    Fore more details understanding go through this!

    http://publib.boulder.ibm.com/infoce...c15cplr374.htm

    Hope this helps you!

Similar Threads

  1. Constructors in Java help
    By cloud101 in forum Software Development
    Replies: 4
    Last Post: 18-01-2012, 05:02 PM
  2. Do Virtual Destructors exist in C++ ?
    By Satchel in forum Operating Systems
    Replies: 6
    Last Post: 20-01-2011, 10:25 PM
  3. what are constructors in C++?
    By Asis in forum Software Development
    Replies: 4
    Last Post: 29-12-2010, 08:38 AM
  4. Constructors and Destructors in PHP
    By garfield1 in forum Software Development
    Replies: 4
    Last Post: 02-03-2010, 11:01 PM
  5. Does anyone know about virtual constructors and destructors?
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 01:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,567,745.38189 seconds with 16 queries