- Oct 10, 2006
- 21,562
- 3
- 0
So I've got a project for my CISC 181 class. Without going into details, it revolves around OOP. Here's the problem.
How do I access a subclass method from a superclass pointer?
In my case, I have a general class WarPlane, of which Fighter is a subclass with some different attributes. However, there is a virtual function "compare" in WarPlane that is overloaded in Fighter. However, the overloaded function in Fighter compares data specific to the Fighter class, even though it accepts a WarPlane* as an argument (required by the project).
The function simply compares different attributes specific to its type (or universal attributes in the case of the original WarPlane function)
So I have:
WarPlane* raptor = new Fighter(/*args go here*/);
raptor->compare(/*another WarPlane*/Fighter declared like raptor*/);
Then, I try to compile (gcc) and I get
ERROR: No function getMissile() in class WarPlane
getMissile is a function specific to Fighter used in the compare function.
Thing is, while the object is a WarPlane*, it was created with a Fighter constructor, so it should have the same function set as a fighter right?
2 hours of googleing has yielded nil. Is this simply an impossible scenario, or am I just missing something blatantly obvious here?
How do I access a subclass method from a superclass pointer?
In my case, I have a general class WarPlane, of which Fighter is a subclass with some different attributes. However, there is a virtual function "compare" in WarPlane that is overloaded in Fighter. However, the overloaded function in Fighter compares data specific to the Fighter class, even though it accepts a WarPlane* as an argument (required by the project).
The function simply compares different attributes specific to its type (or universal attributes in the case of the original WarPlane function)
So I have:
WarPlane* raptor = new Fighter(/*args go here*/);
raptor->compare(/*another WarPlane*/Fighter declared like raptor*/);
Then, I try to compile (gcc) and I get
ERROR: No function getMissile() in class WarPlane
getMissile is a function specific to Fighter used in the compare function.
Thing is, while the object is a WarPlane*, it was created with a Fighter constructor, so it should have the same function set as a fighter right?
2 hours of googleing has yielded nil. Is this simply an impossible scenario, or am I just missing something blatantly obvious here?
