Hey Guys, I have two abstract base classes, one derives from the other. Then i Have 3 other classes deriving from the second one and so on. Im having trouble getting my virtual method to work. I would think that it would need to be in foo(see below) but i get an instantiation error. And the foo pointers im storing in my 2-3-4 tree cannot access the methods I need to access. Let me me know if you need more info or if I didnt explain very well. thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class foo{
...
}
class bar::public foo{
virtualvoid print()=0;
}
class one::public foo{
print(){};
}
I'm trying to access a polymorphic method, print(), that are/is in three derived classes. I'm am instantiating these objects with a base class pointer. When I try to access these methods however they aren't found. The base class is a "keyedItem" class, foo, bar derives from foo, as a base "person" class, and classes one, two and three derive from bar as children classes, with their print() methods implemented. My question is with my foo pointer how can I access these methods.
class foo{
private:
string keyItem;
}
class bar:public foo{
public:
virtualvoid print()=0;
}
class one::public bar{
void print(){
....
}
}
class two::public bar{
public:
void print(){
...
}
}
class three::public bar{
public:
void print(){
...
}
}
void TreeClass::someFunc{
foo* myObject = new one(...);
myObject->print();//unable to access method
}
I am storing the foo pointers inside of a 2-3-4 tree. My thought is there might be a problem with when the constructors are called(like i may need to implicitly define when they are called?) but im not sure how to do this.
Well when using polymorphism you have to watch out for what a base class has knowledge of and what it doesn't.
For example, Bar has knowledge that anything inside of Foo, it's parent, exists because it is inheriting it all.
However, Foo has no idea what is inside of Bar because it's a one-way street. So! In your someFunc() you are creating objects of Foo that has no idea that print() exists because it didn't inherit the function from Bar. Bar instead inherited Foo.
See how that works? When you create an object of a base class you can only expect to use what is in the base class in your object because that is all that it knows about.
That is because the virtual function is a "pure" virtual function. That means that it has no definition (only a declaration). That is the meaning of the '=0' you placed after it.
You cannot instantiate a class with one or more "pure" virtual functions because it doesn't know what to with it. It's what we call an abstract base class. Abstract is defined as "existing in thought or as an idea but not having a physical or concrete existence." So the error occurs when, in your function, you are creating an object of the type Foo.
So to fix this, simply give the function a definition, so as to make the base class no longer abstract.
You could even fake it out by saying something like: virtualvoid print() {}