| | |
Quick Polymorphism Tutorial
![]() |
Polymorphism is what makes using inheritance so powerful ( in my opinion). Here's a quick example of it using objects. This example uses 3 classes: CCar, and CSportsCar and CLuxuryCar which are derived from CCar.
these classes and their GetType() functions are actually pretty useless, but hopefully it will help to illustrate my point. Say you have a dialog class with a pointer to a CCar object declared in it (CCar* m_pCar).
now if your application has a user make a selection, based on that selection you can set the actual type of your object. For instance, your user selected the Luxury Car option:
m_pCar = new CLuxuryCar();
now if you call anywhere throughout your code, the function GetType() while this object is still valid, it will return "LuxuryCar". You can now change the type like so:
m_pCar = new CSportsCar();
You can now call m_pCar->GetType(), and it will return "SportsCar".
When you override a virtual function (like we did with GetType()) there is a vtable created with pointers mapped to which function belongs to the object depending on the type of object it is. Or if the virtual function is good enough, you can still call it without it being overridden in your derived class to use the base class's function. While the preceding function was useless, something like that can still be helpful in determing Run-Time Type Information so that depending on what type of object your pointer actually is at that time, you can determine if something special needs to be done. If anyone likes this post, or doesn't -- I'd appreciate your comments. I'm doing this at the end of my work day, so I can also make it clearer if needed and provide more of an example.:cool:
C Syntax (Toggle Plain Text)
class CCar { //constructors and other stuff... virtual CString GetType() { return "Car";) } ; class CSportsCar : public CCar { //constructors and other stuff... CString GetType() { return "SportsCar";) }; class CSportsCar : public CCar { //constructors and other stuff... CString GetType() { return "LuxuryCar";) };
these classes and their GetType() functions are actually pretty useless, but hopefully it will help to illustrate my point. Say you have a dialog class with a pointer to a CCar object declared in it (CCar* m_pCar).
now if your application has a user make a selection, based on that selection you can set the actual type of your object. For instance, your user selected the Luxury Car option:
m_pCar = new CLuxuryCar();
now if you call anywhere throughout your code, the function GetType() while this object is still valid, it will return "LuxuryCar". You can now change the type like so:
m_pCar = new CSportsCar();
You can now call m_pCar->GetType(), and it will return "SportsCar".
When you override a virtual function (like we did with GetType()) there is a vtable created with pointers mapped to which function belongs to the object depending on the type of object it is. Or if the virtual function is good enough, you can still call it without it being overridden in your derived class to use the base class's function. While the preceding function was useless, something like that can still be helpful in determing Run-Time Type Information so that depending on what type of object your pointer actually is at that time, you can determine if something special needs to be done. If anyone likes this post, or doesn't -- I'd appreciate your comments. I'm doing this at the end of my work day, so I can also make it clearer if needed and provide more of an example.:cool:
Last edited by cscgal; Apr 14th, 2006 at 5:27 pm.
•
•
Join Date: May 2004
Posts: 53
Reputation:
Solved Threads: 1
In batch files you can just have one certain test, eg %[part1]% that is in every line of the first part of the file, then %[part2]% and so on... then when the batch starts have it randomly run part2 or part3 or part4... each part will use the 'find' feature & reconstruct the batch untill the whole batch is remade in a different way. If anybody wants a full code I have one somewhere on my computer & will post it here.
![]() |
Similar Threads
- New Machine Wont Power CD Drive while connected via IDE (Troubleshooting Dead Machines)
- tutorial for UNIX sockets (C)
- How to stop opening multiple instance for a jar file (Java)
- Looking for a Python IDE like NetBeans (Python)
- Telnet Automation How to? (C++)
Other Threads in the C Forum
- Previous Thread: Recursion
- Next Thread: Debugging Link errors
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi







