-2

I've a problem, i've those class, that now works, because i've the attribute in public, but if i put it in private, or protected and inherited to the second class, it's not work, how can i do it? I need to respect OOP rules

template <typename T>
class BinaryNode
{
public:
    T key;
    BinaryNode<T>* left;
    BinaryNode<T>* right;
    BinaryNode<T>* parent;
    BinaryNode(){}
    ~BinaryNode(){}
};
And this:

template <typename T>
class BinarySearchTree
{
private:
    BinaryNode<T>* root;
    BinaryNode<T>* newNode(T key);

    BinaryNode<T>* minimum(BinaryNode<T>* node);
    BinaryNode<T>* maximum(BinaryNode<T>* node);
    BinaryNode<T>* successor(BinaryNode<T>* node);

    void insert(BinaryNode<T>** node, T key);
    BinaryNode<T>* search(BinaryNode<T>* node, T key);
    void distance(BinaryNode<T>* node, T key);
    void inorderTreeWalk(BinaryNode<T>* node);
public:
    BinarySearchTree();
    ~BinarySearchTree();

    void insert(T key);
    BinaryNode<T>* search(T key);
    void distance(T key);
    void inorderTreeWalk();

    BinaryNode<T>* remove(BinaryNode<T>* node);
};

There's the header file: https://www.dropbox.com/s/s8xsn3q01ysyy4f/classi.h

Can you show me how i can do? Thanks, Bye

Raid3nz
  • 9
  • 6

2 Answers2

0

You can make BinarySearchTree a friend class of BinaryNode.

template <typename T>
class BinarySearchTree;  // forward declaration

template <typename T>
class BinaryNode {
  friend class BinarySearchTree<T>; // we only allow BinarySearchTree with the same template T access
  private:
    // make everything you want private
}

This way BinarySearchTree is allowed to access private fields of BinaryNode.

Note that you do not need to create empty default Constructor/Destructors.

The following lines are not necessary:

BinaryNode(){}
~BinaryNode(){}

When should you use 'friend' in C++?

[Edited: added template arguments. See Class template with template class friend, what's really going on here? for other possibilities]

Community
  • 1
  • 1
close2
  • 308
  • 1
  • 7
  • What's wrong here? I got error when i inherited this. http://i.stack.imgur.com/zTsVk.png – Raid3nz Feb 18 '14 at 17:41
  • You would need to inherit from BinaryNode. But don't do this here. A BinarySearchTree **is not** a BinaryNode and you should only inherit if there is such a relation. (I.e. BinarySearchTree **is** a BinaryNode). Why don't you use my `friend` version? – close2 Feb 18 '14 at 17:53
  • I tried it, but i can use friend version, only with operator overloading – Raid3nz Feb 18 '14 at 17:58
  • If you are trying Jerry Coffin answer (he is absolutely right btw), you misunderstood. Put the class definition of class BinaryNode directly into BinarySearchTree: `template... class BinarySearchTree { private: class BinaryNode {...} }` – close2 Feb 18 '14 at 17:59
  • add template before class BinaryNode – close2 Feb 18 '14 at 18:10
  • or (better) just remove from all BinaryNode occurences. It is no longer necessary to make BinaryMode a templated class. – close2 Feb 18 '14 at 18:11
  • Uhm, i didn't understand, i tried to do, but i got too much error... There's all files, can you show me where's the problem? and how can i change it? You can test on my files? Now it works because i've returned to Friend, but if you do as Nested class, it not works... https://www.dropbox.com/s/atd4tif0tntz9to/header.h https://www.dropbox.com/s/s8xsn3q01ysyy4f/classi.h https://www.dropbox.com/s/dgh5pwvsd6jozpw/main.cpp https://www.dropbox.com/s/3qw7od6fysa2mr9/function.cpp Thanks, and sorry :( – Raid3nz Feb 18 '14 at 18:49
  • I compiled your code (I pity you for having to use windows). Apart from some obvious errors (did you forget to delete nodes?) everything looks fine. If you use a nested class you have to declare your functions like this: `typename BinarySearchTree::BinaryNode* BinarySearchTree::maximum(...) {`. But it doesn't really change much. Contrary to J. Coffin's statement there is no implicit Friends statement when using nested classes. – close2 Feb 19 '14 at 10:32
0

Since the basic idea of a BinaryNode is generally only meaningful in the context of the binary tree, you typically start by nesting its definition inside the BinarySearchTree class.

This has the side-effect of making the outer class a friend of the inner, so you can make all the node class' members private, and the tree can still access whatever it needs to.

Personally, I'd change at least a few other things, though they're not really related to the question you asked. For example, I'd change the remove to take a key item instead of a node:

BinaryNode* remove(T key);

I'd probably also change it to return something other than a BinaryNode *. IMO, BinaryNode is something of which the user should be able to remain entirely ignorant. That doesn't mean we need to (try to) deprive them of knowledge of it if they want it, but that they shouldn't normally have any real reason to know or care about it.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111