|
From: Jaap de W. <wo...@ne...> - 2003-07-21 10:17:55
|
You have access to functions without knowing of which class they are:
eg:
suppose you have a base class Person with virtual function: IsMale();
You have two derived classes: Man and Woman.
std::list<Person *> Employees;
You can add a man to your organisation by:
Employees.push_back(new Man(...));
or a woman
Employees.push_back(new Woman(...));
Now you can count the number of man in your organisation with
NrMan = 0;
for (i=Employees.begin(); i != Employees.end(); i++)
{
NrMan = (*i)->IsMale();
}
> -----Original Message-----
> From: pr...@le... [mailto:pr...@le...]
> Sent: Monday, July 21, 2003 12:04 PM
> To: dev...@li...
> Subject: [Dev-C++] Virtual functions?
>
>
> 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
>
>
>
>
> -------------------------------------------------------
> 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: http://www.vmware.com/wl/offer/345/0
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|