Templates / Virtual Functions & Their Overloading
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
This is Tim Heagarty with the Virtual Training Company talking about C Plus Plus Fundamentals. We want to talk about virtual functions and overloading. These are functions that are defined within a class, they are class functions, methods of the class, and their purpose for being is kind of a placeholder in a base class, they assume that they will be replaced in a derived class. So when we make a derived class and derive it from a base class a virtual function can be overridden, it can be overloaded or changed. A non virtual function which is what we worked with so far through our fundamentals tutorial and I'm sure you've seen them in other places without the virtual keyword, those are not overridden, they are linked in at compile time and those functions and their addresses are linked together hard and that's called static linking. And whenever a class is derived from a bass class and you call a function that's in the bass class the one from the bass will be executed. Well we want to be able to run the function that is in the derived class not in the bass class. So in the bass class we have to define our functions as virtual, that will allow them to be linked at runtime so the derived classes can provide their own functionality, that's called dynamic linking. The way we do this is the compiler creates a V table, a virtual table as part of the V table is then populated at runtime with the addresses of the overriding functions. Let's take a look at a piece code and try to put this together alittle bit. Here we have a example, we have a polygon and this is our base class of polygon and it has a width and height that are protected but it has a set values and that's a standard method, a public method and it will take two integers and set the width and height, OK that's, we don't need to change that in derived classes, that's fine through any polygon, but the area of different kinds of polygons will be computed differently so we make that a virtual, it'll still return a int all the time but its gonna be a virtual function and here we just return zero. So a base polygon, would return nothing for the area, it won't do any work but when we derive a rectangle, here's a class rectangle, derived from the public base or from the base C Polygon and we use the public access specifier so that everything that's in the polygon will pass through with the same level of access, security on it. And then we make new public items, members in C rectangle and here we are making a new area so here we are overloading or overriding this virtual area, the area of the rectangle is the width times the height that we will return. So if we make a rectangle and call the dot area, we know how to, how to compute that, if we do a triangle, that needs a different kind of area and that's going to do a width times height over two. OK that's a different overridden area and again C triangle is derived from polygon, but we can change area because area is a virtual, so you got to remember that keyword, virtual if your going to be overriding these methods in your derived classes. That's what a virtual method and overriding of a virtual is all about.
Tutorial Information
| Course: | C++ Fundamentals |
| Author: | Tim Heagarty |
| SKU: | 33797 |
| ISBN: | 1-934743-09-7 |
| Release Date: | 2007-09-14 |
| Duration: | 4 hrs / 55 lessons |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 