A constructor is such a basic and fundamental concept in Object Oriented Programming, that if any OOP programmers are reading this, you better already know what it is and what it does. If you do not, you are in serious peril. And shame on you!

Therefore, I'm going to try and be as non-technical as possible and describe a constructor for those gentle readers who are not crusty programming veterans.

A constructor is a method in an Object Oriented Programming language in which objects of a class are instantiated and/or initialized. Instantiated means that an instance of a class, or an object is created. You can picture a class as a cookie cutter. You can picture your computer's resource heap as the dough. Instantiation is pressing the cookie cutter into the dough to make a gingerbread man (the object).

How does a constructor fit into all that? A constructor is a function which is called during instantiation to put the object into it's initial state. Going back to the gingerbread man analogy, your constructor may draw a face on it, put buttons on it, and/or cover it in green sugar. In C++ your constructor may allocate memory for member pointers, fill in a deque with starting information or read data from a file, and stick that data in a string.

There are a few kinds of constructors. A default constructor creates a generic version of your class with no muss and no fuss. All objects created using the default constructor start off exactly alike. Generally you initialize all your members to default values, (set the ints to 0, set strings to "", etc.). If your gingerbread man was created using a default constructor, a simple smiley face may be painted on it by default.

You can have constructors which take parameters. These constructors overload the default constructor to provide different initializations from the default constructor. You may indicate during instantiation, that you want a scary face on your gingerbread man instead of the default smiley face. You can also tell it to draw on some buttons while it's at it.

The Copy Constructor is hard to describe with the gingerbread man analogy, but basically you're saying that you want a brand new gingerbread man exactly like the one you have now. During the life of an object, its properties (member variables, for instance) may change. When you make another object like it using the copy constructor, you get a duplicate of the object as it is, at instantiation time. This method is also used when passing the object to a function by value - in other words, passing a copy. C++ provides an implicit copy constructor with all objects, which simply does a memberwise copy of the object. It is however, considered better for a class to provide its own copy constructor, if the class has any sort of complexity. A copy constructor must follow the format: X::X(const X&)

In C++ and Java the constructor has the same syntax. It has the same name as the class it's found in, and looks like any other function. In Visual Basic (ver. 4 - 6) the "constructor" (such as it is) is defined in the Initialize Function. Unfortunately, you only get the default constructor. If you need to do any alternate initializations, you have to create another function and call it after instantiation. I haven't tried it yet, but I've heard that VB.NET has fixed this annoying limitation. Ada doesn't have constructors.

Con*struct"or (?), n. [Cf. LL. constructor.]

A constructer.

 

© Webster 1913.

Log in or register to write something here or to contact authors.