[ fromfile: constructors.xml id: constructors ]
A constructor, sometimes abbreviated ctor, is a special member function that controls the process of object initialization. Each constructor must have the same name as its class. Constructors do not return anything and do not have return types.
There is a special syntax for constructor definitions:
Between the closing parenthesis of the parameter list and the opening brace of a function body, an optional member initialization list can be given. A member initialization list begins with a colon (:) and is followed by a comma-separated list of member initializers, each of the form:
memberName(initializingExpression)
If (and only if) no constructor is specified in a class definition the compiler will supply one that looks like this:
ClassName::ClassName() { }
A constructor that can be called with no arguments is referred to as a default constructor. A default constructor gives default initialization to an object of its class. Any data member that is not explicitly initialized in the member initialization list of a constructor is given default initialization by the compiler.
Classes can have several constructors, each of which initializes in a different (and presumably useful) way. Example 2.7 has three constructors.
Example 2.7. src/ctor/complex.h
#include <string> using namespace std; class Complex { public: Complex(double realPart, double imPart); Complex(double realPart); Complex(); string toString() const; private: double m_R, m_I; };
Example 2.8 contains the implementation with some client code.
Example 2.8. src/ctor/complex.cpp
#include "complex.h" #include <iostream> #include <sstream> using namespace std; Complex::Complex(double realPart, double imPart) : m_R(realPart), m_I(imPart) { cout << "complex(" << m_R << "," << m_I << ")" << endl; } Complex::Complex(double realPart) : m_R(realPart), m_I(0) { } Complex::Complex() : m_R(0.0), m_I(0.0) { } string Complex::toString() const { ostringstream strbuf; strbuf << '(' << m_R << ", " << m_I << ')'; return strbuf.str(); } int main() { Complex C1; Complex C2(3.14); Complex C3(6.2, 10.23); cout << C1.toString() << '\t' << C2.toString() << C3.toString() << endl; }
The default constructor for this class gives default initialization to the two data members of the object C1. That initialization is the same kind that would be given to a pair of variables of type double in the following code fragment:
double x, y; cout << x << '\t' << y << endl;
Question | |
---|---|
What would you expect to be the output of that code? |
What happens if you leave out the member initialization list? For example, consider the following constructor definition.
Complex(double realPart, double imPart) { m_R = realPart; m_I = imPart; }
Each of the data members is first given default initialization and then given an assigned value. No error was made but the initialization was, essentially, wasted processing.
Look at one more example, which refers to Figure 2.2 and, for the sake of demonstration, assume that, as you see in
the Point
class diagram, there is only one Point
constructor in the class definition.
Further suppose that you define a Square
constructor without a member initialization
list.
Square::Square(const Point& ul, const Point& lr) { m_UpperLeft = ul; m_LowerRight = lr; }
Because you did not explicitly define a default constructor for the Point
class, and
you did define a two parameter constructor, the Point
class has no default constructor.
Consequently, the compiler reports an error in this Square
constructor.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |