[ 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.
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; }
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?
Complex(double realPart, double imPart) { m_R = realPart; m_I = imPart; }
Square::Square(const Point& ul, const Point& lr) { m_UpperLeft = ul; m_LowerRight = lr; }
Consequently, the compiler reports an error in this Square
constructor.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |