Reputation: 11
I have a C++ inheritance related question. I have a set of classes like this (I have not given the complete class structure coz I am lazy :) ). I want to access chiComponent class public methods using com pointer. How should I go about it?
Note that I am having to change the object which "com" is pointing to in a lot of places. So I do not think I can have another
chiComponent *ccom = <some_cast> com;
ccom.chiComponentMethod()
How should I go about it?
class Component{
};
class chiComponent : public Component {
public:
void chiComponentMethod()
{
cout << "Hi! Chi component function called!!";
}
}
class parent {
protected:
Component *com;
};
class child : public parent{
public:
child() {
com = new chiComponent();
}
}
Regards Arun
Upvotes: 1
Views: 243
Reputation: 7985
AVL tree is a type of a BST tree in the sense, that it supports all the BST tree operations and additionally may provide some more. So at the first glance it makes sense to derive AVLTree form the BSTTree. But BSTTree relies on an inner class BSTNode, that is not sufficient for providing all the AVLTree functionality.
If You derive AVLNode from BSTNode there are two options. You could change the BSTNode to support all the functionality the superclass needs, but it doesn't make much sense. It's way better to just design the node class, that supports all the functionality for both types of tries, from the very beginning.
The other option is to make all the BSTTree public methods virtual and explicitly cast the node to AVLNode in all implementations of those methods for the AVLTree class. You want to make those methods virtual anyway. Otherwise if someone is using a BSTTree interface to manipulate an actual instance of an AVLTree (having for example a BSTTree* pointer pointing to an AVLTree instance) disasters are going to happen.
Upvotes: 1
Reputation: 11
May be I will explain the problem more accurately.
I am trying to code AVL trees in C++ and I am extending AVL class from BST (Binary Search Tree) class. I also have two more classes
class BSTNode {
protected:
int data;
class BSTNode *left, *right;
};
class AVLNode : public BSTNode{
int height;
};
BST class uses BSTNode and AVL class uses AVLNode. I am using BST class services for things like inorder traversal etc. For insertion and deletion I am using AVL class functions (through virtual functions).
For obvious reasons I am having a BSTNode *root in BST class. The root changes whenever I do insert, delete etc. In AVL class is there any easy way to change **root'**s type to a more specific and more meaningful AVLNode type?
Upvotes: 0
Reputation: 83220
You will need to implement a pure virtual method in Component
; say, componentMethod()
:
virtual void componentMethod() = 0;
This will make Component
an abstract class, and thus uninstantiable.
Then in chiComponent
you can have:
virtual void componentMethod() {
chiComponentMethod();
}
Alternatively, you could just rename chiComponentMethod
to componentMethod
and put the implementation in there directly.
Additionally, any further implementations of Component
can implement their own functionality.
Also, I believe that
com = new chiComponent();
should be
*com = new chiComponent();
since com
is a Component *
.
Upvotes: 2
Reputation: 19034
Easiest way is to make virtual functions in Component and chiComponent, then always use the Component *com base class pointer.
Upvotes: 0
Reputation: 1716
Use virtual functions in the base class:
virtual void chiComponentMethod() = 0;
Upvotes: 0