Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 8:33 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Question
 Question subject: run time polymorphism
PostPosted: Sat Nov 08, 2008 3:29 pm 
Offline
Proficient
User avatar

Joined: Sun Oct 19, 2008 3:47 pm
Posts: 281
Has thanked: 0 time
Have thanks: 1 time

What actually facilitates the late binding in run time polymorphism?

Is it only the keyword VIRTUAL?

because for example
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???


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: run time polymorphism
PostPosted: Sat Nov 08, 2008 3:31 pm 
Offline
Proficient
User avatar

Joined: Sun Oct 19, 2008 3:53 pm
Posts: 229
Has thanked: 0 time
Have thanks: 0 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.


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  

 Similar topics
 Plz Help,I want BOTH THREAD to work at a time in SAME WINDOW
 Selecting a Domain Name at the Time of Registration
 login using sessionid or time
 Time countdown
 Having a hard time with this class need help deseperately.
 calculate the load time for a page
 current system time
 Java Websphere Portal Developer Full-time position in IL
 Part Time Online Jobs
 First Name:raju visa process time

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: Google [Bot] and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team