Runtime polymorphism

class Base
{
.....
.....
.....

virtual void display();

}


class Derived
{
...............
.........

void display();

}

int main()
{
Base*ptr=new Derived;
ptr->display();

return 0;
}


in the above polymorphism why is it called runtime polymorphism when i can say seeing the code itself that display() function in derived gets executed with ptr->display(),so how does it become runtime polymorphism when i could get decide at compile itself ??????????.....i am new to c++ please help
You're calling the function from a pointer to a Base, which could be pointing to any kind of Base, not just Derived. And so when you call methods from this pointer, it goes to the class of the pointer type (Base) then polymorphism at runtime sets up the correct actual function to be called.
A large scale C++ project could have hundres of classes. Consider the abstract factory pattern where a user of an object is given a base class pointer to something without knowing what the derived type actually is. Of course it is hard to understand the use when you are looking at such a simplistic example. Perform a web search on the aforementioned pattern, and look at some examples. Perhaps then you will understand. In many cases, the construction will occurr in a place that is far removed from where the user of the object makes a function call. The idea here is to minimize compile time dependencies by separating interface from the implementation. Factories build collections of similar objects, and return pointers to the base classes. Now you can code the user classes to the interfaces. Virtual functions must be implemented otherwise the linking will not work. So there will always be some kind of an implementation for virtual functions that the end user doesn't see. However, the interface is like a contractual obligation. Although there could be different ways of meeting the obligation, the obligation must be met somehow.
in the above polymorphism why is it called runtime polymorphism

The "above" will not compile. Derived does not inherit from Base, and there is no polymorphism of any kind.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cstdlib>
#include <ctime>

class Base
{
public:
    virtual void func() const { std::cout << "Base::func\n" ; }
};

class Derived : public Base
{
public:
    void func() const { std::cout << "Derived::func\n" ; }
};

Base* new_object()
{
    switch (rand() % 2)
    {
    case 0: return new Base ;
    case 1: return new Derived ;
    }
}

int main()
{
    std::srand(static_cast<unsigned>(std::time(0))) ;

    for ( unsigned i=0; i<10; ++i )
    {
        Base* ptr = new_object() ;
        ptr->func() ;
        delete ptr ;
    }
}

You did not inherit Derived from Base.:)

Well, consider the following code. Can you say what will be outputed?:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <ctime>
 
struct Base
{
   virtual void display() const { std::cout << "Base::display()\n"; }
   virtual ~Base() {} // = default;
};
 
struct Derived : Base
{
   virtual void display() const { std::cout << "Derived::display()\n"; }
};
 
int main()
{
   const size_t N = 10;
 
   std::srand( std::time( 0 ) );
 
   std::vector<Base *> v;
   v.reserve( N );
 
   std:: generate_n( std::back_inserter( v ), N, 
                             []{ return ( std::rand() % 2 ? new Base() : new Derived() ); } );
 
   for ( auto p : v ) p->display();
}
It's runtime polymorphism because you can change the Base pointer at runtime, which may change its behavior.

Consider this example, in which you cannot predict the output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// ...snip...

struct Base
{
    virtual void f() const { cout << "Base" << endl; }
    virtual ~Base() {}
};

struct Derived : public Base
{
    virtual void f() const { cout << "Derived" << endl; }
};

int main() {

    srand( time( 0 ) );
    int n = rand() % 100;

    Base * p;
    if( n % 2 == 0 ) {
        p = new Base();
    } else {
        p = new Derived();
    }

    p->f();  // ?

    delete p;
    return 0;
}


Similarly, the type could be swapped during runtime or upon a particular event, including user input. Each time the program is run may have a different behavior.

(Note that runtime polymorphism uses objects on the free store and not just on the stack.)

Static polymorphism binds the symbol (p) at compilation time and it cannot vary without recompilation.
Last edited on
By the way, the idea behind polymorphism is to be able to perform operations on groups of similar objects uniformly. That is, they share a common interface (such as the function f above) that can be called independent of the actual type.
Last edited on
Topic archived. No new replies allowed.