Inheritance question

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{


virtual void print()=0;

}

class one::public foo{

print(){};

}
Last edited on
You didn't explain it very well.

> need to be in foo(see below) but i get an instantiation error
http://www.cplusplus.com/forum/articles/40071/#msg216270 (and next)

> the foo pointers cannot access the methods I need to access.
the `foo' pointers can only execute methods that `foo' declares.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class foo{

private:

string keyItem;

}

class bar:public foo{

public:

virtual void 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.

Last edited on
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.

Does that make sense?
It makes perfect sense, thank you. I however if i move the virtual method to foo instead of bar i get an unresolved external symbol error;

Is it possible to specify that the person constructor is called before the foo constructor?
Last edited on
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:
virtual void print() {}

EDIT: Added information.
Last edited on
You're Awesome, thank you.
Last edited on
You're welcome. :)

Cheers!
Topic archived. No new replies allowed.