Thread: [Dev-C++] Virtual functions?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <pr...@le...> - 2003-07-21 09:51:35
|
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. Am I even on the right track here? Can someone take me back to base again? Thanks. Regards, Pranav Negandhi |
|
From: Ioannis V. <iv...@em...> - 2003-07-21 10:36:23
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of pr...@le...
> Sent: Monday, July 21, 2003 1:04 PM
> To: dev...@li...
> Subject: [Dev-C++] Virtual functions?
>=20
>=20
> Hi List,
>=20
> I've been through numerous books & websites on this topic,=20
> but I _still_
> can't quite understand what is the purpose of a virtual function.
Polymorphism.
=20
> From what I understand, a virtual function is defined in a=20
> base class, but
> isn't implemented there.
No. A virtual function can be defined in the base class. If it is not
defined (=3D0 is used), this indicates that the class is abstract, that =
is no
object of it can be created.
Example:
class base_whatever
{
// ...
public:
virtual void something() const=3D0;
};
// Error
base_whatever x;
class button
{
// ...
=20
public:
// We could make it abstract which would suit better to this
// example
virtual void make_pushed();
};
class flash_button: public button
{
// ...
public:
void make_pushed();
};
class rectangular_button: public button
{
// ...
public:
void make_pushed();
};
void somefunc(const button *btn)
{
// ...
btn->make_pushed();
}
Depending on which object btn points to, the appropriate make_pushed()
member function is called. This is called polymorphishm.
In C++ the time cost of such a virtual function call is the same, =
regardless
of the depth of the inheritance.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|
|
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>
|