1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

what is the use of constructor and Destructors in c++

Discussion in 'Programming' started by bulochc83, Jul 23, 2010.

  1. #1
    suggest me.......
    SEMrush
     
    bulochc83, Jul 23, 2010 IP
    SEMrush
  2. FDIM

    FDIM Member

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Constructor is used to initialize variables, set default values, allocate memory if needed and such. It is called when you assign class to variable eg, List list = new List();
    Destructor is mostly used to free used memory. It is called when you want to delete used variable. Eg, delete list;
     
    FDIM, Jul 23, 2010 IP
  3. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #3
    You use constructors and destructors in a class or object. When you instantiate an object, you sometimes would want to create an initialize variables, allocate memory, etc. in it, you use a constructor for that purpose.

    A destructor function runs automatically as soon as the object gets destroyed - usually, you need this function to manually free used memory.

    For more information about it, google it.
     
    Rainulf, Jul 24, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Let's say you have a game programmed in C++ and you have a class that defines a player... he has hit points, weapons, abilities and such. When you create an instance of this player, it would be nice if he started with full hit points, and maybe even triggers an event to tell everyone he's spawned into the game. You could do all this within a constructor.

    At the same time, when that player leaves or dies - would you really want the memory to still be in use to hold his hitpoints, abilities and weapons? no, you would want it to be used for other players. Destructors will execute code at the end of an objects life - and free up memory that is no longer required.

    Hmm... funny how people just google the question then post what they find.
     
    Deacalion, Jul 24, 2010 IP
  5. Harris21

    Harris21 Peon

    Messages:
    28
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The use of constructors is to initialize objects.Constructor is special member function which has same name as class name and used to construct the object of the class. The function of initialization is automatically carried out by the use of a special member function called a constructor.
    Constructor get called automatically when object is created

    Syntax for constructor

    <class name> { arguments};

    Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.

    General Syntax of Destructors

    ~ classname();
     
    Harris21, Aug 2, 2010 IP
  6. vgupta80

    vgupta80 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Constructor is used to initialize the objects . It will initialize the private , protected and public data members with the values mentioned in the constructor.
    Destructor simply deallocate the memory allocated by the constructors .
     
    vgupta80, Aug 13, 2010 IP
  7. rajeev_seo

    rajeev_seo Peon

    Messages:
    211
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    cunstructor use to initialize a variable and destructor use to unset a variable.....
     
    rajeev_seo, Apr 24, 2011 IP
  8. om39a

    om39a Peon

    Messages:
    287
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    @Deacalion- Simple but very good expmple.
     
    om39a, Apr 25, 2011 IP
  9. rajendradhakal

    rajendradhakal Peon

    Messages:
    245
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i'm agree with you dear friends
     
    rajendradhakal, Apr 25, 2011 IP