Private and public method needed for the same recursive function

We recently worked on implementing recursive methods for multiple classes (trees, heaps, queues, linked lists, etc.) in C ++ for my computer science class. I had no problem writing method implementations, but something my professor spoke about briefly confused me and she never elaborated.

She mentioned that when implementing a recursive function for a class, you need to have both a public and a private version of that function.

For example (part of a linked list class definition):

public: 
  // constructors, destructors
  // member functions etc.
  findMax() { findMax(head); }
private:
  findMax(Node* first_node) { // actual code with recursive calls }
  Node* head;

      

So, there are two functions here, one private and one public. The entire public function makes a call to the private function and returns whatever it thinks is a response.

I have a few questions about this.

Is this what you should be doing? Does this only apply to recursive class methods? If so, what about recursive functions makes this necessary? Would you do this for other functions?

My intuition is that perhaps this is to prevent people using the public methods of the class from going into full use and end up calling the recursive function with parameters that will never lead to a stop / base case.

Can anyone describe this idea in general and explain it in an intuitive way?

Thank!

EDIT: Thanks everyone for your quick answers, they were very helpful! I think that maybe I cheated my professor and it really has nothing to do with recursion (although the example she wrote may have been) and is rather just a convention in object oriented programming.

The idea inside OOP is that findMax () requires the use of a private class (chapter) variable, which would be useful but would be a convenient public function. The private version of findMax (Node * n) allows the public user to find the maximum list without being able to access and mess up the private head.

Thanks everyone! Greetings.

+3


source to share


2 answers


"My intuition is that perhaps this is done to prevent people using public class methods from going into full dingus mode and end up calling a recursive function with parameters that will never lead to a stop / base. "

Your intuition to some extent. head

is managed within a class with a class and should not be entered as a parameter from the caller.

There is no particular relevance for recursion, but rather OOP data encapsulation principles:



head

must be managed from the class internally, although the function findMax()

must be available in the interface of the public class. To provide an appropriate internal implementation, the search is delegated to the implementation private

, which in this case is used recursively. But it doesn't really matter as mentioned.


As for your rights in the question. You should put as much code into the function as possible private

and keep it narrow. I don't see any reason why your professor put them in a public function.

+4


source


Is this something you should do?

Not.

Does this only apply to recursive class methods?

Even that.

If so, what about recursive functions makes this necessary?

Nothing.

Would you do this for other functions?



Yes, I would. These are the choices you make to organize your code. In general, the functions called by my class members public

are private

, unless there is any inherent need for them to be as well public

.

She mentioned that when implementing a recursive function for a class, you need to have both a public and a private version of that function.

Assuming it's close to verbatim, your teacher was either wrong or unclear. (It is also possible that she left an element of "choice" for a future lesson, whether it is ethical / reasonable or not, for discussion.)

You could also argue that in this case she is misleading you by giving two findMax

overloads of the same name; findMax()

and findMax(Head*)

are two separate functions, and you could (I would call it) the latter findMaxImpl(Head*)

, or something. Then you will see that recursion has nothing to do with it.

My intuition is that perhaps this is to prevent people using the public methods of the class from going into full use and end up calling the recursive function with parameters that will never lead to a stop / base case.

Interestingly, your intuition is much more reasonable here than C ++ actually.: P There is nothing in the language that could prevent, or even try to prevent, "full darng mode". Not really.

0


source







All Articles