![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 29
Rep Power: 0
![]() |
How run time polymorphism is achieved in C++ ?
How run time polymorphism is achieved in C++ ?
|
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
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";
}
};void callDraw(Base *b) {
b->draw();
}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. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|