0

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++.

Isaiah B.
  • 13
  • 1
  • 4
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 273K Jun 20 '20 at 17:38
  • You are asking about basic C++ fundamentals that are covered in every C++ textbook. Before you were "given some code for a Binary Search Tree and tasked with adding some functionality", I presume you would've also been given sufficient material that teaches these concepts? Unfortunately, stackoverflow.com is not a replacement for a C++ textbook. If there's something in your textbook's explanation of these language features that's unclear, feel free to quote a brief excerpt and explain your question. You can find a link to popular C++ textbooks above, where you can find more information. – Sam Varshavchik Jun 20 '20 at 17:47

1 Answers1

0

const after function declaration means that it is safe to use this function for const object of your class. Such functions can't change any fields in your object.

ostream & out is a global object std::cout that control output to a stream buffer of implementation-defined type. In simple words this object helps you to print information in your console or file.

ostream & out = cout means that cout is default parameter that will be passed in the function.

Another example of that:

void printX(int x = 5)
{
    std::cout << x;
}

If you don't provide this function any parameter then it will use default parameter.

printX(10); \\ will print 10
printX(); \\ will print 5

Why does this work even though there is no function declaration for printTree with no arguments? That's because this function will use cout to print out your tree.

I do not understand what the ostream & out is referencing in general.

You can't pass to a function a copy of cout (because it's copy constructor is disabled).

NixoN
  • 661
  • 1
  • 6
  • 19