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.
ryan
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.
ryan
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.
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.Code:class MyClass { int* ptr; public: MyClass(int aVal = 10) // Default argument provided { ptr = new int[aVal]; // Dynamically allocate space for int array } ~MyClass() { 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);
"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
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.