derived classes with identical methods
say i have class A.
and i have classes B and C inherited from A.
and i have a pointer P that could point to either a B or a C type object.
And i have method D(int E); in both B and C.
I then do this:
P->D(1);
will this call the method of the B or C type object, whichever it is pointing too at the time?
oops.. i was so busy wording my question i forgot what board i was in
help me anyway tho, please?!
help me anyway tho, please?!
If D(int) is a virtual function, it will do B''s D func. if P points do a B, and C''s D func if P points to a C.
The following should work
The following should work
class A{virtual int D(int e);};class B:public A{virtual int D(int e){ //do B stuff}};class C:public A{virtual int D(int e){ //do C stuff}};
i never said anything about virtual functions, im getting confused!
ok..
i have class ZBase
and the class ZBitmap : class ZBase
and class ZeroGL : class ZBase
and ZBitmaps and ZeroGLs both have a function of
FLAGS BltBitmapTo(ZBitmap *aFromPtr, UINT aX, UINT aY);
i start all my argument variables with a
and then i have
ZBase *Ptr;
can I use Ptr->BltBitmapTo(...) reguardless of if Ptr points to a ZBitmap or a ZeroGL?
ok..
i have class ZBase
and the class ZBitmap : class ZBase
and class ZeroGL : class ZBase
and ZBitmaps and ZeroGLs both have a function of
FLAGS BltBitmapTo(ZBitmap *aFromPtr, UINT aX, UINT aY);
i start all my argument variables with a
and then i have
ZBase *Ptr;
can I use Ptr->BltBitmapTo(...) reguardless of if Ptr points to a ZBitmap or a ZeroGL?
(using the first example)
If function D() in class A *is not* declared as virtual, calling D() from a pointer to B or C will call class A''s D().
If function D() in class A *is* declared as virtual, calling D() from a pointer to B or C will call the D() of B or C, respectively.
That''s what Big B was saying.
.travois.
If function D() in class A *is not* declared as virtual, calling D() from a pointer to B or C will call class A''s D().
If function D() in class A *is* declared as virtual, calling D() from a pointer to B or C will call the D() of B or C, respectively.
That''s what Big B was saying.
.travois.
Also note that you don''t need to declare the method in B nor C as virtual, only in the base class The rule goes: "If you override a method, declare the overridden method as virtual". You should never ever override non-virtual method.
Cheers, Altair
"Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." - Albert Einstein
Cheers, Altair
"Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." - Albert Einstein
ok ok.. my confusion was in what the virtual part did, i couldnt remember. just looked it up, understand now.. except! Do I have to have the class the others are derived from have the virtual function? what if class B and C have each a D() and they are derived from class A which has no D()? will it work?
Naturally it won''t work, because you try to access D() through base class (A) interface. The idea is, that you can pass any class which is derived from A as argument whatfor no assumptations can be made about the interface of derived classes _except_ that it has all methods that A do have. If you have declared some methods in A as virtual, some methods may also have new implementations in derived classes. If you have declared method in A as _pure_ virtual, derived class also has to implement the method.
Cheers, Altair
"Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." - Albert Einstein
Cheers, Altair
"Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." - Albert Einstein
#include <iostream.h>class A{public: virtual void D() = 0;};class B : public A{public: void D() { cout << "B :: D()" << endl; }};class C : public A{public: void D() { cout << "C :: D()" << endl; }};void main(){ A *P = new B; A *P2 = new C; P->D(); P2->D(); delete P; delete P2;}// should print "B :: D()", "C :: D()", right?
-----------------
The Goblin
-----------------
"Oh, God..."
"Yes?" <- My Response
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement