Classes as Class attributes

I'm a java programmer and am trying to build a small app that builds and prints trees. I have classes that contain instances of other classes as attributes...for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Tree
{
   public:
       Node root;
       Tree();
       ~Tree();
       void insertNode();
}
Tree::Tree()
{
   root=new Root();
}
void Tree::insertNode();
{
   if(Root==NULL)
   {
       Root=new Node();//how is this done in C++ without pointers
   }
   if (current.LChild==NULL)
   {
       current.LChild=new Node();//how is this done in C++ without pointers
   }
}
class Node
{
   public:
      Node parent;
      Node LChild;
      Node RChild;
      Node();
      ~Node();      
)
void main()
{
   Tree newTree();
   newTree.insertNode();
}


I hope what I'm asking makes sense...any input would be appreciated.
Operator new returns a pointer. So without pointers this

Root=new Node();//how is this done in C++ without pointers
can not be done.
What suggestions do you have for implementing this strategy without the new keyword...I want to avoid creating and deleting all of the pointers...

Can you show me an brief example?
Without pointers the tree can not be implemented because it is not known apriori how many nodes will be required.
I don't understand...the tree only holds a reference to the root...the nodes hold references to the parent and right and left children...the tree doesn't care how many nodes.

Also it seems that i can't declare a Tree object or Node object without instantiating it... so I can't declare

1
2
3
4
5
6
7
8
class Tree
{
   public:
       Node root;
       Tree();
       ~Tree();
       void insertNode();
}


without instantiating like:
 
        Node root();


then I cant assign a new node to root in any instance...

What up with using classes like this.

My app requires me to pass around nodes, as such, i dont want to use the pointers and keep track of deleting them.

edit: correction...if i declare Node root(); i have problems and if i declare Node parent in the Node class i get errors of incompatible types.
Last edited on
closed account (zb0S216C)
Let's go over a few things:

1) In C++, Trees are implemented with pointers. Without them, there's no tree structure[1].
2) C++ doesn't have a garbage collection system. Therefore, memory allocated with new must be deleted with delete[2]. Failure to do so will cause resource leaks.

3) current doesn't exist in any scope within your code. You need to declare identifiers before you use them. What you're after is this[3].

4)
troutguy wrote:
Tree newTree();

This is called the most vexing parse[4]. It gives the impression that the programmer is declaring a variable, but in fact, it's creating a function prototype. As a result, one would use the function prototype as a variable, and would consequently receive errors.

References:
[1] http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-using-C-C
[2] http://www.cplusplus.com/reference/std/new/operator%20delete/
[3] http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr035.htm

[4] http://en.wikipedia.org/wiki/Most_vexing_parse


You seemed to have missed heaps of information. I recommend going back and studying classes and DMA again.

Wazzak
The code's more or less ok. You just need to make a few adjustments.
1. In Java, objects are pointers. In C++ you have to make it explicit.
2. You have to declare objects before you use them. So you should declare Node before Tree.
3. The Node should hold something, I've added a string for demo purposes.
4. The insert method should be recursive.
5. Node doesn't need a parent pointer.

The corrected code becomes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Node
{
public:
    std::string value; // a node should have some data

//  Node* parent;
    Node* LChild;
    Node* RChild;
    Node(std::string s) { LChild = RChild = 0; value = s; }
//  ~Node();
};

class Tree
{
    Node* root;

    void insert(Node **, std::string value);

public:
    Tree();
    ~Tree();

    void insert(std::string value);
};

Tree::Tree()
{
    root = 0;
}

void Tree::insert(std::string value) // we add the payload to each node
{
    insert(root, value);
}

void Tree::insert(Tree **node, std::string value)
{
    if (*node == 0)
    {
        *node = new Node(value);
        return;
    }

    if (value < (*node)->value)
        insert(&((*node)->LChild), value);
    else
        insert(&((*node)->RChild), value);
}

int main()
{
    Tree newTree();
    newTree.insert("hello");
    return 0;
}


http://www.cplusplus.com/forum/beginner/74949/#msg402173
Last edited on
Thanks for all the insight...i get the pointer issue now...you must explicitly declare and pass the pointers and then delete them in the destructor...unlike java where the instance of a object is a pointer to the object...

Thanks to all...
Last edited on
Topic archived. No new replies allowed.