|
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
|