protected

I am trying to make a small change to the original tree.h :

Here's the original tree.h

class Tree
{
private:
struct Node
{
int i;
Node* left, *right;
} *root;

void add(Node*&, int);
void remove(Node*&, int);
bool find(Node*, int) const;
void print(Node*) const;
void clear(Node*&);

public:
Tree();
~Tree();
void add(int);
void remove(int);
bool find(int) const;
void print() const;
void clear();
};

I am making a small change by making Node and root to protected.
Would this code snippet work:

class Tree
{
protected:
struct Node
{
int i;
Node* left, *right;
} *root;

Changing private to protected will not break anything, but unless you are going to inherit from Tree it's not going to make a difference.
1
2
3
4
class Tree
{
    // ...
} *root;

Please avoid global variables when you can. Declare and initialize the root itself inside main instead, and then pass around your root into functions.

Edit: In your case it wasn't global.
Last edited on
Thanks for the input.
How do I inherit form Tree?

Do I need to have a separate file add add this?

class Tree1:public Tree
{
protected:
struct Node
{
int i;
Node* left, *right;
} *root;

Would this work?
Last edited on
Gando, root isn't public. Here is the snippet with indentation that reflects the structure:
1
2
3
4
5
6
7
8
9
10
class Tree
{
    protected:
    struct Node
    {
        int i;
        Node* left, *right;
    } *root;
    ...
};

Gopinathpc, I don't think Peter87 was suggesting that you should inherit anything. He's saying that if you aren't deriving a new class from Tree, then it makes no different whether Node and Root are public or private.
lol I didn't even see that. Good catch, my bad gopinathpc.
Topic archived. No new replies allowed.