Mail Archives: djgpp/1998/05/01/15:46:20
From: | Nathan Cournia <nac2b AT frank DOT mtsu DOT edu>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Inheritance Problem
|
Date: | Fri, 01 May 1998 14:20:11 -0500
|
Organization: | Middle Tennessee State University
|
Lines: | 105
|
Message-ID: | <354A206B.586A3F60@frank.mtsu.edu>
|
NNTP-Posting-Host: | knuth.mtsu.edu
|
Mime-Version: | 1.0
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
I'm having a problem with inheritance. In my client file I have this
line:
wordTree Word;
I also have this line:
Word.Search(Item, Success);
The previous line calls a public Search function which in turn calls a
private recursive function. The problem is that the public function is
calling the private function for the parent class instead of the derived
class. Can anybody help. Here are the header file for the two classes
and
the server file for the derived class.
Thanks,
Nathan Cournia
//FILE: BSTree.h
template <class T> struct bstNode;
template <class T> class bstClass
{
public:
bstClass(); //default constructor
virtual ~bstClass(); //destructor
bstClass(const bstClass<T>& Tree); //copy constructor
virtual bool Empty(); //checks of tree is empty
virtual void Insert(T& NewItem, bool& Success);
//inserts item in tree
virtual void PrintInorder(); //prints tree inorder
virtual void PrintPostorder(); //prints tree postorder
virtual void PrintPreorder(); //prints tree preorder
virtual void Search(T Item, bool Success); //searches tree for item
protected:
void InsertData(bstNode<T>*& P,T& NewItem, bool& Success);
//recursive insert function
void Inorder(bstNode<T>* P); //inorder recursice function
void Postorder(bstNode<T>* P); //postorder recursive function
void Preorder(bstNode<T>* P); // preorder recursive function
void CopyTree(bstNode<T>* P, bstNode<T>* NewP);
//copies tree at P to NewP
void SearchTree(bstNode<T>* P, T Item, bool Success);
//searches for Item in tree
void DestroyTree(bstNode<T>*& P); //recursive function to destroy
tree
public:
bstNode<T>* Root; //root of the tree
}; //end of bstClass
#include "BSTree.cc"
//FILE: WordTree.h
#include "Word.h" //for wordClass
#include "BSTree.h"
class wordTree: public bstClass<wordClass>
{
protected:
void SearchTree(bstNode<wordClass>* P, wordClass Item, bool
Success);
}; //end of wordTree
#include "WordTree.cc"
//FILE: WordTree.cc
void wordTree::SearchTree(bstNode<wordClass>* P, wordClass Item, bool
Success)
{
if(P==NULL) //check if the list is empty
Success=0;
else if(Item==P->Data) //item found
{
Success=1;
P->Data.IncFreq();
P->Data.AddLineNumber();
}
else if(Item<P->Data)
SearchTree(P->LChild, Item, Success); //search left tree
else
SearchTree(P->RChild, Item, Success); //search right tree
} //end of SearchItem()
- Raw text -