Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hey guys, this is quite an easy and obvious question, but I couldn't find the answer in my textbooks or online.
Here's a problem:
I have a class A, a class B derived from class A, and a class C derived from class B
class A
class B : public class A
class C : public class Beach of this classes has a method called print( ) , so it's like this:
class A
{ print() {...} ;
};class B : public A
{
print() {
A::print() ;
... ;
}
};And
class C : public B {
print( ) {
B::print() ;
... ;
} } ;there's dynamic binding, so I have something like this
A * a = new B() ;
A * b = new C() ;
a->print() ;
b->print() ;Logically, print() in class A shoud be a virtual function, e.g. virtual print ( ) ;
But what about print( ) in class B?? It's a base class for class C, afterall.
Also, when I make print( ) in either class virtual method, my program doesn't compile, it spews thousands of linking errors on my screen. But if I leave virtual out, print() doesn't work as desired, only print() of class A is called for objects a and b.
I'm tired of this. I can't ask anyone about it, because it's late at night, I can't find it on the Internet, because all examples portray classes derived from two different classes, but not from another derived class. My textbooks don't explain how to do it. I don't know how to link the damn thing so that it would workthank you for any help.

Hi.
If you are talking about C++ then:
1) it is enough to declare a method as a virtual one only in the base class , in all derived classes the method will be virtual too. So if you declare A::print() as a virtual, them B::print() and C::print() will be virtual too. On the other hand it wont cause any damage if you declare all the print() methods as virtual;
2) It would be very usefull if you supply an example of the error message you get.
3) As far as I understand from your question, if you declare A::print() as virtual it should solve the problem.

![]() |
C Prgramming help
|
QBasic newbie question
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |