Hey guys,
I'm learning C++ for the first time and have found it quite similar to Java in many ways. However, Inheritance differed a little bit, and I seem to have trouble with it, especially when it comes to trees.
The book I'm studying from defines Binary Trees as two seperate classes - an abstract binaryTree, which holds basic functions such as nodeCount, leavesCount, inorderTraversa l, etc. etc. This class also holds a protected variable of type treeNode - a struct defined in the header file holding a variable of some type, a pointer to the left child, and a pointer to the right child (each child is of type treeNode). The protected variable is entitled root - it is the first node inserted into the tree, and it is how the original variable keeps track of the tree.
The second class is called orderedBinaryTr ee. It is a subclass of binaryTree, inheriting its members publicly:
As a subclass of binaryTree, it should have access to the protected variable root - yet, when compiling a short test-program, my orderedBinaryTr ee variable does not recognize root - it obviously cannot see root in binaryTree.
I have basically copied the code for both classes straight out of the book, changing only a few things (such as indenting style and naming style) to my preference, yet it doesn't work. My teacher actually downloaded the official copy of these classes and received the same error.
Any ideas as to why orderedBinaryTr ee cannot access root?
I'm learning C++ for the first time and have found it quite similar to Java in many ways. However, Inheritance differed a little bit, and I seem to have trouble with it, especially when it comes to trees.
The book I'm studying from defines Binary Trees as two seperate classes - an abstract binaryTree, which holds basic functions such as nodeCount, leavesCount, inorderTraversa l, etc. etc. This class also holds a protected variable of type treeNode - a struct defined in the header file holding a variable of some type, a pointer to the left child, and a pointer to the right child (each child is of type treeNode). The protected variable is entitled root - it is the first node inserted into the tree, and it is how the original variable keeps track of the tree.
The second class is called orderedBinaryTr ee. It is a subclass of binaryTree, inheriting its members publicly:
Code:
template <class elem>
class orderedBinaryTree : public binaryTree {
...
...
...
}
I have basically copied the code for both classes straight out of the book, changing only a few things (such as indenting style and naming style) to my preference, yet it doesn't work. My teacher actually downloaded the official copy of these classes and received the same error.
Any ideas as to why orderedBinaryTr ee cannot access root?
Comment