| RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To |
| Creating Custom Delphi Components - Inside and Out | ||||||||||||||||||
| Page 9: Virtual functions and Abstract Classes | ||||||||||||||||||
Virtual Functions If I had code like this:
then DoSomething would execute TMyShape.Draw regardless of what was passed in. If a TCircle was passed in this would still be the case, and we wouldn't get a circle drawn. Clearly this isn't what we want to do - we want to execute the appropriate Draw procedure depending on what object was passed in, not the base class version! This is exactly where virtual methods come in.
When you declare a method as virtual you are saying to the compiler "I want you to call the appropriate derived version of this, not mine!" This means that you have an object declared as the base class and it magically calls the correct derived methods. This feature is very nifty and can be exceptionally useful in reducing code size or making your code very extensible.
To use virtual methods, add the "virtual" keyword after the method declaration. When you write sub-class versions of the procedure, remember the "override" part (a-ha! Now you finally know what that keyword is for!). Here's the previous example updated:
Now the correct method gets called depending on what shape gets passed in. Very neat and tidy, isn't it? Remember that you might want to use the "inherited" keyword if your base class does useful stuff.
Abstract Classes Look at the last example, just above. What exactly is a "Shape"? It could be described, maybe, as an enclosed space. However, maybe this description doesn't fit with your idea of a shape. You can't really be certain of what a shape is. If I asked you to "draw a shape" you might draw a circle, a square, or whatever. If I then said to you, "No! You're drawing a descendant of a shape. I said draw a shape!" you would get a little confused. It looks like we shouldn't really have a Shape, because it doesn't seem very easily defined.
The base class here, though, is handy. It can clearly define behaviour for the object (such as "Draw"). It should exist, then, but only as a starting point to introduce common functionality into its derived classes. Therefore, it seems, we should have it but shouldn't let it be created. Hence, it should be abstract.
To create an abstract class you just need to declare at least one virtual method as "abstract". It must be a virtual method, of course, because as you can't create an abstract class any attempt to access the method must be routed correctly to one of the base classes. Here's an example of changing the base class (TMyShape) to be abstract:
That's all there is to it! And with this, I must leave now (I'm the guy who mugs people and throws their wallets through the ATM money gap ;-)).
Question, Suggestions... First page > About components >
Page
1,
2,
3,
4,
5,
6,
7,
8,
9
|
||||||||||||||||||
All graphics (if any) in this feature created by Zarko Gajic.
| More Delphi |
|
· Learn another routine every day - RTL Quick Reference. · Download free source code applications and components. · Talk about Delphi Programming, real time. · Link to the Delphi Programming site from your Web pages. · Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC. |
|
· NEXT ARTICLE:
GDI Graphics In Delphi. From simple lines to direct API calls: the ultimate tutorial to GDI graphics in Delphi. This tutorial will introduce you to some of the techniques in the GDI Delphi drawing. Look for: drawing lines and shapes, drawing pictures, flicker-free drawings, off-screen bitmaps, GDI drawings the API way... |
| Stay informed with all new and interesting things about Delphi (for free). |
|
|
| Got some code to share? Got a question? Need some help? |
