The definition of a constructor may contain an initialization section in the function heading.
For example, suppose you have the following class definition:
class Rational
{
public:
Rational( );
Rational(int whole_number);
Rational(int numerator, int denominator);
...
private:
int top;
int bottom;
};
//Initializes top to 0 and bottom to 1
Rational::Rational( ) : top(0), bottom(1)
{
//Empty body
}
//Initializes top to whole_number and bottom to 1
Rational::Rational(int whole_number) : top(whole_number), bottom(1)
{
//Empty body
}
//Initializes top to numerator and bottom to denominator
Rational::Rational(int numerator, int denominator)
: top(numerator), bottom(denominator)
{
//Empty body
}
The constructor can then be called either with no parameter, one parameter or two parameters.