inheritance - Suggestions on C++ Inheritence -


i have been implementing red black trees in c++ using inheritence. have 4 classes, node, tree, rbnode, rbtree.

class node {     protected:         int data;         node *left;         node *right;         node *parent;     public:         node();         node(int data);         void print_node(ofstream &file);         node * find_node(int data);         void insert_node(tree *t);         void left_rotate_node(tree *t);         void right_rotate_node(tree *t);         void delete_node(tree *t); }  class tree {     protected:         node * root;         list<int> treedata;     public:         tree();         virtual node * get_root();         virtual void set_root(node *root_node);         void insert_into_tree();         void delete_from_tree();         virtual void print_tree(); } 

rbnode , rbtree inherit node, tree respectively. not able use functions of node class. example, function void tree::insert_node(tree *t);

even in class rbnode, function same work except funntion receives rbtree parameter. how can make use of same function without redeclaring in rbnode. thought of using casting inside function, how know classes object calling function.

please give me suggestions. new c++.

either inheritance not defined, or there confuson on insert_node(tree *t) defined in node , not in tree.

anyway, following minimal code example compiles well:

class tree;  class node { protected:     int data;     node *left,*right, *parent; public:     node(int data=0) : data(data), left(nullptr), right(nullptr), parent(nullptr) {}     void insert_node(tree *t) { cout << "insert" << endl; } }; class tree { protected:     node * root;     list<int> treedata; public:     tree() : root(nullptr) {} }; class rbsnode : public node {};  // public inheritance  class rbstree : public tree {};    ... rbsnode n;  rbstree t;  n.insert_node(&t);  

note in absence of public inheritance specifier, private inheritance assumed: within class have acces protected , public members of base class, outside, class, don't see inherited members. guess it's happenned you.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -