|
From: Graham D. <g.d...@nt...> - 2003-07-21 18:03:23
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title></title>
</head>
<body>
<br>
<br>
<a class="moz-txt-link-abbreviated" href="mailto:pr...@le...">pr...@le...</a> wrote:<br>
<blockquote type="cite"
cite="midOF20FBEAD4.74884782-ON65256D6A.0036EE15@com">
<pre wrap="">Hi List,
I've been through numerous books & websites on this topic, but I _still_
can't quite understand what is the purpose of a virtual function.
>From what I understand, a virtual function is defined in a base class, but
isn't implemented there. The implementation goes in the derived class. Now
if this is true, then why should I need to define a function in my base
class at all? Since I'm eventually going to implement it elsewhere, I might
as well define it there too.</pre>
</blockquote>
But you might implement it in multiple derived classes. By defining it
in the base class, you can call the functionthrough the base class <span
style="font-style: italic;">without knowing what the actual derived
class is</span>. The declaration (and possible definition) in the base
class is defining a <span style="font-style: italic;">contract</span>
that all derived classes should implement. If you make the virtual
function pure (using the = 0 specifier), then you force derived classes
to implement that part of the contract (otherwise, the base provides a
default implementation that derived clases may or may not override). In
this way, you can write code against the base class's contract and know
that all derived classes will fulfill that contract.<br>
<br>
It's called an abstraction. You can define the concept, without
defining the details. For example, you can say that all birds fly
(well, apart from a few, but let's ignore them). However, different
birds fly in different ways: sparrows flap their wings furiously, while
albatrosses glide majestically. Now, there will be situations where
you're only interested in the fact that a bird flies, not in how it
flies. Your base class virtual function (bird::fly()) deals with the
fact of flying, so you might have a function:<br>
<br>
<span style="font-family: monospace;">void migrate(bird* b)</span><br
style="font-family: monospace;">
<span style="font-family: monospace;">{</span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> b->fly();</span><br
style="font-family: monospace;">
<span style="font-family: monospace;">}</span><br>
<br>
The thing about this function is that it is general. No matter what
specific bird you call the function with, it will work. So, if we have
a <span style="font-family: monospace;">class albatross</span> and a <span
style="font-family: monospace;">class sparrow</span> both derived from
<span style="font-family: monospace;">bird</span>, we can call <span
style="font-family: monospace;">migrate()</span> with objects of both
of those classes. Within <span style="font-family: monospace;">migrate()</span>,
the <span style="font-family: monospace;">sparrow </span>will do it
differently to the <span style="font-family: monospace;">albatross</span>,
but migrate doesn't care how, only that they do it.<br>
<blockquote type="cite"
cite="midOF20FBEAD4.74884782-ON65256D6A.0036EE15@com">
<pre wrap="">
Am I even on the right track here? Can someone take me back to base again?
Thanks.
Regards,
Pranav Negandhi
-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: <a class="moz-txt-link-freetext" href="http://www.vmware.com/wl/offer/345/0">http://www.vmware.com/wl/offer/345/0</a>
_______________________________________________
Dev-cpp-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Dev...@li...">Dev...@li...</a>
TO UNSUBSCRIBE: <a class="moz-txt-link-freetext" href="http://www23.brinkster.com/noicys/devcpp/ub.htm">http://www23.brinkster.com/noicys/devcpp/ub.htm</a>
<a class="moz-txt-link-freetext" href="https://lists.sourceforge.net/lists/listinfo/dev-cpp-users">https://lists.sourceforge.net/lists/listinfo/dev-cpp-users</a>
</pre>
</blockquote>
</body>
</html>
|