|
||||||
| C Tutorials All C Tutorials and Code |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
||||
|
Virtual Functions and Polymorphism
It occured to me that with the STL series I'm working on, it might be important to talk a little bit about Virtual functions. Declaring functions as virtual makes the use of classes easier to manage, and allows for polymorphism, a central tenet of Object-Oriented programming. I know that these kinds of topics are difficult for newer programmers to pick up, and as such, I'll attempt to explain it as simply and with as much detail as possible to help people understand. Polymorphism, to put it rather simply, means that a derived class (a class that inherits properties from a base class) is able to change what a function declared as virtual in the base class actually does. We'll take the code example below as a set of classes that do NOT exhibit polymorphism.
C++ Code:
Code:
Class cls Shows: Class Num: 100 'Func' Shows: Class Num: 100 Class cls2 Shows: Class Num Doubled: 200 'Func' Shows: Class Num: 100 Class cls3 Shows: Class Num Tripled: 300 'Func' Shows: Class Num: 100 C++ Code:
Code:
Class cls Shows: Class Num: 100 'Func' Shows: Class Num: 100 Class cls2 Shows: Class Num Doubled: 200 'Func' Shows: Class Num Doubled: 200 Class cls3 Shows: Class Num Tripled: 300 'Func' Shows: Class Num Tripled: 300 So, what if you want to use the base classes version of show in the func function? Is that impossible? Not at all! Let's add another line of code to exemplify this, changing the func function now: C++ Code:
Code:
Class cls Shows: Class Num: 100
'Func' Shows: Class Num: 100
Class Num: 100
Class cls2 Shows: Class Num Doubled: 200
'Func' Shows: Class Num Doubled: 200
Class Num: 100
Class cls3 Shows: Class Num Tripled: 300
'Func' Shows: Class Num Tripled: 300
Class Num: 100
Finally, in the case of abstract classes (IE classes that aren't supposed to be declared and implemented in themselves, and are only supposed to be inherited by other derived classes), you can define virtual functions as such, and allow each derived class to implement their own version of that function: C++ Code:
Hope you learned something about the virtual function, and maybe a little more about how polymorphism works!
__________________
On Hiatus... |
|
||||
|
Re: Virtual Functions and Polymorphism
Very nice! +rep
__________________
Questions and Answers | Online News and Social Bookmarking | Code and Text Collaboration General Chat Forum |
|
||||
|
Re: Virtual Functions and Polymorphism
Nice little demo
+rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
![]() |
| Tags |
| c++, polymorphism, virtual keyword |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Subclasses & virtual functions | RobotGymnast | C and C++ | 7 | 12-13-2008 09:54 PM |
All times are GMT -5. The time now is 08:22 AM.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc