I have been given some code for a Binary Search Tree and tasked with adding some functionality to it. But first, I would really like to gain a better understanding of the parameters/function definition for one of the functions given to me. The code:
void printTree( ostream & out = cout ) const
{
if( isEmpty( ) )
out << "Empty tree" << endl;
else
printTree( root, out );
}
void printTree( BinaryNode *t, ostream & out ) const
{
if( t != nullptr )
{
printTree( t->left, out );
out << t->element << endl;
printTree( t->right, out );
}
}
First off, I don't get why there is a const
after the parentheses at the end of the function declaration. Another thing that doesn't make sense to me is the first function declaration's parameters ostream & out = cout
. Why are the parameters = to something, I have never seen this. I do not understand what the ostream & out
is referencing in general. Running printTree()
with no arguments works just fine. Why does this work even though there is no function declaration for printTree
with no arguments?
By the way, this is all in C++.