Advertisement

Virtual Methods ?

Started by January 03, 2002 07:17 PM
2 comments, last by arkule 22 years, 11 months ago
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.
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
Advertisement
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
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.

This topic is closed to new replies.

Advertisement