Tuesday, January 18, 2011

Constructors

Constructors
A constructor is a special kind of class member function that is executed when an object of that class is instantiated. Constructors are typically used to initialize member variables of the class to appropriate default values, or to allow the user to easily initialize those member variables to whatever values are desired.
Unlike normal functions, constructors have specific rules for how they must be named:
1) Constructors should always have the same name as the class (with the same capitalization)
2) Constructors have no return type (not even void)
A constructor that takes no parameters (or has all optional parameters) is called a default constructor.
Here is an example of a class that has a default constructor:
01class Fraction
02{
03private:
04    int m_nNumerator;
05    int m_nDenominator;
06 
07public:
08    Fraction() // default constructor
09    {
10         m_nNumerator = 0;
11         m_nDenominator = 1;
12    }
13 
14    int GetNumerator() { return m_nNumerator; }
15    int GetDenominator() { return m_nDenominator; }
16    double GetFraction() { return static_cast<double>(m_nNumerator) / m_nDenominator; }
17};
This class was designed to hold a fractional value as an integer numerator and denominator. We have defined a default constructor named Fraction (the same as the class). When we create an instance of the Fraction class, this default constructor will be called immediately after memory is allocated, and our object will be initialized. For example, the following snippet:
1Fraction cDefault; // calls Fraction() constructor
2std::cout << cDefault.GetNumerator() << "/" << cDefault.GetDenominator() << std::endl;
produces the output:
0/1
Note that our numerator and denominator were initialized with the values we set in our default constructor! This is such a useful feature that almost every class includes a default constructor. Without a default constructor, the numerator and denominator would have garbage values until we explicitly assigned them reasonable values.
Constructors with parameters
While the default constructor is great for ensuring our classes are initialized with reasonable default values, often times we want instances of our class to have specific values. Fortunately, constructors can also be declared with parameters. Here is an example of a constructor that takes two integer parameters that are used to initialize the numerator and denominator:
01#include <cassert>
02class Fraction
03{
04private:
05    int m_nNumerator;
06    int m_nDenominator;
07 
08public:
09    Fraction() // default constructor
10    {
11         m_nNumerator = 0;
12         m_nDenominator = 1;
13    }
14 
15    // Constructor with parameters
16    Fraction(int nNumerator, int nDenominator=1)
17    {
18        assert(nDenominator != 0);
19        m_nNumerator = nNumerator;
20        m_nDenominator = nDenominator;
21    }
22 
23    int GetNumerator() { return m_nNumerator; }
24    int GetDenominator() { return m_nDenominator; }
25    double GetFraction() { return static_cast<double>(m_nNumerator) / m_nDenominator; }
26};
Note that we now have two constructors: a default constructor that will be called in the default case, and a second constructor that takes two parameters. These two constructors can coexist peacefully in the same class due to function overloading. In fact, you can define as many constructors as you want, so long as each has a unique signature (number and type of parameters).
So how do we use this constructor with parameters? It’s simple:
1Fraction cFiveThirds(5, 3); // calls Fraction(int, int) constructor
This particular fraction will be initialized to the fraction 5/3!
Note that we have made use of a default value for the second parameter of the constructor with parameters, so the following is also legal:
1Fraction Six(6); // calls Fraction(int, int) constructor
In this case, our default constructor is actually somewhat redundant. We could simplify this class as follows:
01#include <cassert>
02class Fraction
03{
04private:
05    int m_nNumerator;
06    int m_nDenominator;
07 
08public:
09    // Default constructor
10    Fraction(int nNumerator=0, int nDenominator=1)
11    {
12        assert(nDenominator != 0);
13        m_nNumerator = nNumerator;
14        m_nDenominator = nDenominator;
15    }
16 
17    int GetNumerator() { return m_nNumerator; }
18    int GetDenominator() { return m_nDenominator; }
19    double GetFraction() { return static_cast<double>(m_nNumerator) / m_nDenominator; }
20};
This constructor has been defined in a way that allows it to serve as both a default and a non-default constructor!
1Fraction cDefault; // will call Fraction(0, 1)
2Fraction cSix(6); // will call Fraction(6, 1)
3Fraction cFiveThirds(5,3); // will call Fraction(5,3)
Classes without default constructors
What happens if we do not declare a default constructor and then instantiate our class? The answer is that C++ will allocate space for our class instance, but will not initialize the members of the class (similar to what happens when you declare an int, double, or other basic data type). For example:
01class Date
02{
03private:
04    int m_nMonth;
05    int m_nDay;
06    int m_nYear;
07};
08 
09int main()
10{
11    Date cDate;
12    // cDate's member variables now contain garbage
13    // Who knows what date we'll get?
14 
15    return 0;
16}
In the above example, because we declared a Date object, but there is no default constructor, m_nMonth, m_nDay, and m_nYear were never initialized. Consequently, they will hold garbage values. Generally speaking, this is why providing a default constructor is almost always a good idea:
01class Date
02{
03private:
04    int m_nMonth;
05    int m_nDay;
06    int m_nYear;
07 
08public:
09    Date(int nMonth=1, int nDay=1, int nYear=1970)
10    {
11        m_nMonth = nMonth;
12        m_nDay = nDay;
13        m_nYear = nYear;
14    }
15};
16 
17int main()
18{
19    Date cDate; // cDate is initialized to Jan 1st, 1970 instead of garbage
20 
21    Date cToday(9, 5, 2007); // cToday is initialized to Sept 5th, 2007
22 
23    return 0;
24}

No comments: