Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 8th, 2005, 10:16 PM   #1
sham
Newbie
 
Join Date: Mar 2005
Posts: 29
Rep Power: 0 sham is on a distinguished road
How run time polymorphism is achieved in C++ ?

How run time polymorphism is achieved in C++ ?
sham is offline   Reply With Quote
Old Apr 9th, 2005, 4:50 AM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Declare a member virtual in a class then subclass that class and re-implement that member. Here's an untested sample:
// A base class which has a virtual 'draw' method.
class Base {
    public:
        virtual void draw() {
            std::cout << "Base::draw()\n";
        }
};

// This class re-implements the draw method
class Sub : public Base {
    public:
        void draw() {
            std::cout << "Sub::draw()\n";
        }
};
Now lets say I have a function that takes a Base object pointer as a parameter and then calls draw on it:
void callDraw(Base *b) {
    b->draw();
}
I can create an instance of the subclass and pass a pointer to it to callDraw and it will call the subclasses draw method.
Sub *s = new Sub();
callDraw(s);      // Output: 'Sub::draw()'

Hope that's been helpful. If you need clarification on a certain point be more specific and i'll do my best.
Cerulean is offline   Reply With Quote
Old Apr 11th, 2005, 10:51 AM   #3
java_roshan
Professional Programmer
 
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4 java_roshan is on a distinguished road
I wud just like to add.
In Run time polymorphism, the compiler does not know which function it has to select for a particular object if there are two functions with the same name and same prototypes. The selection of the function is done while the code is executed.
__________________
Visit: http://www.somaiya.edu
java_roshan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:19 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC