Monday, March 22, 2010

OOP344 - who needs default constructors anyway?

i always had a feeling that default constructors were pointless. I don't know, something about making a function that sets everything to null seems... meaningless? Fortunately, I've learned an alternative! Let's say we have a class called, for instance, Poop (lol). Inside Poop, we want to keep color (char*) and size (int). If we were to use a default constructor AND an argument constructor, our prototypes would look like this:

Poop();
Poop(char* color, int size);

This method would leave us having to write the same constructor twice, except the argument constructor would assign values other than 0.
In order to avoid the redundancy, we can write 1 arguement constructor with this little tweek:

Poop(char* color = 0, int size = 0);

If Poop() is called without arguments, the constructor will automatically use the default values provided in the declaration! Genius, I know!