Total members 11895 |It is currently Sat Dec 21, 2024 4:01 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





What actually facilitates the late binding in run time polymorphism?

Is it only the keyword VIRTUAL?

because for example
cpp code
#include ....

class A
{
public:
virtual void display () = 0;
};

class B: public A
{
public:
void display ()
{ cout << "B"; }
};

class C: public A
{
public:
void display ()
{cout <<"C";}
};

void main()
{
A a;
B b;
C c;
A * ptr[3];
ptr[0] = &a;
ptr[1] = &b;
ptr[2] = &c;

ptr[1]-> display(); //What's so run time about this?
}

I mean, it is known quite well that ptr[1] points to an object of type
B, this information is, according to what i think, available at
compile time. Or does the keyword VIRTUAL change everything? and if
that's so, what's the advantage of it run time polymorphism over
compile time polymorphism???




Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

You've made the #1 mistake of base/derived classes: You forgot the
virtual destructors. Get in the habit of including them even for cheesy
examples.


To answer your question: You can pass a derived class to a function
that takes a base class as its input. That function can then call the
derived methods by calling the base class methods of the same name and
numbers and types of parameters. However, abstract classes (i.e. where
you declare functions in a class 'virtual ...() = 0') are much more
annoying to deal with in this manner.

_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : run time polymorphism
 What is Polymorphism?!!     -  
 polymorphism     -  
 Inheritance & polymorphism checker code     -  
 What Time Is It?     -  
 max execution time     -  
 explode time     -  
 Time countdown     -  
 How to get system time in java     -  
 time for which a key was kept pressed by user     -  
 Time Stamp with mktime()     -  



Topic Tags

C++ OOP






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com