Hi all,

I'm having trouble will inheritance in c++. I am using a BaseNode class and deriving other node classes that can except different types of data. The problem I am running into is I can't find a way to make a decent copy function that can make an exact copy of any of these kinds of nodes. Here is what I have so far:
expTree.h

#include <iostream>
#include <string>

using namespace std;

#ifndef _EXPTREE_
#define _EXPTREE_

class BaseNode
{
public:
	// Default Constructor
	BaseNode()
	{
		left = NULL;
		right = NULL;
	}
	virtual void print() = 0;
protected:
	BaseNode* left;
	BaseNode* right;
	friend class expTree;
};

class node_int : public BaseNode
{
public:
	// Default Constructor
	node_int()
	{
		left = NULL;
		right = NULL;
		data = 0;
	}
	// Constructor
	node_int(int d)
	{
		left = NULL;
		right = NULL;
		data = d;
	}
	// Constructor
	node_int(int d, BaseNode* L, BaseNode* R)
	{
		left = L;
		right = R;
		data = d;
	}
	void print()
	{
		cout << data << endl;
	}

protected:
	BaseNode* left;
	BaseNode* right;
	int data;
	friend class expTree;
};

class node_string : public BaseNode
{
public:
	// Default Constructor
	node_string()
	{
		left = NULL;
		right = NULL;
		data = " ";
	}
	// Constructor
	node_string(string d)
	{
		left = NULL;
		right = NULL;
		data = d;
	}
	void print()
	{
		cout << data << endl;
	}
	
protected:
	node_int* left;
	node_int* right;
	string data;
};

class node_char : public BaseNode
{
public:
	// Default Constructor
	node_char()
	{
		left = NULL;
		right = NULL;
		data = ' ';
	}
	// Constructor
	node_char(char d)
	{
		left = NULL;
		right = NULL;
		data = d;
	}
	void print()
	{
		cout << data << endl;
	}
	
protected:
	node_int* left;
	node_int* right;
	char data;
};

class expTree
{
public:
	expTree();
	expTree(int);
	expTree(string);
	expTree(char);
	/*
	expTree& operator+(const expTree&);
	expTree& operator-(const expTree&);
	expTree& operator*(const expTree&);
	expTree& operator/(const expTree&);*/
	BaseNode* getRoot() const;

protected:
	BaseNode* root;

	BaseNode* copyTree( BaseNode* rootPtr ) const;


};

#endif

expTree.cpp

#include <iostream>

#include "expTree.h"

expTree::expTree()
{
	root = NULL;
}

expTree::expTree(int data)
{
	root = new node_int(data);
}

expTree::expTree(string data)
{
	root = new node_string(data);
}

expTree::expTree(char data)
{
	root = new node_char(data);
}

BaseNode* expTree::getRoot() const
{
	return root;
}

BaseNode* expTree::copyTree( BaseNode* rootPtr ) const
{
	BaseNode* copyN = NULL;

	if ( rootPtr != NULL )
	{
		copyN = new BaseNode(rootPtr);
			copyTree( rootPtr->left );
			copyTree( rootPtr->right ) );
	}
	return copyN;
}

main

#include <iostream>

#include "expTree.h"

using namespace std;

int main()
{
	expTree t1(10);
	t1.getRoot()->print();

	expTree t2("abc");
	t2.getRoot()->print();

	expTree t3 ('+');
	t3.getRoot()->print();

	system("PAUSE");
	return 0;

}

Any help would be greatly appreciated.... At some point I would like this copy function to be used with an overloaded operator+ to combine two trees together.

Thanks!

First you should be using templates. But the solution to your problem is to provide a virtual clone function. Look here for more information.

I'll look into the clone thing, but I think we are supposed to write a recursive copy function

I'll look into the clone thing, but I think we are supposed to write a recursive copy function

what is recursive copy?

This article has been dead for over six months. Start a new discussion instead.