Topic: help derived classes


Author: Mike Ferrel <ferrel@cis.santarosa.edu>
Date: 1995/04/12
Raw View
>class tree
>{
>protected:
>  tree *left, *right;
>public:
>  tree() {left = right = NULL;}
>  virtual void insert(int i) {cout << "Error, attempt to insert into
tree frame"; exit(1);
>  virtual void walk() {cout << "Error, attempt to walk over tree frame";
exit(1);}
>};
>
Your insert and walk functions should be pure virtual to prevent
instantiation of the base tree, as:

  virtual void insert(int) = 0;

>class intTree : public tree
>{
>  int item;
>  intTree *t;
>public:
>  intTree() {t = NULL;}
>  void insert(int i);
>  void walk();
>};
>

Your intTree is a different kind of beast than a Tree. A Tree is a node,
with no data member. An intTree contains data, but is not a node, it
merely contains a pointer to a node.

>void intTree::insert(int i)
>{
>  if(t == NULL)
>    {
>      t = new(intTree);
>      t->item = i;
>      t->left = NULL;
>      t->right = NULL;
>    }
>  else if(t->item > i)
>    t->left->insert(i);     // t->left or t->right is an error.

Well, sure it is. When you entered the first data, you set t -> left to
NULL. You are now trying to call the insert member of something that
isn't an object. That is pretty much guaranteed to bomb.

--
****************************************
Mike Ferrel, Instructor
Santa Rosa Junior College Santa Rosa, CA
ferrel@cis.santarosa.edu







Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/04/07
Raw View
andy@morinda.it.ntu.edu.au (Andrew Joraslafsky) writes:

>Could someone please point out my error in the following code, many
>hours of debugging has me on the edge.

Followups redirected to comp.lang.c++, since this is not appropriate for
comp.std.c++.

>** code as follows **
>
>class tree
>{
>protected:
>  tree *left, *right;
>public:
>  tree() {left = right = NULL;}
>  virtual void insert(int i) {cout << "Error, attempt to insert into tree frame"; exit(1);
>  virtual void walk() {cout << "Error, attempt to walk over tree frame"; exit(1);}

You should declare those methods as pure virtual:

 virtual void walk() = 0;
 virtual void insert(int i) = 0;

>};

>class intTree : public tree

This looks like not such a good design to me.
You should probably use templates rather than inheritence.

The other major problem is that you need to separate out the tree node
from the tree class.  Try something like this:

 template <class T>
 class Tree {
  struct Node {
   Tree *left, *right;
   T data;
  };
  Node *root;
 public:
  ...
 };

--
Fergus Henderson - fjh@munta.cs.mu.oz.au





Author: andy@morinda.it.ntu.edu.au (Andrew Joraslafsky)
Date: 1995/04/06
Raw View
Could someone please point out my error in the following code, many hours of debugging has me on the edge.

** code as follows **

class tree
{
protected:
  tree *left, *right;
public:
  tree() {left = right = NULL;}
  virtual void insert(int i) {cout << "Error, attempt to insert into tree frame"; exit(1);
  virtual void walk() {cout << "Error, attempt to walk over tree frame"; exit(1);}
};

class intTree : public tree
{
  int item;
  intTree *t;
public:
  intTree() {t = NULL;}
  void insert(int i);
  void walk();
};

void intTree::insert(int i)
{
  if(t == NULL)
    {
      t = new(intTree);
      t->item = i;
      t->left = NULL;
      t->right = NULL;
    }
  else if(t->item > i)
    t->left->insert(i);     // t->left or t->right is an error.
  else
    t->right->insert(i);
}

void intTre::walk()
{
  if(t != NULL)
    {
      t->left->walk();
      cout << t->item << endl;
      t->right->walk();
    }
}

void main()
{
  intTree t;
  int inpt;

  cin >> inpt;
  while(inpt != 0)
    {
      t.insert(inpt);
      cin >> inpt;
    }
  t.walk();
}

** end of code **

If someone knows a way around my problem or knows an easy solution to the problem I would appreciate any help.

Thanks in advance.

Andy

email andy@it.ntu.edu.au