Archived

This topic is now archived and is closed to further replies.

arkule

Virtual Methods ?

Recommended Posts

arkule    122
I somewhat know what these are, but i don''t understand what they do, why you use them, and when you would use them over non-virtual. I know they have to do with derived classes and overiding functions and stuff, but they are confusing me. Any help would be apreciated.

Share this post


Link to post
Share on other sites
Floppi    122
I think you should write a clear question for help. For me it is not clear what you really want to know. Afterwards I may be able to help you.

see ya Floppi

Share this post


Link to post
Share on other sites
B-Barlow    122
Basically they're used when you have methods of the base class which you want to call methods of the derived class.


        
class CShape
{
int x, y; // center point of shape

int oldX, oldY; // previous location of shape


MoveShape(int newX, int newY);

virtual EraseShape();
virtual DrawShape();
}

CShape::MoveShape(int newX, int newY)
{
oldX = x;
oldY = y;
x = newX;
y = newY;

EraseShape();
DrawShape();
}


class CCircle : CShape
{
int radius;

blah...;
blah...;
}

CCircle::EraseShape()
{
blah...;
blah...;
}

CCircle::DrawShape()
{
blah...;
}



This is the basis of polymorphism. you can use the same code to work with numerous differen classes as long as the classes each share the same interface. Which means I can define 20 different subclasses of CShape and just write one piece of code which moves 'em all around.


Edited by - B-Barlow on January 3, 2002 10:13:29 PM

Share this post


Link to post
Share on other sites
MetalicFog    122
virtual functions/methods are used to implement interface classes that are most commonly used to implement "framework". Framework is a quasi-generic program structure, kind of like a large scale design pattern. Reading the first two chapters of design pattern book (Gamma et al) will give you a good idea.

Share this post


Link to post
Share on other sites