Thread: Confused on Constructors/Destructors

  1. #1

    Confused on Constructors/Destructors

    What do these really do. I'm kind of confused by them. If possible, can you like show me an example. Like what is the whole point of having constructors or destructors? thanks. still learnin. hehe.


  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Constructors enable a user, or the designer of the class, to initialize an object with a particular set of values upon creation of an object of that type. They are sometimes common places to open files used by the object and allocate dynamic memory for later use by the class.

    Destructors are common places to put code that, for example, may deallocate/free up any dynamic memory that was used by the object. They may also be where you close any open files that were used.

    class MyClass
        int* ptr;
        MyClass(int aVal = 10)  // Default argument provided
            ptr = new int[aVal];  // Dynamically allocate space for int array
            delete [] ptr;  // Deallocate dynamic memory
    // Create MyClass object.  Internal arrays get space allocated for 10 ints
    MyClass mc1;
    // Create MyClass object.  Internal arrays get space allocated for 14 ints
    MyClass mc2(14);
    When the above objects go out of scope, the calls to delete in the destructors will automatically deallocate the memory reserved when the objects were first created.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2002
    In a little more abstract terms, here's an analagy that may help.

    Think of an abstract description of a house. My idea of a house may be different from yours, but I suspect we can agree that the house has more than one wall, a roof, and at least one door. This abstraction of a house is analagous to a class declaration. A given house however needs to have a given number of walls, and a given number of doors. This can be accomplished by planning ahead creating a blueprint in building terms, and by a constructor, in coding terms. Later on you may want to remodel the house. In coding terms this can be done with mutator methods that change or set a given value originally built by the constructor. Eventually you may want to raze the house and you might want to leave explicit instructions in terms of what to do with certain parts. In coding parlance this is equivalent to the destructor.

    Now, could you build the house without a blueprint, just "remodeling" as you go? Sure, just leave the blueprint blank and start working. Likewise, could you build an object using just mutator functions? Sure, just leave the constructor blank and do everything with mutators. However, the likelihood of making a mistake without a written plan is much higher than if you have it all planned out in advance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  2. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM